如何从基于节点的Xml文件中读取

时间:2016-10-06 08:44:31

标签: java xml

我有一个从xml文件中读取的java代码。 有三个不同的节点android,ios和web,我必须根据我指定的节点获取哈希映射的键中的所有值,如果我指定android那么来自android的所有键值应该存储在表中

我尝试使用代码但是发生了null异常,任何人都可以帮助我

我的代码

public HashMap<String, String> readXML (String elementName)
        throws ParserConfigurationException, SAXException, IOException {
    HashMap<String, String> locator = new HashMap<>();
    try {

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        File file = new File("XML.xml");
        Document doc = dBuilder.parse(file);
        NodeList nList = doc.getElementsByTagName("android");
        Element fileElementactivity=(Element)nList.item(0);
        NodeList nListactivity = fileElementactivity.getElementsByTagName("activity");
        Element fileElementactivityname=(Element)nListactivity.item(1);
        NodeList nListkeys = fileElementactivityname.getElementsByTagName("keys");
        Element fileElementkey=(Element)nListkeys.item(0);
        NodeList nListkey = fileElementkey.getElementsByTagName("key");


        System.out.println(nListkey);
        for (int j = 0; j < nListkey.getLength(); j++) {
            Node columnNode = nListkey.item(j);
            Element columnElement = (Element) columnNode;
            Node data = columnElement.getElementsByTagName("name").item(0);
            String name = "";
            if (data != null) {
                name = data.getTextContent();
                if (name.contentEquals(elementName)) {
                    data = columnElement.getElementsByTagName("type").item(0);
                    String type = data.getTextContent();
                    data = columnElement.getElementsByTagName("value").item(0);
                    String value = data.getTextContent();
                    System.out.println(name + "******" + type + " : " + value);
                    locator.put(type, value);
                }
            }
        }

        System.out.println(locator.size());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return locator;

}

和我的xml

<object> 
<android>
 <activity>
 <activity-name>UserProfileLoginActivity</activity-name>
 <keys>
     <key>
        <name>fake</name>
        <type>index</type> 
        <value>0</value>
     </key>
     <key>
         <name>fake</name>
         <type>name</type> 
        <value>signIn</value> 
    </key>

 </keys> 
</activity>
</android>

<ios>
 <activity>
 <activity-name>UserProfileLoginActivity</activity-name>
 <keys>
     <key>
        <name>fake</name>
        <type>index</type> 
        <value>0</value>
     </key>
     <key>
         <name>fake</name>
         <type>name</type> 
        <value>signIn</value> 
    </key>

 </keys> 
</activity>
</ios>

<web>
 <activity>
 <activity-name>UserProfileLoginActivity</activity-name>
 <keys>
     <key>
        <name>fake</name>
        <type>index</type> 
        <value>0</value>
     </key>
     <key>
         <name>fake</name>
         <type>name</type> 
        <value>signIn</value> 
    </key>

 </keys> 
</activity>
</web>
</object> 

1 个答案:

答案 0 :(得分:1)

如果你愿意放弃直接的org.w3c.dom。* masochism,也许Java 8库Dynamics可能会有所帮助。同样直接,但无效且有帮助。

XmlDynamic example = new XmlDynamic(xmlStringOrReaderOrInputSourceEtc);

String target = "android";
Map<String, String> keyTypeToValue = example.get("object")
    .get(target)
    .get("activity|keys")
    .children()
    .collect(toMap(key -> key.get("type").asString(), key -> key.get("value").asString()));
// {name=signIn, index=0}

很容易将其放入一种方法,以便为&#34; ios&#34;,&#34; web&#34;等