Dheeraj Kumar
Dheeraj Kumar

Reputation: 13

nested xml parseing using Xerces DOMParser in java

I have string xml. i want to parse this xml to get the output as below.

Input

<?xml version="1.0" encoding="UTF-8"?>
<req:messages xmlns:req="http://hp.com/ecc/request/">
    <req:message category="GENERIC" type="error" number="6">Null pointer exception occured.</req:message>
    <req:message category="GENERIC" type="error" number="6">Arithmetic exception occured.</req:message>
    <req:message category="GENERIC" type="error" number="6">Class not found exception occured.</req:message>
</req:messages>

Output

Null pointer exception occured.
Arithmetic exception occured.
Class not found exception occured.

i have tried so far below

public static String getCharacterDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof  org.w3c.dom.CharacterData) {
          org.w3c.dom.CharacterData cd = ( org.w3c.dom.CharacterData) child;
          return cd.getData();
        }
        return "";
      }

    private String parseXmlData() throws ParserConfigurationException, SAXException, IOException {
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><req:messages xmlns:req=\"http://hp.com/ecc/request/\"><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Null pointer exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Arithmetic exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Class not found exception occured.</req:message></req:messages>";
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(str));

        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("req:messages");
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < nodes.getLength(); i++) {
          Element element = (Element) nodes.item(i);

          NodeList name = element.getElementsByTagName("req:message");
          Element line = (Element) name.item(0);
          sb.append(getCharacterDataFromElement(line) + System.lineSeparator());
        }
        return sb.toString();
    }

  public static void main(String args[]) {
    try{
        String data = parseXmlData();
        System.out.println(data);
       } catch(Exceprtion e) {
      }
  }

Using above program i am able to get only 1st error as below.

Null pointer exception occured.

Upvotes: 1

Views: 136

Answers (1)

Sree
Sree

Reputation: 374

You are iterating wrong node.. you have multiple 'req:message' tags, not 'req:messages'.. hope you understand.. please check this code

In this you can directly get 'req:message' elements and iterate it..

    private static String parseXmlData() throws ParserConfigurationException, SAXException, IOException {
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><req:messages xmlns:req=\"http://hp.com/ecc/request/\"><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Null pointer exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Arithmetic exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Class not found exception occured.</req:message></req:messages>";
        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(str));
        Document doc = db.parse(is);
        NodeList nodes = doc.getElementsByTagName("req:message");
        StringBuilder sb = new StringBuilder();        
        for (int i = 0; i < nodes.getLength(); i++) {
          Element element = (Element) nodes.item(i);
          sb.append(getCharacterDataFromElement(element) + System.lineSeparator());
        }
        return sb.toString();
    }

or If you want only 'req:message' elements under 'req:messages' tags, use this code...

private static String parseXmlData() throws ParserConfigurationException, SAXException, IOException {
    String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><req:messages xmlns:req=\"http://hp.com/ecc/request/\"><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Null pointer exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Arithmetic exception occured.</req:message><req:message category=\"GENERIC\" type=\"error\" number=\"6\">Class not found exception occured.</req:message></req:messages>";
    DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(str));
    Document doc = db.parse(is);
    NodeList nodes = doc.getElementsByTagName("req:messages");
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < nodes.getLength(); i++) {
      Element element = (Element) nodes.item(i);
      NodeList names = element.getElementsByTagName("req:message");
      for (int j = 0; j < names.getLength(); j++) {
          Element line = (Element) names.item(j);
          sb.append(getCharacterDataFromElement(line) + System.lineSeparator());
      }
    }
    return sb.toString();
}

Upvotes: 1

Related Questions