gpt4 book ai didi

java - XML 解析对某些标签返回 null

转载 作者:行者123 更新时间:2023-12-02 07:40:24 25 4
gpt4 key购买 nike

我试图解析一些由3ds max和maya生成的xml文件。一切正常,但有些值返回 null。例如,考虑下面由 max 或 maya 生成的 XML(带有 .dae 扩展名的 collada),

  <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

上面我已经解释了我面临的问题。 Here你可以找到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 可能有什么问题?请有人在这方面帮助我。感谢您阅读我的问题。

维奈

最佳答案

这可能是 Eclipse 的错误。你的代码似乎是正确的,我写了 my own XPath demo 来测试输入文件。该程序在 Eclipse 中运行良好,但是当我尝试输出 <p> 时内容来自 System.out.println() ,第二个<p>没有打印到 Eclipse 控制台,因为该行字符太多(超过 30K)。请参阅this comment在 Eclipse bugzilla 上:

I get weird things happening with the console on the Mac, when printing long lines eg:

public static void main(String[] args)
{
System.out.print("Counting");
StringBuffer buffer = new StringBuffer();
for (int i=0; i<28504; i++)
{
buffer.append("*");
}
System.out.print(" some more");
System.out.println(buffer);
System.out.println("Complete");
}

The output I get from this is as follows:

Counting some
Complete

ie the long string of '*' isn't displayed and overwrites part of the preceding output

Steve

因此文件已成功读取和处理,目标信息已完全收集,只有输出部分失败,但这不取决于您 - 并且它不会影响程序,因为我认为这不是您想要的正在向用户的眼睛打印一大行文本

关于java - XML 解析对某些标签返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11699382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com