0

我试图使用平面 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

两个标签的输出为空白。

4

3 回答 3

3

我建议使用javax.xml.xpath自 Java SE 5 起在 JDK/JRE 中可用的 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());
        }
    }

}

输入.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  

更新

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

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

以下方法将始终为您提供您正在寻找的数据,即使p后来在文档中的其他位置添加了元素。

NodeList nodeList = (NodeList) xpath.evaluate("/mesh/triangles/p", document, XPathConstants.NODESET);
于 2012-07-27T15:21:15.087 回答
1

nodeValue()of an被记录Element为空。

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

于 2012-07-27T13:52:14.780 回答
1

如果您不需要它们,则不必遍历先前的节点。例如,它是如何打印<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")

于 2012-07-27T14:23:09.483 回答