2017-05-10 136 views
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&nbsp;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&nbsp;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解析器,我尝试通过创建节点的子元素,但我得到错误。 我很感谢你的回答。

回答

0

要让图像标记中的孩子,您不能直接在图像标记中调用getChildNodes,因为有一些额外的节点,请参阅java xml cast Node to Element。 因此,请尝试通过getElementsByTagName直接获取孩子。

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()); 
     Element images = (Element) eElement2.getElementsByTagName("images").item(0); 
     Element image1Node = (Element) images.getElementsByTagName("Image1").item(0); 
     Element image2Node = (Element) images.getElementsByTagName("Image2").item(0); 
     Element image3Node = (Element) images.getElementsByTagName("Image3").item(0); 
     if (image1Node.getAttribute("image").equals("1")) { 
      System.out.println("image1Node id attribute is 1"); 
     } 
     if (image2Node.getAttribute("image").equals("1")) { 
      System.out.println("image2Node id attribute is 1"); 
     } 
     if (image3Node.getAttribute("image").equals("1")) { 
      System.out.println("image3Node id attribute is 1"); 
     } 
    } 
}