parse this xml file in java

1k views Asked by At

How to parse this xml file in java to read all the attributes of this XML file in java?

<?xml version="1.0"  encoding="UTF-8"?
<dsml:batchRequest xmlns:dsml="urn:oasis:names:tc:DSML:2:0:core"                  
  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-   instance">


    <dsml:addRequest  dn="cn=stifford,ou=Users,o=data">

  <attr name="objectclass"><value>top</value></attr>
  <attr name="objectclass"><value>person</value></attr>
  <attr name="objectclass"><value>organizationalPerson</value></attr>
  <attr name="objectclass"><value>inetorgperson</value></attr>
  <attr name="UID"><value>kevin985</value></attr>
  <attr name="sn"><value>kevin</value></attr>
  <attr name="givenName"><value>stifford</value></attr>
   <attr name="telephoneNumber"><value>9852898994</value></attr>
  <attr name="mail"><value>[email protected]</value></attr>

3

There are 3 answers

9
Code On
public static void main(String[] args) throws MarshalException,
ValidationException, ParserConfigurationException, SAXException,
IOException {

    File fXmlFile = new File(
            "/home/Parsing/src/com/parsing/test.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);

    doc.getDocumentElement().normalize();

    Node node = doc.getDocumentElement().getParentNode();

    System.out.println(doc.getDocumentElement().getNodeName());

    NodeList itemList = node.getChildNodes();
    for (int i = 0; i < itemList.getLength(); i++) {

        Node nNode = itemList.item(i);

        System.out.println("Current Element : " + nNode.getNodeName());

        getChildNode(nNode.getChildNodes());

    }
}

private static void getChildNode(NodeList childNodes) {   // This method is going to retrieve the child nodes
    // TODO Auto-generated method stub
    System.out.println(childNodes.getLength());

    for (int i = 1; i < childNodes.getLength(); i++) {
        Node cNode = childNodes.item(i);

        System.out.println(cNode.getNodeName());
        /**
         * This will get the attribute of the node 
         */
        if (cNode.hasAttributes()) {

            NamedNodeMap nodeMap = cNode.getAttributes();
            for (int f = 0; f < nodeMap.getLength(); f++) {
                System.out.println("Att " + nodeMap.item(f).getNodeName()
                        + " " + nodeMap.item(f).getNodeValue());
            }
        }

        if (cNode.hasChildNodes()) {

            // For getting the value if node has more than 2 or atleast two childs

            if (cNode.getChildNodes().getLength() >= 2) {

                getChildNode(cNode.getChildNodes());
            }

            // For getting the node has no childs and it contains text node value

            else if (cNode.getNodeType() == cNode.ELEMENT_NODE) {

                Element ele = (Element) cNode;
                System.out.println("\t" + ele.getTextContent());

            }
        }

        i++;
    }
}
0
Eugene On

The answer is specific for a parser type. If you are going to use DOM parser, probably this will be helpful. These are examples for the SAX parser.

3
Theolodis On

One way to go would be to do it manually, by evaluating the strings. Another would be to use one of all the available Java XML libraries... http://en.wikipedia.org/wiki/Java_API_for_XML_Processing