2013-07-01 120 views
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我想兩個文件名是這樣的:如何使用Java XML解析器從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 
+1

也許'xpath'? –

回答

1

如果你只是在做簡單的東西,看看在任何Java XML解析參考。

對於一個良好的XML解析器採取JAXB像這樣的東西(未經測試):

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

然後編組和解組,只要你喜歡。

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

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.*; 
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++; 
    } 

按照上面的代碼中,陣列對象,文件名具有所有的文件名利用能夠在xml文件中