2014-09-05 15 views
0

編輯:我想我的問題不夠清楚。如何訪問我的xml文件中的義務字段並將其作爲java中的字符串返回?正如標題所述,我一直在爲這個XML解析問題爭取2天。我將給出我的XML和我嘗試過的代碼片段。無法在Java中找到我的XML字段

XML:

<aircraft name="Hawk"> 
    <AAgent/> 
    <capabilities file="hawk.xml"/> 
    <Obligations file="HawkO.xml"/> 
    <Restrictions file="HawkR.xml"/> 
    <Negotiation file="HawkN.xml"/> 
    </aircraft> 

的Java訪問義務的文件:

if (AssetElement.getAttribute("name").equalsIgnoreCase(assetName)) { // works 
    NodeList policies = AssetElement.getChildNodes(); 

    System.out.println("Policies nodes " + policies.toString()); 
    String oblig = AssetElement.getAttribute("Obligations file"); 
    System.out.println("Oblig = " + oblig); 
    NodeList nl = AssetElement.getChildNodes(); 
    Node node = nl.item(2); 
    System.out.println("1" + node.getNodeValue()); 
    System.out.println("2" + node.getNodeName()); 
    System.out.println("3" + node.getNodeValue()); 
    System.out.println("4" + node.getTagName()); 
    System.out.println("5" + node.getTextContent()); 
    System.out.println("Content" + AssetElement.getFirstChild().getNextSibling().getTextContent()); 
    System.out.println("Content 2" + AssetElement.getElementsByTagName("Obligations file").item(0)); 
    System.out.println("Name = " + AssetElement.getFirstChild().getNodeName()); 
    System.out.println("Value = " + AssetElement.getFirstChild().getNodeValue()); 

} 

正如你可以看到我打印語句後print語句解決此。迄今爲止,它們都沒有用處。這是我的輸出到所有這些打印語句:

1 

2#text 
3 

5 

Content 
Content2null 
Name = #text 
Value = 
+1

什麼是你的問題? – Dici 2014-09-05 00:03:07

+0

你面對什麼問題? – 2014-09-05 00:03:13

+0

如何訪問我的xml文件中的Obligations字段並將其作爲java中的字符串返回? – user1814946 2014-09-05 00:05:22

回答

0

只需使用

assetElement.getChild("Obligations").getAttributeValue("file"); 
相關問題