2017-09-21 16 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ファイルです。

:私は2つの問題がありましたあなたは

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

にライン

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

を変更する必要が

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

上記のXMLファイルからのGoogle Analyticsの値を取得する必要があります
  1. あなたはnaを比較しますあなたがコンテンツ

  2. を比較したいとき文字列は、あなたはまた、ループのために、いくつかのthinsg(一つだけを簡素化することができます

.equals()と比較されなければならないときに私は、あなたは==、との文字列を比較します)

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

ご連絡いただきありがとうございます。私はurの変更でコードを再編集しました。しかし、次の行にnullポインタ例外を表示しています。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

は、私のxmlファイルからちょうどサンプルの行thatsです。私はGAの値をUA-111111-24として取得する必要があります。私はそれを得ることができませんでした。あなたは私を助けることができます – hii

+0

全体のXMLファイルを投稿できますか? – Juliane

+0

あなたは私を助けることができます – hii

0

EDIT:あなたが最初のラップ "辞書" を探して、その後、こののチャイルズを検索する必要があります。

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

コードを実装した後に適切な値が得られません。 (node.getTextContent()。equals( "GAAnalytics"))他の値があります – hii

+0

XMLの "string"エントリよりも多くの "key"エントリがあるので、最初のアプローチは動作しません。最後のものをお試しください。 – Juliane

+0

私は次のコードを試しました。しかし、私は出力を取得していませんNodeList nodeList = document.getElementsByTagName( "key"); NodeList nodeList1 = document.getElementsByTagName( "string"); for(int i = 0; i hii

関連する問題