java - 在java中解析XML文件的元数据属性

标签 java xml dom

我正在尝试解析具有以下形式的 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 解析器。请帮忙。

谢谢。

最佳答案

这在这里完成了工作(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()); 
        }
    }
}    

关于java - 在java中解析XML文件的元数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11994277/

相关文章:

java - 尝试通过 SOAP 网络服务创建或访问用户对象时,PermissionChecker 未初始化 Liferay 异常

java - 从具有不同依赖项的单个源文件夹中 Gradle 多个 jar

java - 无法将 ascii 121 打印到字符 y,而是打印特殊字符

python - 使用 python 的 lxml 剥离内联标签

javascript - 使用 Jquery 循环 tr 并将内容与系列中的下一行进行比较

java - 在另一个程序中调用 IMAGEJ 插件

xml - 从XML节点提取未知值

sql-server - XML 解析错误 : why is semicolon expected?

javascript - 如何使用 Jquery 向下滑动添加到 DOM 之前隐藏的元素?

javascript - 检测 DOM 对象是否可以放置在 SPAN 中