简体   繁体   中英

How To read from Xml files based on nodes

I have a java code to read from a xml file. there is three different nodes android ,ios and web and i have to get all values in the key to a hash map based on node i have specified,if i specify android then all the key values from the android should store in has table

i tried with a code but null exception is occurring can any one help me on this

My code

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;

}

and my 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> 

If you are willing to step away from direct org.w3c.dom.* masochism, perhaps the Java 8 library Dynamics may be of help. Similarly direct, but null-safe and helpful.

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}

It is simple to put this in a method to work for "ios", "web" etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM