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 Dom Xml解析器_Java_Xml - Fatal编程技术网

Java Dom Xml解析器

Java Dom Xml解析器,java,xml,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:totalP

我有这样的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>

Java教程
我想提取内容标记内的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();
    }
试试看{
File fXmlFile=新文件(“toc.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“导航点”);
对于(int-temp=0;temp
我的问题是如何在父标记内获取attr to标记的值。 在我的例子中,src attr的值总是null。 非常感谢

请尝试以下方法:

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();
        }
试试看{
File fXmlFile=新文件(“/home/angelo/Scrivania/toc.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“导航点”);
对于(int-temp=0;temp
但是有更快更好的XML读取方法


Angelo要获得更友好的API,请查看。使用DOM:

String src = Xsylum.elementFor(xmlFile).get("navMap").get("navPoint").get("content").attribute("src");
或者使用XPath:

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

我们期待你方作出一定程度的努力。告诉我们您尝试了什么,并告诉我们失败的原因。那么,您为什么选择
导航点
?为什么不在调用
getElementsByTagName
时选择
content
?因为NavPoint中的内容标签感谢您的帮助Angelo。但是,请告诉我xml解析器中其他更快的方法。