I have an XML with the following data...
<movies total="3">
<movie cover="9_pro.jpg" Title="A Very " MovieDuration="1.29" showtime="2:50 PM" theatre="UV3"/>
<movie cover="5_pro.jpg" Title="Par" MovieDuration="1.24" showtime=" 12:00 PM" theatre="University Village 3"/>
<movie cover="8_pro.jpg" Title="PinBts" MovieDuration="1.30" showtime="9:20 PM" theatre="University Village 3"/>
</movies>
I want to parse this using JDOM parser in a servlet...I have used the following code so far:
try
{
doc=builder.build(url);
Element root = doc.getRootElement();
List children = root.getChildren();
out.println(root);
for (int i = 0; i < children.size(); i++)
{
Element movieAtt = doc.getRootElement().getChild("movie");
//out.println(movieAtt.getAttributeValue( "cover" ));
out.println(movieAtt.getAttributeValue( "Title" ));
//out.println(movieAtt.getAttributeValue( "MovieDuration" ));
//out.println(movieAtt.getAttributeValue( "showtime" ));
//out.println(movieAtt.getAttributeValue( "theatre" ));
}
}
However my code returns values for the first child element of root repetitively 3 times. I assume this is because i have all 3 child name as "movie" only.
So i want to distinguish these, and make the count to next movie child with attributes like Title="par" etc..
Been figuring out this since so long but could not find. Help would be really appreciable
Your's is not working because even though you are looping 3 times, you are always fetching the same (first) node through:
Element movieAtt = doc.getRootElement().getChild("movie");
Try this:
(untested)
Element root = doc.getRootElement();
List children = root.getChildren();
out.println(root);
if (children != null)
{
for (Element child : children)
{
out.println(child.getAttributeValue( "Title" ));
}
}
The best way is to get the attribute from the children list instead of asking for movieAtt again.
I have never used JDOM but my pseudocode would be as follows:
for (int i = 0; i < children.size(); i++) {
Element e = children.get(i);
String title = e.getAttributeValue("Title");
}
Related
I need advise about how to convert XML to a custom Excel with Java
I need to convert XML to Excel with a custom layout. I found a POI and it seems like it can help with this task. But I don't have this experiences and as I understood POI works the best with in memory trees like DOM. I started to pars my XML(I can show a small part of, it's really big and goes deep)
<advantage>
<companies>
<name>Name1</name>
<name>Name2</name>
<name>Name3</name>
<name>Name4</name>
<name>Name6</name>
</companies>
<companyPreCode>
<PreCode>1</PreCode>
<PreCode>2</PreCode>
<PreCode>3</PreCode>
<PreCode>4</PreCode>
<PreCode>6</PreCode>
</companyPreCode>
by using DOM as I saw in one online tutorial like this
Document xmlDoc = getDocument("./src/xmlForTest.xml");
xmlDoc.getDocumentElement().normalize();
System.out.println("Root element of the doc is :\" "+ xmlDoc.getDocumentElement().getNodeName() + "\"");
NodeList listOfAdvantage = xmlDoc.getElementsByTagName("advantage"); //first we need to find total number of Advantage blocks
int totalAdvantage = listOfAdvantage.getLength();
System.out.println("Total no of advantage : " + totalAdvantage);
for (int s = 0; s < listOfAdvantage.getLength(); s++) //get into advantage
{
Node AdvantageNode = listOfAdvantage.item(s);
System.out.println("advantage number : " + s);
if (AdvantageNode.getNodeType() == Node.ELEMENT_NODE)
{
Element AdvantageElement = (Element) AdvantageNode;
NodeList CompanyList = AdvantageElement.getElementsByTagName("companies"); // find node companies
System.out.println("companies number : " + CompanyList.getLength());
for(int cl = 0; cl < CompanyList.getLength(); cl++) {
NodeList CompanyNameList = CompanyList.item(cl).getChildNodes(); //AdvantageElement.getElementsByTagName("name");
for (int j = 0; j < CompanyNameList.getLength(); j++) {
Node childNode = CompanyNameList.item(j);
if ("name".equals(childNode.getNodeName())) {
for (int nl = 0; nl < CompanyNameList.getLength(); nl++) {
Element CompanyNameElement = (Element) CompanyNameList.item(nl);
NodeList textFNList = CompanyNameElement.getChildNodes();
System.out.println("Company: " + nl + " :" + (textFNList.item(0)).getNodeValue().trim());
CompaniesNames.add((textFNList.item(0)).getNodeValue().trim());
}
}
}
}
}// end of if clause
}// end of for loop with s var
and now I have several questions
How to make this parsing easier? my file is big and in some places I Have the same tags for different things, like Name can be for company, product or a person. But it's getting hard to retrieve it one by one the way I did it
How to feed this data later into POI so I can start using this POI to build my Excel files? Because right now I have a set of ArrayLists with my data from different tags and I just don't know what I need to next with it
I am facing some problem while parsing my xml using JDOM parser.It gives me the commented lines when I am trying to retrieve the content.Is there a way so that we can ignore these commented lines.
Java Code:
SAXBuilder jdomBuilder = new SAXBuilder();
// jdomDocument is the JDOM2 Object
Document jdomDocument = jdomBuilder.build("C:/manu/WebservicesWS/DynamicXmlParse/src/PO_XML.xml");
// The root element is the root of the document. we print its name
System.out.println(jdomDocument.getRootElement().getName()); // prints
// "rss"
Element rss = jdomDocument.getRootElement();
System.out.println(rss.getNamespaceURI());
List<Element> rssChildren = rss.getChildren();
// getElement(rssChildren);
for (int i = 0; i < rssChildren.size(); i++) {
Element rssChild = rssChildren.get(i);
System.out.println(rssChild.getName());// prints 'title' and 'link'
List<Content> rssContents = rssChild.getContent();
for (int j = 0; j < rssContents.size(); j++) {
Content content = rssContents.get(j);
System.out.println(content.getValue());
}
}
XML Structure
<interchange-control-header>
<control-number>2</control-number>
<sender-id>ZZ:IQAAOBUYER7</sender-id>
<receiver-id>ZZ:33347456972</receiver-id>
<!--sender-id>ZZ:IQAAOBUYER2</sender-id>
<receiver-id>ZZ:IQAAOSUPPLIER2</receiver-id>
<sender-id>IQAOrionBuyer</sender-id>
<receiver-id>IQAOrionSupplier</receiver-id-->
<date-time>2012-06-29T09:30:47-05:00</date-time>
<control-version>1</control-version>
<usage-indicator>T</usage-indicator>
<is-copy>0</is-copy>
</interchange-control-header>
current Output
interchange-control-header
2
ZZ:IQAAOBUYER7
ZZ:33347456972
sender-id>ZZ:IQAAOBUYER2</sender-id>
<receiver-id>ZZ:IQAAOSUPPLIER2</receiver-id>
<sender-id>IQAOrionBuyer</sender-id>
<receiver-id>IQAOrionSupplier</receiver-id
2012-06-29T09:30:47-05:00
1
T
0
required Output:
interchange-control-header
2
ZZ:IQAAOBUYER7
ZZ:33347456972
2012-06-29T09:30:47-05:00
1
T
0
Comments are considered to be an identifiable part of an XML document, along with the more obvious things like Elements. Other content to be aware of are Processing Instructions, Text, and Entity References.
When you call getContent on the rssChild Element, you get the Comment content, and it's value is the text inside that content.
It appears you just want to print out the text content of each child element, not of all content.
The simple way to get all child elements is to use the getChildren() method (instead of the getContent). You are already using the getChildren in other places, so I am not sure why you forgot to use it here....
Additionally, you can simplify the loops to be for-each style... this code:
List<Element> rssChildren = rss.getChildren();
// getElement(rssChildren);
for (int i = 0; i < rssChildren.size(); i++) {
Element rssChild = rssChildren.get(i);
System.out.println(rssChild.getName());// prints 'title' and 'link'
List<Content> rssContents = rssChild.getContent();
for (int j = 0; j < rssContents.size(); j++) {
Content content = rssContents.get(j);
System.out.println(content.getValue());
}
}
could be:
for (Element rssChild : rss.getChildren()) {
System.out.println(rssChild.getName());// prints 'title' and 'link'
for (Element subRss : rssChild.getChildren()) {
System.out.println(subRss.getValue());
}
}
I have a xml file that looks like this.
<Device>
<Staff>
<Name>ABC</Name>
<Name>Hello</Name>
</Staff>
<Connect>
<Speed>123</Speed>
<Speed>456</Speed>
</Connect>
</Device>
I need help in retrieving the value of name & speed as i have never tried xml before. I am getting null pointer exception whenever I try to retrieve the element values. Any help is appreciated.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// Load the input XML document, parse it and return an instance of the
// Document class.
Document document = builder.parse(new File("C:/Users/AA/Desktop/eclipse/lol/testing.xml"));//change to own directory
NodeList nodeList = document.getDocumentElement().getChildNodes();
System.out.println(nodeList.getLength());
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(i);
Element elem = (Element) node;
// Get the value of the ID attribute.
// String ID = node.getAttributes().getNamedItem("ID").getNodeValue();
// Get the value of all sub-elements.
String name = elem.getElementsByTagName("Name")
.item(0).getChildNodes().item(0).getNodeValue();
Integer speed = Integer.parseInt(elem.getElementsByTagName("Connect")
.item(0).getChildNodes().item(0).getNodeValue());//null pointer exception happens here
staffList.add(new staff(name));
connectList.add(new connect(speed));
}
}
// Print all employees.
for (staff stl : staffList)
{System.out.println("STAFF "+stl.getName());}
for (connect ctl : connectList)
{System.out.println("Connect "+ctl.getSpeed());}
You will have null pointer exceptions because you're assuming that in every iteration of the for loop, the desired nodes have children elements:
String name = elem.getElementsByTagName("Name")
.item(0).getChildNodes().item(0).getNodeValue();
In the above code, you are accessing the first child of a Name element which is a text node (e.g. ABC), and then getting its children nodes, which will cause an exception since there no children elements inside the text node.
Likewise,
Integer speed = Integer.parseInt(elem.getElementsByTagName("Connect")
.item(0).getChildNodes().item(0).getNodeValue());
will cause an exception in one of the iterations of the loop where elem corresponds to Connect itself.
You can try the following code instead:
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(i);
Element elem = (Element) node;
// Get the value of the ID attribute.
// String ID =
// node.getAttributes().getNamedItem("ID").getNodeValue();
// Get the value of all sub-elements.
NodeList nameNodes = elem.getElementsByTagName("Name");
for(int j = 0; j < nameNodes.getLength(); j++) {
Node nameNode = nameNodes.item(j);
staffList.add(new staff(nameNode.getTextContent()));
}
NodeList speedNodes = elem.getElementsByTagName("Speed");
for(int j = 0; j < speedNodes.getLength(); j++) {
Node speedNode = speedNodes.item(j);
connectList.add(new connect(Integer.parseInt(speedNode.getTextContent())));
}
}
P.S.: Try to use class names that start with an uppercase.
You want getTextContent() rather than getNodeValue() - the latter always returns null for element nodes.
See: DOMDocument getNodeValue() returns null (contains an output escaped string)
I have to develop an android application.
Here i have follows following xml format.
<Product>
<product name="viki" productid="111">
<ProductType>
<producttype>Nokia</producttype>
<producttype>Samsung</producttype>
</ProductType>
</product>
</Product>
Here i have to get the producttype for particluar product.so i have wrote the following code:
if(subCategoryChildNode.hasChildNodes()){
// parse 'Subcategory' childs
NodeList productNL = subCategoryChildElmt.getElementsByTagName("product");
if(productNL.getLength() > 0){
ArrayList<Product> productAL = new ArrayList<Product>();
Product productBean = null;
for(int pCnt=0;pCnt<productNL.getLength();pCnt++){
Node productNode = productNL.item(pCnt);
Element productElmt = null;
// parse 'product' tag attributes
if(productNode.hasAttributes()){
productBean = new Product();
productElmt = (Element)productNode;
productBean.setmProductName(productElmt.getAttribute("name"));
}
if(productNode.hasChildNodes()){
NodeList productTypeNL = productElmt.getElementsByTagName("ProductType");
if(productTypeNL.getLength() > 0){
ArrayList<ProductType> ProductTypeAL = new ArrayList<ProductType>();
ProductType productTypeBean = null;
for(int ptCnt=0;ptCnt<productTypeNL.getLength();ptCnt++){
Node productTypeNode = productTypeNL.item(ptCnt);
Element productTypeElmt = null;
if(productTypeNode.hasChildNodes()){
productTypeBean = new ProductType();
productTypeElmt = (Element)productTypeNode;
productTypeBean.setmProductType(XMLfunctions.getValue(productTypeElmt,"producttype"));
System.out.println("Product Types are "+ " "+XMLfunctions.getValue(productTypeElmt,"producttype"));
ProductTypeAL.add(productTypeBean);
}
productBean.setmProductTypes(ProductTypeAL);
}
productAL.add(productBean);
}
}
subCategoryBean.setmProducts(productAL);
}
}
subCategoryAL.add(subCategoryBean);
}
Here am getting the value is nokia alone.but i need to display the value nokia,samsung...if i have to run the app means getting single value.but i need to get the list of all values..
What's wrong in my code .. please check and give me solution fot these ???
The reason you're getting only one <producttype> (Nokia) instead of the complete list because you're looping over the length of <ProductType> nodes thinking you're looping over the <producttype> ones.
So, you need another inner loop to cover all the child product type nodes like
for(int ptCnt=0; ptCnt < productTypeNL.getLength(); ptCnt++) {
Node productTypeNode = productTypeNL.item(ptCnt);
if(productTypeNode.hasChildNodes()){
NodeList childProductTypeNL = productTypeNode.getChildNodes();
System.out.print("Product Types are: ");
for (int cptCnt=0; cptCnt < childProductTypeNL.getLength(); cptCnt++) {
productTypeBean = new ProductType();
productTypeBean.setmProductType (
childProductTypeNL.item(cptCnt).getTextContent());
System.out.print(productTypeBean.getmProductType() + ", ");
ProductTypeAL.add(productTypeBean);
}
}
productBean.setmProductTypes(ProductTypeAL);
}
I've directly used the Node.getChildNodes() and Node.getTextContexnt() methods, instead of type casting to Element first and using its methods or the XMLfunctions utility class.
I also recommend using different names for child nodes instead of relying on using a different case to avoid such problems in future. A simple way to avoid name collision (when you're not able to come up with a different name) is to simply use a plural like <ProductTypes> for the parent tag.
However, a better approach when you need to parse deep within a DOM tree is to use an XPath to directly get the list of nodes you're interested in. I'm not entirely sure what the program does but just to give you an example an XPath like
String xpath = "//product[#name=\"viki\"]/ProductType/producttype";
would give you the NodeList for <producttype> nodes directly.
I'd say one of the problem of your code (might be others), is that you declare your productTypeBean and productTypeElmt before your for loop, and since it's not required after, it isn't needed.
if(subCategoryChildNode.hasChildNodes()){
// parse 'Subcategory' childs
NodeList productNL = subCategoryChildElmt.getElementsByTagName("product");
if(productNL.getLength() > 0){
ArrayList<Product> productAL = new ArrayList<Product>();
Product productBean = null;
for(int pCnt=0;pCnt<productNL.getLength();pCnt++){
Node productNode = productNL.item(pCnt);
Element productElmt = null;
// parse 'product' tag attributes
if(productNode.hasAttributes()){
productBean = new Product();
productElmt = (Element)productNode;
productBean.setmProductName(productElmt.getAttribute("name"));
}
if(productNode.hasChildNodes()){
NodeList productTypeNL = productElmt.getElementsByTagName("ProductType");
if(productTypeNL.getLength() > 0){
ArrayList<ProductType> ProductTypeAL = new ArrayList<ProductType>();
for(int ptCnt=0;ptCnt<productTypeNL.getLength();ptCnt++){
Node productTypeNode = productTypeNL.item(ptCnt);
if(productTypeNode.hasChildNodes()){
ProductType productTypeBean = new ProductType();
Element productTypeElmt = (Element)productTypeNode;
productTypeBean.setmProductType(XMLfunctions.getValue(productTypeElmt,"producttype"));
System.out.println("Product Types are "+ " "+XMLfunctions.getValue(productTypeElmt,"producttype"));
ProductTypeAL.add(productTypeBean);
}
productBean.setmProductTypes(ProductTypeAL);
}
productAL.add(productBean);
}
}
subCategoryBean.setmProducts(productAL);
}
}
subCategoryAL.add(subCategoryBean);
}
I want to read a multi level tags from xml(DOM) using java and the sample xml is : <root>
<subclass>
<subclass>
<subclass>
<name>test1</name>
<address>address1</address>
</subclass>
<name>test2</name>
<address>address2</address>
</subclass>
<name>test3</name>
<address>address3</address>
</subclass>
</root>
How to read <name>test2</name> and <address>address2</address> from the above xml?
I have given a sample code .. but i need to find the values dynamically.
when i am iterating bu using subclass tag, it's giving all the data. just i want to know how to get the specific data like <name>test2</name> and <address>address2</address> .
Below is my java code which is reading the above xml:
NodeList fList = firstWordElement
.getElementsByTagName("root");
for (int i = 0; i < fList.getLength(); i++) {
Node firstFLNode = fList.item(i);
if (firstFLNode.getNodeType() == Node.ELEMENT_NODE) {
Element firstWdElement = (Element) firstFLNode;
NodeList firstWdList = firstWdElement.getElementsByTagName("innerclass");
for (int j = 0; j < firstWdList.getLength(); j++) {
Element firstWd1Element = (Element) firstWdList.item(j);
if (firstWd1Element.getNodeType() == Node.ELEMENT_NODE) {
String InnerName = ParseUtil.getTagValue("name", firstWd1Element);
String InnerFormat = ParseUtil.getTagValue("format", firstWd1Element);
String InnerDescription = ParseUtil.getTagValue("description", firstWd1Element);
NodeList innerClassList = firstWd1Element.getElementsByTagName("subclass");
for (int k = 0; k < innerClassList.getLength(); k++) {
Element subClassElement = (Element) innerClassList
.item(k);
if (subClassElement.getNodeType() == Node.ELEMENT_NODE) {
String InnerSubName = ParseUtil.getTagValue("name", subClassElement);
System.out.println("Innername==="+ InnerSubName);
String InnerSubFormat = ParseUtil.getTagValue("format", subClassElement);
System.out.println("Innerformat==="+ InnerSubFormat);
String InnerSubDescription = ParseUtil.getTagValue("description", subClassElement);
System.out.println("Innerdescription==="+ InnerSubDescription);
}
}
}
}
}
}
A quick way to do this is by using XPath queries. Check out these tutorials:
http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html
http://www.javabeat.net/2009/03/how-to-query-xml-using-xpath/
Traditionally you have to iterate trough the nodes, usually done by creating a NodeList with getChildren() from the parent node. If you only want the test2 -node you have to perform some sort of comparison - you can't jump straight to the second node. That is to say you can, but that wont make a very robust or scale able solution.