Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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 DOM上使用getElementsByTagName()值的IF语句_Java_Xml_Parsing_Dom_Screen Scraping - Fatal编程技术网

使用Java在XML DOM上使用getElementsByTagName()值的IF语句

使用Java在XML DOM上使用getElementsByTagName()值的IF语句,java,xml,parsing,dom,screen-scraping,Java,Xml,Parsing,Dom,Screen Scraping,我有一个名为feed.XML的XML文件,我正在使用JAVA上的DOM包来废弃数据 我能够成功地废弃数据,现在我需要能够根据从XML获得的数据创建IF语句 我的问题是,即使我在字符串变量中分配属性数据,当我使用IF进行比较时,条件返回FALSE,而它实际上应该是正确的 这是我的一些XML文档 <inventory> <item UnitID="1234" Record="0"> <id>1234</id> &

我有一个名为feed.XML的XML文件,我正在使用JAVA上的DOM包来废弃数据

我能够成功地废弃数据,现在我需要能够根据从XML获得的数据创建IF语句

我的问题是,即使我在字符串变量中分配属性数据,当我使用IF进行比较时,条件返回FALSE,而它实际上应该是正确的

这是我的一些XML文档

<inventory>
    <item UnitID="1234" Record="0">
        <id>1234</id>
        <dealerid>455</dealerid>
        <stock_number>1600Xtreme</stock_number>
        <make>Nvidia</make>                       
    </item>
    <item UnitID="7854" Record="1">
        <id>7854</id>
        <dealerid>587</dealerid>
        <stock_number>12TMAX5500</stock_number>
        <make>Realtek</make> 
    </item>
</inventory>

1234
455
1600Xtreme
英伟达
7854
587
12TMAX5500
雷亚泰克
这是我的一些用于抓取数据的JAVA代码,也是我假装使用的IF语句:

File fXmlFile=new File("feed.xml");
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName("item");
for (int temp=0; temp < nList.getLength(); temp++)
{
    Node nNode=nList.item(temp);
    Element eElement2=(Element)nNode;
    String search="Nvidia";
    System.out.println("This is the value to search from my variable: " + 
    search); //This prints Nvidia
    String toTest=(eElement2.getAttribute("make"));
    System.out.println("toTest is equal to: " + toTest); //This prints 
    Nvidia
    if (toTest == search)
    {
        System.out.println("The condition on the IF is True");
    }
}
File fXmlFile=新文件(“feed.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“项目”);
对于(int-temp=0;temp
我应该得到以下输出: “IF上的条件为真”

然而,我什么也得不到,因为根据JAVA的说法是不正确的


我已经研究并尝试了许多不同的方法来进行比较,但似乎没有任何效果。(如果我比较整数,它对我有效,但在本例中是一个字符串)我感谢您的回答。

您希望在比较对象时使用
.equals
=
对于
字符串
不会返回true,即使它们包含相同的信息,因为
=
通过引用进行比较,而
.equals
通过值进行比较


因此,将其更改为
if(toTest.equals(search))

您希望在比较对象时使用
.equals
=
对于
字符串
不会返回true,即使它们包含相同的信息,因为
=
通过引用进行比较,而
.equals
通过值进行比较


因此将其更改为
if(toTest.equals(search))
UnitID
Record
节点的属性
make
项目
节点的子节点。因此,您需要在“make”节点上获取文本内容,以便检索其值。请尝试以下代码:

File fXmlFile=new File("feed.xml");
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName("item");

for (int temp=0; temp < nList.getLength(); temp++)
    {
        Node nNode=nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            NodeList children = nNode.getChildNodes();
            for(int i = 0; i < children.getLength(); i++) {
                Node childNode = children.item(i);
                if (childNode.getNodeName() == "make"){
                    String make = childNode.getTextContent();
                    if ("Nvidia".equals(make)) {
                        System.out.print(childNode.getTextContent()); // This will print Nvidia
                    }
                }
            }
        }
    }
File fXmlFile=新文件(“feed.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“项目”);
对于(int-temp=0;temp
UnitID
Record
节点的属性
make
项目
节点的子节点。因此,您需要在“make”节点上获取文本内容,以便检索其值。请尝试以下代码:

File fXmlFile=new File("feed.xml");
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
Document doc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName("item");

for (int temp=0; temp < nList.getLength(); temp++)
    {
        Node nNode=nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            NodeList children = nNode.getChildNodes();
            for(int i = 0; i < children.getLength(); i++) {
                Node childNode = children.item(i);
                if (childNode.getNodeName() == "make"){
                    String make = childNode.getTextContent();
                    if ("Nvidia".equals(make)) {
                        System.out.print(childNode.getTextContent()); // This will print Nvidia
                    }
                }
            }
        }
    }
File fXmlFile=新文件(“feed.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“项目”);
对于(int-temp=0;temp