• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Regarding xml parsing using dom

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cheedella_koti@yahoo.com hi,
i am facing a problem in parsing xml file in java by using DOM parser.
my input xml file is:
<?xml version="1.0" ?>
<links>
<link1>
<name>abc</name>
<val>123</val>
</link1>
<link2>
<name>xyz</name>
<val>456</val>
</link2>
</links>

and java prog is:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class xmlparse {
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
DocumentBuilder dlb = fac.newDocumentBuilder();
System.out.println(dlb + " At creation");
Document doc = dlb.parse(new File("c:\\xmlexp.xml"));
System.out.println(doc +" At Parsing");
Node root = doc.getDocumentElement();
System.out.println(root.getNodeName() + " Root element");
NodeList childs = root.getChildNodes();
System.out.println(childs.getLength()+" No of childs");
for (int i=0; i<childs.getLength();i++)
{
Node first = childs.item(i);
System.out.println(first.getNodeName() + " Node element");
Node firstchild = first.getFirstChild();
String firsttext = first.getFirstChild().getNodeValue();
System.out.println(firsttext +" firstchild value");
Node lastchild = first.getLastChild();
String lasttext = lastchild.getFirstChild().getNodeValue();
System.out.println(lasttext +" lastchild value");
}
}
}


and the output i got is:

org.apache.crimson.jaxp.DocumentBuilderImpl@fabe9 At creation
org.apache.crimson.tree.XmlDocument@1888759 At Parsing
links Root element
5 No of childs
#text Node element
java.lang.NullPointerException
at xmlparse.main(xmlparse.java:27)
Exception in thread "main"

in my xml file the root tag (<links>) contains 2 child tags (<link1>,<link2>)
but the output of my java prog is showing 5 child tags. please tell me why my java prog is behaving like this and provide me the solution.
 
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The actual DOM structure of your TOP element <links> is
#text
Node (link1)
#text
Node (link2)
#text

See the Text element API to understand the DOM structure.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a VERY handy table in org.w3c.dom.Node JavaDocs that shows what every kind of Node responds with for the various methods.
There you will see that many types of Node have a nodeValue of null, including Element nodes.
Bill
 
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic