rickygrimes
rickygrimes

Reputation: 2716

Find a String in xml file and assign its value to another String

I have the below plist.

    <?xml version="1.5" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//PSect//ACD PLIST 1.5//" "http://pset.com/ACD/plist.dtd">
<plist version="1.5">
<dict>
        <key>City</key>
        <string>Melbourne</string>
        <key>DetailedInfo</key>
        <dict>
                <key>Name</key>
                <real>Sam</real>
                <key>Income</key>
                <real>4000</real>
        </dict>
        <key>Status</key>
        <string>Single</string>
        <key>PIN</key>
        <string>123456789</string>
</dict>

I have the code to parse this plist into an xml file. What I need help with is to find the key City in the plist. I have looked at some posts to search for a string in an xml file, but haven't had much luck. Basically what I want to do is,

1. Check if my xml file has Key City
2. If it does, assign its value (Melbourne) to another String. 

Is there anyway I can achieve this? Please suggest.

Upvotes: 0

Views: 161

Answers (3)

pasha701
pasha701

Reputation: 7207

    XPath path = XPathFactory.newInstance().newXPath();
    NodeList nl = (NodeList) path.evaluate("//dict[key/text()='City']", doc, XPathConstants.NODESET);
    if (nl.getLength() == 1) {
        Element dictElement = (Element) nl.item(0);

        NodeList stringNodeList = dictElement.getElementsByTagName("string");
        for (int i = 0; i < stringNodeList.getLength(); i++) {
            // replace string here
            System.out.println("Replace: " + stringNodeList.item(i));
        }
    }

Upvotes: 1

Thirumalai Parthasarathi
Thirumalai Parthasarathi

Reputation: 4671

i am not sure of the doctype you have in your plist but try this try this

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class Demo {

    public static void main(String[] args) throws Exception {

        String keyVal = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(new File("input.xml"));
        NodeList keyList = document.getElementsByTagName("key");

        if(keyList !=null && keyList.getLength() > 0) {
            for(int i =0; i< keyList.getLength(); i++) {
                keyVal = keyList.item(i).getTextContent();
                if ("City".equals(keyVal)) {
                    NodeList stringList = document.getElementsByTagName("string");
                    if(stringList !=null && stringList.getLength() > 0) {
                        System.out.println(stringList.item(i).getTextContent());
                    }
                }
            }
        }
    }
}

Upvotes: 1

aryann
aryann

Reputation: 949

why don't you use object-xml binding framework like jaxb or xtream to do this. Those frameworks would create an object out of your xml and it will be very easy to navigate this xml then. for example you could do if("City".equals(getDict().getKey()) { then do this}

Upvotes: 0

Related Questions