Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取以下xml代码的文本内容_Java_Xml Parsing - Fatal编程技术网

Java 如何获取以下xml代码的文本内容

Java 如何获取以下xml代码的文本内容,java,xml-parsing,Java,Xml Parsing,我需要从上面的xml中获取输出。我尝试了下面的代码,但没有显示任何输出 你能帮我吗 上面一个是XML文件。我需要从上面的xml文件中获取Google analytics的值 Above one is the XML file. I need to get the value for the Google analytics from the above xml file 公共类Xml{ 公共静态void main(字符串[]args)引发SAXException、IOException

我需要从上面的xml中获取输出。我尝试了下面的代码,但没有显示任何输出
你能帮我吗

上面一个是XML文件。我需要从上面的xml文件中获取Google analytics的值

    Above one is the XML file. I need to get the value for the Google analytics from the above xml file
公共类Xml{
公共静态void main(字符串[]args)引发SAXException、IOException、ParserConfiguration异常、XPathExpressionException{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document Document=db.parse(新文件输入流(新文件(“C:\\Users\\VAMSIKRISHNA\\Desktop\\Apache Poi\\appConfig.plist”));
NodeList NodeList=document.getElementsByTagName(“key”);
NodeList nodeList1=document.getElementsByTagName(“字符串”);
for(int i=0;i对于(int j=0;j您需要更改行

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

有两个问题:

  • 当您想要比较内容时,可以比较名称

  • 当字符串应该与.equals()进行比较时,将字符串与==

  • 您还可以简化某些内容(仅一个用于循环)

    publicstaticvoidmain(String[]args)抛出SAXException、IOException、ParserConfiguration异常、XPathExpressionException{
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=dbf.newDocumentBuilder();
    Document Document=db.parse(新文件输入流(新文件(“C:\\Users\\VAMSIKRISHNA\\Desktop\\Apache Poi\\appConfig.plist”));
    NodeList NodeList=document.getElementsByTagName(“key”);
    NodeList nodeList1=document.getElementsByTagName(“字符串”);
    for(int i=0;i

    警告:这确实假设您具有相同数量的键和字符串元素。

    您的示例XML与您的解释不匹配,并且它是无效的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++) {
            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);
            }
        }
    }
    
    prod
    GA
    UA-111111-24
    
    您应该浏览列表中的所有配置元素,然后打印子元素的名称和文本内容。

    尝试node.getTextContent().equals(“GA”)


    请参阅:

    编辑:您应该首先查找包装“dict”,然后搜索此文件的孩子

    <type>prod</type>
    <dict></dict>
    <key>GA</key>
    <string>UA-111111-24</string>
    
    NodeList dictList=document.getElementsByTagName(“dict”);
    对于(int j=0;j
    非常感谢您的及时回复。我已经用您的更改重新编辑了代码。但是在下面的行System.out.println(node1.getFirstChild().getTextContent());很高兴我能提供帮助;您需要添加一个空检查:if(node1.getFirstChild()!=null)我已经在代码中添加了空检查。我尝试过使用您的代码,但它显示了所有元素的字符串值。我在上面编辑了我的xml文件。请检查一下。我需要专门用于google Analytics的值。在实现代码后,我仍然无法获得正确的值。例如if(node.getTextContent()).equals(“砷化镓分析”))我得到了一些其他值–这只是我xml文件中的示例行。我需要得到GA值作为UA-111111-24。我无法得到它。你能帮我发布整个xml文件吗?你能帮我试试吗。见下文。@Juliane你能帮我在实现代码后没有得到正确的值吗。例如如果(node.getTextContent().equals(“GAAnalytics”))我得到了一些其他值xml中的“键”项多于“字符串”项,因此我的第一种方法无法工作。请尝试最后一种方法。我已尝试使用以下代码。但我没有得到任何输出NodeList NodeList=document.getElementsByTagName(“键”);NodeList nodeList1=document.getElementsByTagName(“字符串”);for(int i=0;ipublic 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); } } }
    <type>prod</type>
    <dict></dict>
    <key>GA</key>
    <string>UA-111111-24</string>
    
        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());
                    }
                }
            }
        }