未检测到子节点

时间:2012-07-27 07:29:08

标签: java xml parsing

这是一个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之外,它甚至不检测子节点输入和p。可能是什么问题?请帮我解决这个问题。

1 个答案:

答案 0 :(得分: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());
    }