Java Dom Xml解析器

时间:2014-04-17 16:09:14

标签: java xml

我有像这样的xml文件

<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<head>
<meta name="dtb:uid" content="Spontaneous Derivation [2008.12.10-21:02:00]"/>
<meta name="dtb:depth" content="1"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
</head>
<navMap>

<navPoint id="navpoint-1" playOrder="1">
<navLabel>
<text>Java Tutorial</text>
</navLabel>
<content src="3.html"/>
</navPoint>

</navMap>
</ncx>

我想在内容标记

中提取src属性值

这是我的尝试

   try {
         File fXmlFile = new File("toc.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();
        NodeList nList = doc.getElementsByTagName("navPoint");
        for (int temp = 0; temp < nList.getLength(); temp++) {
             Node nNode = nList.item(temp);

            System.out.println("\nCurrent Element :" + nNode.getNodeName());

            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

           System.out.println("content : " +   eElement.getElementsByTagName("content"));

            }
        }


    } catch (Exception e) {
        e.printStackTrace();
    }

我的问题是如何获取attr值以在父标记内标记。 在我的例子中,src attr的值总是为null。 非常感谢

2 个答案:

答案 0 :(得分:0)

尝试这种方式:

try {
            File fXmlFile = new File("/home/angelo/Scrivania/toc.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("navPoint");
            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);

                System.out.println("\nCurrent Element :" + nNode.getNodeName());

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    System.out.println("content : " +   eElement.getElementsByTagName("content"));
                    NodeList nl = eElement.getElementsByTagName("content");
                    for(int i = 0; i < nl.getLength(); i++)
                    {
                        Node aNode = nl.item(i);
                        NamedNodeMap nnm = aNode.getAttributes();
                        System.out.println(nnm.getNamedItem("src").getTextContent());
                    }

                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

但是有更快更好的XML阅读方式

安吉洛

答案 1 :(得分:0)

如需更友好的API,请查看Xsylum。使用DOM:

String src = Xsylum.elementFor(xmlFile).get("navMap").get("navPoint").get("content").attribute("src");

或者使用XPath:

String src = Xsylum.elementFor(xmlFile).value("//navPoint/content/@src");