2012-08-16 92 views
1

我想解析XML文件,它的形式。在java中解析XML文件的元数据属性

<parent tag> 
    <child tag> 
     <element key="property1">value</element> 
     <element key="property2">value</element> 
    </child tag> 
</parent tag> 

我怎样才能有property1element标签的价值?我的代码如下。

public static ArrayList<String> parseXML(URL url_str,URLConnection conn_str,String root_tag,String child_tag) throws ParserConfigurationException, SAXException, IOException 
{ 
    String s = null; 
    ArrayList <String> List_value=new ArrayList<String>();  
    DocumentBuilderFactory dbF = DocumentBuilderFactory.newInstance(); 
    DocumentBuilder dBuilder = dbF.newDocumentBuilder(); 
    Document doc = dBuilder.parse(conn_str.getInputStream()); 
    doc.getDocumentElement().normalize(); 
    System.out.println("Root : "+doc.getDocumentElement()); 
    System.out.println("****************"); 
    NodeList nList= doc.getElementsByTagName(root_tag); 
    System.out.println("****************"); 

    for (int i = 0; i < nList.getLength(); i++) { 
     Node node = nList.item(i); 
     if (node.getNodeType() == Node.ELEMENT_NODE) { 
      Element element = (Element) node; 
      NodeList nodelist1 = element.getElementsByTagName(child_tag); 
      for (int i1 = 0; i1 < nodelist1.getLength(); i1++) 
      { 
       Element element1 = (Element) nodelist1.item(i1); 
       NodeList fstNm = element1.getChildNodes(); 
       s=fstNm.item(0).getNodeValue(); 

       List_value.add(s); 
      } 
      for(int c=0;c<List_value.size();c++) 
      { 
        System.out.println(List_value.get(c)); 
      } 

     } 

    } 
    return List_value; 
} 

我正在使用DOM解析器。请帮助。

谢谢。

+0

请提供您试图解析的xml。 – 2012-08-16 19:43:10

+0

XML文件格式是我想解析XML格式的文件。 <元件键= “property1”>值 <元件键= “property2”>值 我怎样才能获得具有元素标签property1的值? 我正在使用DOM解析器。 请帮忙。 谢谢。 – Soham 2012-08-16 19:43:26

+0

剪贴板有问题,我输入时没有发布问题。 XML的 – Soham 2012-08-16 19:44:13

回答

3

这在这里(DOM + XPath的)做的工作,更多的文档在这里:http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html

下面是有关XPath的一个很好的解释和它如何工作:http://www.xmlplease.com/axis

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.xpath.XPath; 
import javax.xml.xpath.XPathConstants; 
import javax.xml.xpath.XPathExpression; 
import javax.xml.xpath.XPathFactory; 

import org.w3c.dom.Document; 
import org.w3c.dom.NodeList; 


public class XmlParseTest { 
    public static void main(String[] args) throws Exception { 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     factory.setNamespaceAware(true); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     Document doc = builder.parse("test.xml"); 

     XPathFactory xpathFactory = XPathFactory.newInstance(); 
     XPath xpath = xpathFactory.newXPath(); 
     XPathExpression expr = xpath.compile("//element[@key='property1']/text()"); 
     Object result = expr.evaluate(doc, XPathConstants.NODESET); 

     NodeList nodes = (NodeList) result; 
     for (int i = 0; i < nodes.getLength(); i++) { 
      System.out.println(nodes.item(i).getNodeValue()); 
     } 
    } 
}  
+0

这正是我正在寻找..谢谢.. :) – Soham 2012-08-16 20:03:41

+0

哪个jar文件将不得不包括在classpath中以实现XPath? – Soham 2012-08-16 20:21:59

+1

无,其标准Oracle/Sun JDK的一部分,请记住添加进口:) – 2012-08-16 20:23:50

1

"An Introduction to APIs for XML Processing"将是一个好点开始学习XML解析Java中

+0

我写的代码只返回子标记的值。但我不知道如何获得属性的标签。 – Soham 2012-08-16 19:53:20

+0

@Soham:张贴代码,让我们看看:) – Sujay 2012-08-16 19:53:49

+0

我的代码不适合在这里.. :( – Soham 2012-08-16 19:54:33