2013-05-06 86 views
0

如何解析java中的这个xml文件.to在java中读取这个xml文件的所有属性?在java中解析这个xml文件

的xmlns:XSD = “http://www.w3.org/2001/XMLSchema” 的xmlns:的xsi = “http://www.w3.org/2001/XMLSchema-实例”>

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

+0

您是否搜索过任何东西? – 2013-05-06 09:18:30

+0

只是检查此链接http://www.mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/ – Srinivas 2013-05-06 09:20:21

回答

0

一条路可走会做手工,通过评估的字符串。 另一种方法是使用所有可用的Java XML库之一... http://en.wikipedia.org/wiki/Java_API_for_XML_Processing

+0

我高中试过,但这个XML是在不同的格式,所以在解析它时不接受请求帮助 – DURGA 2013-05-06 09:22:19

+0

它显示批处理请求,但无法读取所有值。因为它是一个XML的dsml形式,其属性必须与该' – DURGA 2013-05-06 09:26:03

+0

中的编辑和存储值匹配在Android中,您可以在两个解析XML中方法。 DOM或SAX,只是谷歌它。 – 2013-05-06 09:39:46

0

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

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++; 
    } 
} 
+0

非常感谢你帮助了很多 – DURGA 2013-05-07 06:31:53

+0

我的快乐朋友 – Code 2013-05-07 11:40:18

+0

感谢您的帮助。能否请你让我知道如果我要解析只有下面这两个属性形成XML并分配到一个变量: 凯文 斯蒂福​​德 DURGA 2013-05-15 06:23:55