如何使用Java XML解析器从XML获取节点属性值?

时间:2013-07-01 12:40:47

标签: java xml

<Files>
    <File Name="D:/temp/OpId_63_7b126c8d-f90a-402b-9902-786c7995314f/35f9cdf8-f6cc-4c9d-b0e5-cc21c1842765" />
    <File Name="D:/temp/PPPPOpId_63_7b126c8d-f90a-402b-9902-786c7995314f/35f9cdf8-f6cc-4c9d-b0e5-cc21c1842765" />
</Files>

从上面的XML我想要两个这样的文件名:

D:/temp/OpId_63_7b126c8d-f90a-402b-9902-786c7995314f/35f9cdf8-f6cc-4c9d-b0e5-cc21c1842765
D:/temp/PPPPOpId_63_7b126c8d-f90a-402b-9902-786c7995314f/35f9cdf8-f6cc-4c9d-b0e5-cc21c1842765

4 个答案:

答案 0 :(得分:5)

使用javax,您可以通过xpath查询提取数据,如下所示:

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(stream);

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();

String name1 = (String)xpath.evaluate("/Files/File[1]/@Name", doc, XPathConstants.STRING);
String name2 = (String)xpath.evaluate("/Files/File[2]/@Name", doc, XPathConstants.STRING);

这假设您的XML是从 stream 变量中的输入流加载的。如果您已将XML作为字符串,则可以将其转换为如下所示的流:

InputStream stream = new ByteArrayInputStream(xmlstring.getBytes("UTF-8"));

您还可以从以下网址加载XML目录:

Document doc = docBuilder.parse(url);

请注意,您至少需要这些导入:

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;

答案 1 :(得分:3)

使用DOM XML解析器

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

然后,

File filesXML = new File("/files.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(filesXML);


NodeList nList = doc.getElementsByTagName("File");

for (int i= 0; i< nList.getLength(); i++) {

    Node nNode = nList.item(i);

    if (nNode.getNodeType() == Node.ELEMENT_NODE) {



        Element eElement = (Element) nNode;

            System.out.println("File: " + eElement.getAttribute("Name"));
        }
}

Reference

答案 2 :(得分:1)

如果您只是在做那些简单的事情,请查看任何Java XML解析参考。

对于一个好的XML解析器,使用JAXB这样的东西(未经测试):

@XmlRootElement(name="Files")
public class FilesXML {
    @XmlElementWrapper(name="File")
    @XmlAttribute(name="Name")
    private String filename;
}

然后你可以随意编组和解组。

答案 3 :(得分:0)

   DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
   DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder();
   Document document = docbuilder.parse(fileName);
   String xpath = "//File";
   NodeList testConfig = org.apache.xpath.XPathAPI.selectNodeList(document, xpath);
   count = testConfig.getLength();
   String fileNames[] = new String[count];
   int rows = 0;
   while (rows < count) {
     Node row = testConfig.item(rows);
     if (row.getNodeType() == Node.ELEMENT_NODE) {
        AttributeMap map = (AttributeMap) row.getAttributes();
        int attrCount = map.getLength();
        int j = 0;
        Properties props = new Properties();
        while (j < attrCount) {
             props.put(map.item(j).getNodeName(),map.item(j).getNodeValue().trim());
             j++;
        }
        fileNames[rows] = props.getProperty("Name"); // Maniuplate The props object
     }
    rows++;
  }

根据上面的代码,数组对象fileNames具有xml文件中可用的所有文件名