繁体   English   中英

使用平面DOM解析器在Java中使用XML解析返回null值

[英]Null value returnred with XML parsing in java using plane DOM parser

我试图使用平面DOM解析器解析Java中的collada(.dae)文件。 当我尝试获取价值时,它将返回null。 我尝试了其他讨论的答案和建议,但没有成功。 我使用的代码如下。

for(int k1=0;k1<meshlist.getLength();k1++) {
    Element geometryItr1 = (Element)geometrylist.item(k);

    NodeList trianglelist = geometryItr1.getElementsByTagName("triangles");

    //System.out.println("Triangles length is " + trianglelist.getLength());     

        for(int o=0;o<trianglelist.getLength();o++) {

            Element trichildnodes = (Element) trianglelist.item(o);
            NodeList inputs = trichildnodes.getElementsByTagName("input");
        NodeList p = trichildnodes.getElementsByTagName("p");
        Element ppp = (Element) p.item(0);
        System.out.println("Node Value " + ppp.getNodeValue());
        System.out.println(inputs.getLength() + "Input length");

        for(int in=0;in<inputs.getLength();in++) {

            Element inn = (Element) inputs.item(in);
            System.out.println(inn.getAttribute("semantic") + " " + inn.getAttribute("source") + " Attributes");

        }


        //System.out.println(p.getLength() +  " P's length" );
        //System.out.println("P's content " + ppp.getFirstChild().getNodeValue());


    }   
}

XML非常大,我正在发布一个我试图解析的部分。

<mesh>
  <source> </source>
  <source> </source>
  <source> </source>
  <triangles>
    <input />
    <input />
    <input />
    <p> 24 262 2 72 72 72 72 2222 8198219  </p>
  <triangles>
  <triangles>
    <input />
    <input />
    <input />
    <p> 24 262 2 72 72 72 72 2222 8198219  </p>
  <triangles>
  <triangles>
    <input />
    <input />
    <input />
    <p> 24 262 2 72 72 72 72 2222 8198219  </p>
  <triangles>
  <triangles>
    <input />
    <input />
    <input />
    <p> 24 262 2 72 72 72 72 2222 8198219  </p>
  <triangles>
</mesh>

我试图获取<p>的值。 除了获得p的值之外,一切都正常。 但是,当我调试时,我可以看到与第一个孩子关联的值。 我什至尝试使用firstChild。 我完全迷失了试图寻找解决方案的解析。 请有人帮我找到关于如何获得p值的解决方案。

当我改用getTextContent时,我得到如下输出:

NodeValue null
NodeValue 24 262 2 72 72 72 72 2222 8198219
NodeValue null

对于两个标签,输出为空白。

从Java SE 5开始,我建议使用JDK / JRE中提供的javax.xml.xpath API,以使XML文档的处理更加容易:

package forum11688757;

import java.io.File;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document document = db.parse(new File("src/forum11688757/input.xml"));

        XPathFactory xpf = XPathFactory.newInstance();
        XPath xpath = xpf.newXPath();
        NodeList nodeList = (NodeList) xpath.evaluate("/mesh/triangles/p", document, XPathConstants.NODESET);
        for(int x=0; x<nodeList.getLength(); x++) {
            System.out.println(nodeList.item(x).getTextContent());
        }
    }

}

input.xml中

<mesh>
  <source> </source>
  <source> </source>
  <source> </source>
  <triangles>
    <input />
    <input />
    <input />
    <p> 24 262 2 72 72 72 72 2222 8198219  </p>
  </triangles>
  <triangles>
    <input />
    <input />
    <input />
    <p> 24 262 2 72 72 72 72 2222 8198219  </p>
  </triangles>
  <triangles>
    <input />
    <input />
    <input />
    <p> 24 262 2 72 72 72 72 2222 8198219  </p>
  </triangles>
  <triangles>
    <input />
    <input />
    <input />
    <p> 24 262 2 72 72 72 72 2222 8198219  </p>
  </triangles>
</mesh>

产量

 24 262 2 72 72 72 72 2222 8198219  
 24 262 2 72 72 72 72 2222 8198219  
 24 262 2 72 72 72 72 2222 8198219  
 24 262 2 72 72 72 72 2222 8198219  

UPDATE

您还可以使用以下代码行获取p元素。 但是您需要小心,因为它将获取所有p元素,而不仅仅是/mesh/triangles/p路径中的元素

NodeList nodeList = document.getElementsByTagName("p");

下面的方法将总是得到你正在寻找的数据,即使p eleements在文档中后来添加的其他地方。

NodeList nodeList = (NodeList) xpath.evaluate("/mesh/triangles/p", document, XPathConstants.NODESET);

ElementnodeValue()记录为空。

相反,您可能想调用getTextContent() 但是请注意,它具有自己的特质(如果在树的根上调用它,它将连接树中所有元素的文本)。

如果不需要它们,则不必遍历先前的节点。 例如,这是在<p>标记中打印所有文本内容的方法:

    File xmlPath = new File("test.xml");

    DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
    fabrique.setCoalescing(true);
    fabrique.setIgnoringElementContentWhitespace(true);

    DocumentBuilder constructeur = fabrique.newDocumentBuilder();

    Document document = constructeur.parse(xmlPath);  
    document.setXmlVersion("1.0");
    Element racine = document.getDocumentElement();
    NodeList liste = racine.getElementsByTagName("p");

    for(int i=0; i<liste.getLength(); i++) {
        Element e = (Element)liste.item(i);  
        System.out.println(e.getFirstChild().getTextContent());
    }

您可以使用它来详细说明您想要的东西。 如果需要属性值,只需使用: e.getAttribute("att_name")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM