?
Solved

XML parsing in Java

Posted on 2014-01-12
6
Medium Priority
?
428 Views
Last Modified: 2014-01-14
How to parse XML below in Java using DOM Parser:
<employees>
  <employee id="111">
    <firstName>Rakesh</firstName>
    <lastName>Mishra</lastName>
    <location>Bangalore</location>
  </employee>
  <employee id="112">
    <firstName>John</firstName>
    <lastName>Davis</lastName>
    <location>Chennai</location>
  </employee>
  <employee id="113">
    <firstName>Rajesh</firstName>
    <lastName>Sharma</lastName>
    <location>Pune</location>
  </employee>
</employees>
0
Comment
Question by:Vakils
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 36

Expert Comment

by:mccarl
ID: 39775815
Where is this XML coming from? A file, a database, the web, or hardcoded in your Java program?  Assuming that this may be in a file, parsing is as simple as this...

DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document doc = documentBuilder.parse(new FileInputStream("my_xml_file.xml"));

// Do something useful with "doc"

Open in new window

0
 

Author Comment

by:Vakils
ID: 39778527
XML comes from file. Reading nodes value, is what I want. XML elements have no attributes at this stage and some elements may not any have value. For example while traversing XML tree, if an element has no value, I get null pointer exception in element.getTextContent(). Is there a elegant way to know that element has no value, rather than checking if element is null?

XML:
<contracts>
            <contract>123</contract> // OK
             <contract_year></contract_year>        //exception
</contracts>
//Iterating through the nodes and extracting the data.
    NodeList nodeList = document.getDocumentElement().getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {

      //We have encountered an <employee> tag.
      Node node = nodeList.item(i);
      if (node instanceof Element) {
        Employee emp = new Employee();
        emp.id = node.getAttributes().
            getNamedItem("id").getNodeValue();

        NodeList childNodes = node.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
          Node cNode = childNodes.item(j);

          //Identifying the child tag of employee encountered.
          if (cNode instanceof Element) {
          String content = cNode.getLastChild().getTextContent().trim();  // null pointer exception if element has no value
0
 

Author Comment

by:Vakils
ID: 39778530
XML:
<employee>
            <Name>SAM</Name> // OK
             <Middle></Middle>        //exception
</employee>
//Iterating through the nodes and extracting the data.
    NodeList nodeList = document.getDocumentElement().getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {

      //We have encountered an <employee> tag.
      Node node = nodeList.item(i);
      if (node instanceof Element) {
        Employee emp = new Employee();
        emp.id = node.getAttributes().
            getNamedItem("id").getNodeValue();

        NodeList childNodes = node.getChildNodes();
        for (int j = 0; j < childNodes.getLength(); j++) {
          Node cNode = childNodes.item(j);

          //Identifying the child tag of employee encountered.
          if (cNode instanceof Element) {
         String content = cNode.getLastChild().getTextContent().trim();  // null pointer exception if element has no value
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 39780987
Just remove the    .getLastChild()    call from your bold line of code, ie.
String content = cNode.getTextContent().trim();

Open in new window

It was the .getLastChild() method call that is returning a NULL for the empty element and so when you attempt to call  .getTextContent() on a NULL object you get the NullPointerException. Also, I'm pretty sure that .getTextContent() never returns a NULL (at least for Element nodes which are the only ones that you are processing) so calling .trim() on that should be safe.
0
 

Author Comment

by:Vakils
ID: 39781067
OK. Got it. Calling trim is OK. getLastChild is not needed.
Thanks!
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39781073
Not a problem, glad to help! :)
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question