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

4 回答 4

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 是从变量中的输入流加载的。如果您已经将 XML 作为字符串,则可以将其转换为如下流:

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

您还可以从 url 加载 XML 目录:

Document doc = docBuilder.parse(url);

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

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
于 2013-07-01T13:40:21.410 回答
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"));
        }
}

参考

于 2013-07-01T13:25:00.947 回答
1

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

对于一个好的 XML ParserJAXB来说,像这样(未经测试):

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

然后根据需要编组和解组。

于 2013-07-01T12:53:25.980 回答
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 文件中可用的所有文件名

于 2013-07-01T13:49:27.553 回答