0

这是一个 XML:

<mesh>

  <triangles material="face" count="990">
          <input semantic="VERTEX" source="#boyShape-skin-vertices" offset="0"/>
          <input semantic="NORMAL" source="#boyShape-skin-normals" offset="1"/>
          <input semantic="TEXCOORD" source="#boyShape-skin-map-channel1" offset="2" set="1"/>
          <p>45 56 44 61 57 45 58 58 46 58 58 46 65 59 47 45 56 44 46 60 48 67 61 49 58 58 46 58 58 46 61 57 45 46 60 48 49 62 50 66 63 51 </p>
  </triangles>
  <triangles material="face" count="1320">
          <input semantic="VERTEX" source="#boyShape-skin-vertices" offset="0"/>
          <input semantic="NORMAL" source="#boyShape-skin-normals" offset="1"/>
          <input semantic="TEXCOORD" source="#boyShape-skin-map-channel1" offset="2" set="1"/>
          <p>45 46 60 48 49 62 50 66 63 51 45 56 44 61 57 45 58 58 46 58 58 46 65 59 47 45 56 44 46 60 48 67 61 49 58 58 46 58 58 46 61 57 45 46 60 48 49 62 50 66 63 51 </p>
  </triangles>
  <triangles material="face" count="1540">
          <input semantic="VERTEX" source="#boyShape-skin-vertices" offset="0"/>
          <input semantic="NORMAL" source="#boyShape-skin-normals" offset="1"/>
          <input semantic="TEXCOORD" source="#boyShape-skin-map-channel1" offset="2" set="1"/>
          <p>58 46 58 58 46 65 59 47 45 56 44 61 57 45 58 58 46 58 58 46 65 59 47 45 56 44 46 60 48 67 61 49 58 58 46 58 58 46 61 57 45 46 60 48 49 62 50 66 63 51 </p>
  </triangles> 
  </mesh>

Java 代码是

for(int s=0; s<mesh.getLength() ; s++){
    Node firstPersonNode = mesh.item(s);
    System.out.println(mesh.item(s).getNodeName() + " Test");
    if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
        Element firstPersonElement = (Element)firstPersonNode;
        NodeList source = firstPersonElement.getElementsByTagName("triangles");    
        System.out.println(source.getLength() + "Source Length");
        Node firstsource = source.item(1); // triangles index given here
        if(firstsource.getNodeType() == Node.ELEMENT_NODE){
            Element firstPersonElement1 = (Element)firstsource;
            System.out.println(firstPersonElement1.getAttribute("id") + " -->  <-- ");
            NodeList floatarray = firstPersonElement1.getElementsByTagName("p");
            Element firstNameElement = (Element)floatarray.item(0);
            NodeList textFNList = firstNameElement.getChildNodes();
            System.out.println("Vertexes : " + ((Node)textFNList.item(0)).getNodeValue().trim());  
        }
    }
}

我正在尝试解析位于三角形节点内的输入和 p 但是,我有一个问题。问题是当我在上述代码中将索引指定为 1 时,所有子节点都被检测到并被解析。除了 1 它甚至没有检测到子节点 input 和 p。可能是什么问题?请帮我解决这个问题。

4

1 回答 1

0

以下是使用 DOM API 解析文档的方法:

    NodeList triangles = document.getElementsByTagName("triangles");
    System.out.println("triangles: " + triangles.getLength());

    for (int i = 0; i < triangles.getLength(); i++) {
        Element triangle = (Element) triangles.item(i);
        System.out.println("triangle: material=" + triangle.getAttribute("material") + " count=" + triangle.getAttribute("count"));

        NodeList inputs = triangle.getElementsByTagName("input");
        for (int j = 0; j < triangles.getLength(); j++) {
            Element input = (Element) inputs.item(j);
            System.out.println("  input: semantic=" + input.getAttribute("semantic"));
        }

        Element vertices = (Element) triangle.getElementsByTagName("p").item(0);
        System.out.println("  vertices: " + vertices.getTextContent().trim());
    }
于 2012-07-27T08:29:13.880 回答