我试图解析一些由 3ds max 和 maya 生成的 xml 文件。一切正常,但有些值返回 null。例如下面的 concider(带有 .dae 扩展名的 collada)由 max 或 maya 生成的 xml,
<triangles>
<p> 234 32 4 23 544 35 43 5 435</p>
</triangles>
<triangles>
<p> 43543 45 34 5 12 34 4 36457 6</p>
</triangles>
<triangles>
<p>2345 325 34 543 5 34 534 5 435</p>
</triangles>
现在,当我使用 java 代码解析时(如果需要,我会提供),
输出是:
p has 234 32 4 23 544 35 43 5 435
p has
p has 2345 325 34 543 5 34 534 5 435
即使有值,输出的第二行也不显示任何内容。现在,如果我自己编辑该行,则输出与预期的一样,即输出如下所示。这里我在 xml 文件中给出了我自己的值。
编辑的xml是
<triangles>
<p> 234 32 4 23 544 35 43 5 435</p>
</triangles>
<triangles>
<p> 28 234 34 32 4 23 4 23 423 43</p>
</triangles>
<triangles>
<p>2345 325 34 543 5 34 534 5 435</p>
</triangles>
输出是:
p has 234 32 4 23 544 35 43 5 435
p has 28 234 34 32 4 23 4 23 423 43
p has 2345 325 34 543 5 34 534 5 435
上面我已经解释了我面临的问题。在这里您可以找到由 Maya 生成的文件。我使用的java代码如下。
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Main {
public static void main(String args[]) {
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("d://tablemaya.dae"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("library_geometries");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
Node firstPersonNode = listOfPersons.item(0);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList geometrylist = firstPersonElement.getElementsByTagName("geometry");
System.out.println(geometrylist.getLength() + " Geometry size");
for(int k=0;k<geometrylist.getLength();k++) {
Element geometryItr = (Element)geometrylist.item(k);
NodeList meshlist = geometryItr.getElementsByTagName("mesh");
System.out.println("Mesh length is " + meshlist.getLength());
for(int k1=0;k1<meshlist.getLength();k1++) {
Element geometryItr1 = (Element)meshlist.item(k1);
NodeList meshlist1 = geometryItr1.getElementsByTagName("source");
System.out.println("Source length is " + meshlist1.getLength());
}
for(int k2=0;k2<meshlist.getLength();k2++) {
Element geometryItr1 = (Element)meshlist.item(k2);
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");
//System.out.println("Fucking Problem " + p.item(0).getFirstChild().getNodeValue());
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");
}
for(int i=0; i<p.getLength(); i++) {
Element e = (Element)p.item(i);
String ss = e.getFirstChild().getTextContent();
System.out.println("Noide is " + ss);
}
//System.out.println(p.getLength() + " P's length" );
//System.out.println("P's content " + ppp.getFirstChild().getNodeValue());
}
}
for(int k1=0;k1<meshlist.getLength();k1++) {
Element geometryItr1 = (Element)meshlist.item(k1);
NodeList meshlist1 = geometryItr1.getElementsByTagName("vertices");
System.out.println("Vertices length is " + meshlist1.getLength());
}
}
}
}catch (Exception err) {
err.printStackTrace();
}
}
}
我无法获取<p>
. 我的代码或xml可能有什么问题?请有人在这方面帮助我。感谢您阅读我的问题。
维奈