2011-01-21 51 views
4

我開始解析XML文檔,並有問題:如何在Java上獲取特定的XML元素參數值?如何獲取特定的XML元素參數值?

XML文檔:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<data> 
<keyword name="text123"> 
    <profile num="1"> 
     <url>http://www.a.com</url> 
     <field-1 param="">text</field-1> 
     <filed-2 param="">text</field-2> 
    </profile> 
    <profile num="2"> 
     <url>http://www.b.com</url> 
     <field-1 param="">text</field-1> 
     <filed-2 param="">text</field-2> 
    </profile> 
</keyword> 
<keyword name="textabc123"> 
    <profile num="1"> 
     <url>http://www.1a.com</url> 
     <field-1 param="">text</field-1> 
     <filed-2 param="">text</field-2> 
    </profile> 
    <profile num="2"> 
     <url>http://www.1b.com</url> 
     <field-1 param="">text</field-1> 
     <filed-2 param="">text</field-2> 
    </profile> 
</keyword> 
</data> 

代碼我寫的Java:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = factory.newDocumentBuilder(); 
File xml_file=new File("file.xml"); 
if (xml_file.isFile() && xml_file.canRead()) { 
Document doc = builder.parse(xml_file); 
Element root = doc.getDocumentElement(); 
NodeList nodel = root.getChildNodes(); 
for (int a = 0; a < nodel.getLength(); a++) { 
    String data = /* code i don't know to write*/ 
    System.out.println(data); 
} 
} else {} 

我想出去控制檯元素 「關鍵字」 參數 「名稱」 值:

text123

text123abc

請幫忙,謝謝。

回答

4
Node node = nodel.item(a); 
if(node instanceof Element) { 
    data = ((Element)node).getAttribute("name"); 
    System.out.println(data); 
} 
+0

添加代碼後,我得到錯誤:錯誤:com.sun.org.apache.xerces.internal.dom.DeferredTextImpl無法轉換爲org.w3c.dom.Element該怎麼辦? – user488963 2011-01-21 12:41:22

+0

與我的片段它是不可能獲得這個例外,因爲有一個與instanceof – lweller 2011-01-21 12:44:07

+0

@Iweller檢查感謝您的答案,我從這裏看起來很長一段時間+1 – Luffy 2016-10-14 05:13:49

0

節點列表包含節點,它實際上是子接口(元素,文本等)的實例。

此代碼應該這樣工作:

Node node = nodel.item(a); 
if (node instanceof Element) { 
    Element e = (Element) node; 
    System.out.println(e.getAttribute("name"); 
} 
5

您可以使用XPath

InputStream is = getClass().getResourceAsStream("somefile.xml"); 
DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder docBuilder = xmlFactory.newDocumentBuilder(); 
Document xmlDoc = docBuilder.parse(is); 
XPathFactory xpathFact = XPathFactory.newInstance(); 
XPath xpath = xpathFact.newXPath(); 

String text123 = (String) xpath.evaluate("/data/keyword[1]/@name", xmlDoc, XPathConstants.STRING); 
String textabc123 = (String) xpath.evaluate("/data/keyword[2]/@name", xmlDoc, XPathConstants.STRING); 
5

我會指導你如何使用JAXB做到這一點。

首先,您的XML格式不正確。你幾乎沒有filed而不是field

正確的XML:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<data> 
<keyword name="text123"> 
    <profile num="1"> 
     <url>http://www.a.com</url> 
     <field-1 param="">text</field-1> 
     <field-2 param="">text</field-2> 
    </profile> 
    <profile num="2"> 
     <url>http://www.b.com</url> 
     <field-1 param="">text</field-1> 
     <field-2 param="">text</field-2> 
    </profile> 
</keyword> 
<keyword name="textabc123"> 
    <profile num="1"> 
     <url>http://www.1a.com</url> 
     <field-1 param="">text</field-1> 
     <field-2 param="">text</field-2> 
    </profile> 
    <profile num="2"> 
     <url>http://www.1b.com</url> 
     <field-1 param="">text</field-1> 
     <field-2 param="">text</field-2> 
    </profile> 
</keyword> 
</data> 

接下來,進入this website和下載莊。

假設您的XML文件名爲sample.xml,請使用java -jar trang.jar sample.xml sample.xsd通過Trang運行它以獲取xml文件的xsd模式。

現在運行xjc sample.xsd(xjc是一個爲XML模式生成Java類的工具,它與Java 6 SDK捆綁在一起)。

你會得到Java類的列表:

 
generated\Data.java 
generated\Field1.java 
generated\Field2.java 
generated\Keyword.java 
generated\ObjectFactory.java 
generated\Profile.java 

放在你的Java項目文件,並把sample.xml你的程序可以找到它。現在,你這是怎麼弄的關鍵字名:

JAXBContext context = JAXBContext.newInstance(Data.class); 
Data data = (Data)context.createUnmarshaller().unmarshal(new File("sample.xml")); 

List<Keyword> keywords = data.getKeyword(); 

for (Keyword keyword : keywords) { 
    System.out.println(keyword.getName()); 
} 

這種方法似乎在開始時有點凌亂,但如果你的XML結構不改變,我覺得更好的處理類型的Java對象比使用DOM本身。