0
我能够通过使用XML DOM解析器解析XML的主要元素,例如我可以获取getElementsByTagName(“id”)或getElementsByTagName(“stock_number”)等等上,但我遇到了一个问题,因为在标签我需要能够只返回的信息。java DOM XML解析器内部元素
我在文件中的XML feed.xml这样的:
<inventory>
<item UnitID="1234" Record="0">
<id>1234</id>
<dealerid>455</dealerid>
<stock_number>1600Xtreme</stock_number>
<make>Nvidia</make>
<noteshtml>This is some random info about the product1234.<br><br>
Buy with trust from a LQZ dealership.<br><br> Contact Dave at
Burnaby location - 604 123 4578.<br>
</noteshtml>
<images>
<Image1 image="1" UnitID="1234">
https://anydomain.com/photo54.jpg
</Image1>
<Image2 image="2" UnitID="1234">
https://anydomain.com/photo22.jpg
</Image2>
<Image3 image="3" UnitID="1234">
https://anydomain.com/photo32.jpg
</Image3>
</images>
</item>
<item UnitID="7854" Record="1">
<id>7854</id>
<dealerid>587</dealerid>
<stock_number>12TMAX5500</stock_number>
<make>Realtek</make>
<noteshtml>This is some random info about the product 7854.<br><br>
Buy with trust from a LQZ dealership.<br><br> Contact Dave at
Burnaby location - 604 123 4578.<br>
</noteshtml>
<images>
<Image1 image="1" UnitID="7854">
https://anydomain.com/photo656.jpg
</Image1>
<Image2 image="2" UnitID="7854">
https://anydomain.com/photo6565.jpg
</Image2>
<Image3 image="3" UnitID="7854">
https://anydomain.com/photo908.jpg
</Image3>
</images>
</item>
</inventory>
这是一般我的Java代码,我得到了什么至今:
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;
int search=1234;
System.out.println("This is the value to search from my variable: " + search);
int toTest=Integer.parseInt(eElement2.getAttribute("UnitID"));
System.out.println("toTest is equal to: " + toTest);
if (toTest == search)
{
System.out.println("stock Number: " + eElement2.getElementsByTagName("stock_number").item(0).getTextContent());
}
}
我的问题是如何现在进入图像标签,并只打印该图像= 1标识符的URL。
我有很多搜索,并研究如何使用DOM解析器,我尝试通过创建节点的子元素,但我得到错误。 我很感谢你的回答。