1

我正在尝试解析格式为的 XML 文件。

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

如何获得element标签的价值property1?我的代码如下。

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 解析器。请帮忙。

谢谢你。

4

2 回答 2

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()); 
        }
    }
}    
于 2012-08-16T19:56:01.957 回答
1

“An Introduction to APIs for XML Processing”是开始学习 Java 中 XML 解析的好起点

于 2012-08-16T19:44:33.863 回答