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());
}
}
}
}
}
非常感謝您的及時回覆。我已經用你的改變重新編碼了。但它在下面一行顯示空指針異常System.out.println(node1.getFirstChild()。getTextContent()); – hii
很高興我能幫上忙;您需要添加一個空檢查:if(node1.getFirstChild()!= null) – JensS
我已經將空檢查添加到代碼中。 – JensS