Link to home
Start Free TrialLog in
Avatar of Vakils
VakilsFlag for United States of America

asked on

XML parsing in Java

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>
Avatar of mccarl
mccarl
Flag of Australia image

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

Avatar of Vakils

ASKER

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
Avatar of Vakils

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Vakils

ASKER

OK. Got it. Calling trim is OK. getLastChild is not needed.
Thanks!
Not a problem, glad to help! :)