java DOM XML Parser内部元素

时间:2017-05-10 23:24:45

标签: java xml parsing dom

我能够使用XML DOM Parser解析xml的主要元素,例如我可以获取getElementsByTagName(“id”)或getElementsByTagName(“stock_number”),依此类推,但我得到了一个问题,因为在标签中我需要只能返回。的信息。

我在 feed.xml 文件中有一个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标识符。

我已经搜索了很多并研究了如何使用DOM解析器,我尝试通过创建Node的子元素但我收到错误。 我很感激你的回答。

1 个答案:

答案 0 :(得分:0)

要获取图片标记中的子项,您无法直接在images标记中调用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");
        }
    }
}