1

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>sample@bsample.com</value></attr>

vimuth
  • 5,064
  • 33
  • 79
  • 116
DURGA
  • 63
  • 1
  • 1
  • 10

3 Answers3

1
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++;
    }
}
Navoneel Talukdar
  • 4,393
  • 5
  • 21
  • 42
Code
  • 184
  • 1
  • 8
  • Thanks for help. Could you please let me know if I have to parse only below two attribute form the xml and assign into a variable: kevin stifford – DURGA May 15 '13 at 06:23
  • can you help in this its very important for me from the two values have to generate dynamic username and password – DURGA May 15 '13 at 06:51
  • its working fine when i am running on my machine kevin stifford here "class" is the root tag – Code May 15 '13 at 07:30
  • i want to parse only these two values.i dont want to parse rest of the values – DURGA May 15 '13 at 07:39
  • In that case you can take the node by the nodeName and then later you can retrieve its child element and get the attribute value – Code May 15 '13 at 13:22
  • ok thanks you i need one help from you.i have a written a program that serach entries in eDirectory but i want to generate UID based upon the firstname and sn .and if the uid is present it must incriment to 1.ex:sacbad is present it must incriment to sacbad1. – DURGA May 16 '13 at 05:32
  • http://stackoverflow.com/questions/16564672/assign-unique-uid-value-dynamically-based-upon-3-character-of-sn-and-givenname pls check this link i mentioned the programme – DURGA May 16 '13 at 05:36
  • hi buddy i can parse the xml and get UID – DURGA May 17 '13 at 07:24
  • how to call a class getunique id – DURGA May 17 '13 at 07:42
0

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

Theolodis
  • 4,977
  • 3
  • 34
  • 53
  • i hv tried but this xml is in different format so while parsing it is not accepting pls help – DURGA May 06 '13 at 09:22
  • it is showing batch request but cant able to read all the values.because it is a dsml form of xml which the attribute must match the edirectory and store value in that' – DURGA May 06 '13 at 09:26
  • In android you can parse xml in two ways. DOM or SAX, just google it. – Yahya Arshad May 06 '13 at 09:39
0

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.

Eugene
  • 520
  • 3
  • 8