在java中解析这个xml文件

时间:2013-05-06 09:17:13

标签: java xml parsing dsml

如何在java中解析这个xml文件,以便在java中读取这个XML文件的所有属性?

<?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>

3 个答案:

答案 0 :(得分: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++;
    }
}

答案 1 :(得分:0)

一种方法是通过评估字符串手动完成。 另一种方法是使用所有可用的Java XML库中的一个...... http://en.wikipedia.org/wiki/Java_API_for_XML_Processing

答案 2 :(得分:0)

答案特定于解析器类型。如果您打算使用DOM解析器,那么this可能会有所帮助。对于SAX解析器,这些是examples