-1
    Above one is the XML file. I need to get the value for the Google analytics from the above xml file

I need to get the output as from the above xml. I have tried the following code but it's not showing any output Can you help me on this

Above one is the XML file. I need to get the value for the Google analytics from the above xml file

public class Xml {
    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(new FileInputStream(new File("C:\\Users\\VAMSIKRISHNA\\Desktop\\Apache Poi\\appConfig.plist")));

        NodeList nodeList=document.getElementsByTagName("key");
        NodeList nodeList1=document.getElementsByTagName("string");

        for (int i=0; i < nodeList.getLength(); i++) {
            for(int j=0;j<nodeList1.getLength();j++) {
                Node node = nodeList.item(i);
                Node node1=nodeList1.item(j);
                if (node.getNodeName()=="GA") {
                    System.out.println("Nodename"+node1.getNodeName()+" : "+node1.getFirstChild().getTextContent().trim());
                }
            }
        }
    }
}
hii
  • 1
  • 4

4 Answers4

0

You need to change the line

if (node.getNodeName()=="GA") {

to

if (node.getTextContent().equals("GA")) {

There were two problems:

  1. you compare the name, when you want to compare the content

  2. you compare a String with ==, when Strings should be compared with .equals()

You can also simplify some thinsg (just one for loop)

public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new FileInputStream(new File("C:\\Users\\VAMSIKRISHNA\\Desktop\\Apache Poi\\appConfig.plist")));

    NodeList nodeList=document.getElementsByTagName("key");
    NodeList nodeList1=document.getElementsByTagName("string");

    for (int i=0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        Node node1 = nodeList1.item(i);
        if (node.getTextContent().startsWith("GA")) {
            String textOfChild = "";
            if(node1.getFirstChild() != null && node1.getFirstChild().getTextContent() != null) {
                textOfChild = node1.getFirstChild().getTextContent().trim();
            }
            System.out.println("Nodename"+node1.getNodeName()+" : "+ textOfChild);
        }
    }
}

Caveat: This does assume that you have the same number of key and string elements.

JensS
  • 1,151
  • 2
  • 13
  • 20
  • Thank you so much for your prompt response. I have reedited the code with ur changes. But its showing null pointer exception on the following line System.out.println(node1.getFirstChild().getTextContent()); – hii Sep 21 '17 at 09:05
  • Glad I could help; you'll need to add a null check: if(node1.getFirstChild() != null) – JensS Sep 21 '17 at 09:10
  • I have added the null check to the code. – JensS Sep 21 '17 at 09:16
  • I have tried with your code But its showing the string value for the all elements. I have edited my xml file in above . Can you please check that once. I need the value specifically for google analystics – hii Sep 21 '17 at 09:27
  • I'm still not getting the proper values after implementing your code. For ex if (node.getTextContent().equals("GAAnalytics")) I'm getting some other value – – hii Sep 22 '17 at 04:40
  • Can you help me – hii Sep 22 '17 at 07:49
  • Which output are you getting? – JensS Sep 22 '17 at 08:38
  • Nodenamestring : /4177/UCLA_iPhone Nodenamestring : button – hii Sep 22 '17 at 08:41
  • This value is not in the XML you have posted. Can you please post the complete XML file. – JensS Sep 22 '17 at 08:48
  • I did . check the edited xml – hii Sep 22 '17 at 08:49
  • The value /4177/UCLA_iPhone is not in the XML; is it possible that you parse a different xml file? – JensS Sep 22 '17 at 10:09
  • i have changed to UCLA to some other value in the xml file – hii Sep 22 '17 at 16:00
  • @JensS- I have changed to UCLA to some other value in the xml file – hii Sep 23 '17 at 09:09
0

Your example XML does not match to your explanation and it is not valid xml.

<type>prod</type>
<dict></dict>
<key>GA</key>
<string>UA-111111-24</string>

You should go through all config elements of your list and then print the names and textcontent of the subelements.

Juliane
  • 57
  • 12
0

try node.getTextContent().equals("GA")

Please refer : https://www.javatpoint.com/string-comparison-in-java

Miraj Hamid
  • 554
  • 10
  • 19
0

EDIT: You first should look for the wrapping "dict" and then search the childs of this.

    NodeList dictList = document.getElementsByTagName("dict");
    for (int j = 0; j < dictList.getLength(); j++) {
        if (dictList.item(j) instanceof Element) {
            Element dict = (Element) dictList.item(j);

            NodeList nodeList = dict.getElementsByTagName("key");
            NodeList nodeList1 = dict.getElementsByTagName("string");
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);

                if (node.getTextContent().equals("GAAnalyticsKey")) {
                    Node node1 = nodeList1.item(i);
                    System.out.println("Nodename" + node.getNodeName() + " : "
                            + node.getTextContent().trim() + " " + node1.getNodeName() + " : "
                            + node1.getTextContent().trim());
                }
            }
        }
    }
Juliane
  • 57
  • 12
  • I'm not getting the proper values after implementing your code. For ex if (node.getTextContent().equals("GAAnalytics")) I'm getting some other value – hii Sep 21 '17 at 09:37
  • There are more "key" entries than "string" entries in your xml, so my first approach can not work. Try the last one. – Juliane Sep 21 '17 at 09:42
  • I have tried with the following code. But iam not getting any output NodeList nodeList = document.getElementsByTagName("key"); NodeList nodeList1 = document.getElementsByTagName("string"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getTextContent().equals("GAAnalyticsKey")) { Node node1 = node.getNextSibling(); System.out.println( node1.getTextContent()); } }} – hii Sep 21 '17 at 09:51
  • I'm still not getting the proper values after implementing your code. For ex if (node.getTextContent().equals("GAAnalytics")) I'm getting some other value – hii Sep 21 '17 at 18:36
  • Which value did you get? With the given data my code worked for me. The problem with the code of JensS is that you have more Keys than strings. Do you understand the differences between your original code, JensS and my Code? – Juliane Sep 22 '17 at 20:47
  • I have edited the xml file and added more keys and strings . Can you check the code now. – hii Sep 22 '17 at 22:02