【问题标题】:parse this xml file in java [duplicate]在java中解析这个xml文件[重复]
【发布时间】:2013-04-30 00:29:29
【问题描述】:

如何在java中解析这个xml文件来读取这个xml文件在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>

【问题讨论】:

标签: java xml parsing dsml


【解决方案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++;
    }
}

【讨论】:

  • 非常感谢它帮了很多忙
  • 感谢您的帮助。如果我只需要解析 xml 中的两个属性并分配给一个变量,请告诉我:kevinstifford
  • 你能帮忙吗?这对我来说非常重要,因为这两个值必须生成动态用户名和密码
  • 当我在我的机器上运行时它工作正常 kevinstifford 这里的“class”是根标签
  • 我只想解析这两个值。我不想解析其余的值
【解决方案2】:

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

【讨论】:

  • 我试过了,但是这个 xml 格式不同,所以在解析它时不接受请帮助
  • 它正在显示批处理请求,但无法读取所有值。因为它是 xml 的 dsml 形式,其属性必须与 edirectory 匹配并在其中存储值'
  • 在android中你可以通过两种方式解析xml。 DOM 或 SAX,谷歌一下。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 2012-08-26
    • 2012-12-25
    • 2011-03-10
    • 1970-01-01
    • 2010-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多