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

我需要從上面的xml中獲取輸出。我試過下面的代碼,但它沒有顯示任何輸出 你能幫我嗎如何獲取以下xml代碼的文本內容

上面是一個XML文件。我需要從上面的XML的谷歌分析的值文件

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()); 
       } 
      } 
     } 
    } 
} 

回答

0

您需要更改線路

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

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

有兩個問題:

  1. 你比較吶我,當你想要的內容

  2. 比較你比較==,字符串時字符串應該.equals()

您也可以簡化一些thinsg(只是一個for循環進行比較)

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); 
     } 
    } 
} 

警告:這確實假定您具有相同數量的鍵和字符串元素。

+0

非常感謝您的及時回覆。我已經用你的改變重新編碼了。但它在下面一行顯示空指針異常System.out.println(node1.getFirstChild()。getTextContent()); – hii

+0

很高興我能幫上忙;您需要添加一個空檢查:if(node1.getFirstChild()!= null) – JensS

+0

我已經將空檢查添加到代碼中。 – JensS

0

您的示例XML與您的解釋不匹配,它不是有效的xml。

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

您應該瀏覽列表中的所有配置元素,然後打印子元素的名稱和文本內容。

+0

中的某些其他值更改爲UCLA,即我的xml文件中的示例行。我需要將GA值作爲UA-111111-24。我無法得到。你能幫我嗎 – hii

+0

你能發佈整個XML文件嗎? – Juliane

+0

你能幫我嗎 – hii

0

編輯:您首先應該認準包裝 「字典」,然後搜索這個孩子的。

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()); 
       } 
      } 
     } 
    } 
+0

在執行你的代碼後,我沒有得到正確的值。對於如果(節點.getTextContent()。等於(「GAAnalytics」))我得到一些其他的價值 – hii

+0

有更多的「鍵」條目比你的XML中的「字符串」條目,所以我的第一種方法不能工作。試試最後一個。 – Juliane

+0

我用以下代碼嘗試過。但是iam沒有得到任何輸出NodeList nodeList = document.getElementsByTagName(「key」); NodeList nodeList1 = document.getElementsByTagName(「string」);對於(int i = 0; i hii