代码之家  ›  专栏  ›  技术社区  ›  Espen

爪哇中如何解析高级XML文件

  •  1
  • Espen  · 技术社区  · 14 年前

    我见过很多关于如何在Java中读取XML文件的例子。但它们只显示简单的XML文件。例如,它们演示了如何从XML文件中提取名字和姓氏。但是,我需要从collada XML文件中提取数据。这样地:

    <library_visual_scenes>
        <visual_scene id="ID1">
            <node name="SketchUp">
                <instance_geometry url="#ID2">
                    <bind_material>
                        <technique_common>
                            <instance_material symbol="Material2" target="#ID3">
                                <bind_vertex_input semantic="UVSET0" input_semantic="TEXCOORD" input_set="0" />
                            </instance_material>
                        </technique_common>
                    </bind_material>
                </instance_geometry>
            </node>
        </visual_scene>
    </library_visual_scenes>
    

    这只是collada文件的一小部分。这里我需要提取视觉场景的ID,然后提取实例几何的URL,最后提取实例材质的目标。当然,我需要提取更多,但我不知道如何真正地使用它,这是一个开始的地方。

    到目前为止,我有以下代码:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    try {
        builder = factory.newDocumentBuilder();
    }
    catch( ParserConfigurationException error ) {
        Log.e( "Collada", error.getMessage() ); return;
    }
    Document document = null;
    try {
        document = builder.parse( string );
    }
    catch( IOException error ) {
        Log.e( "Collada", error.getMessage() ); return;
    }
    catch( SAXException error ) {
        Log.e( "Collada", error.getMessage() ); return;
    }
    NodeList library_visual_scenes = document.getElementsByTagName( "library_visual_scenes" );
    

    网络上的大多数例子似乎与此类似: http://www.easywayserver.com/blog/java-how-to-read-xml-file/

    当我想要提取更深层的标签或者找到一个关于读取/解析XML文件的好教程时,我需要帮助来弄清楚该怎么做。

    4 回复  |  直到 10 年前
        1
  •  4
  •   Cratylus    14 年前



    document = builder.parse( string );//document is loaded in memory as tree  
    

    NodeList library_visual_scenes = document.getElementsByTagName( "library_visual_scenes" );
    


     for (int i = 0; i < library_visual_scenes.getLength(); i++) {
       Element element = (Element) nodes.item(i);
       Node visual_scene = element.getFirstChild();
       if(visual_scene.getNodeType() == Node.ELEMENT_NODE)
       {
          String id = ((Element)visual_scene).getAttribute(id);
          System.out.println("id="+id);
        }
     }
    

        2
  •  6
  •   LarsH    14 年前

    builder.parse(string)

    Here

    • /*/visual_scene/@id
    • /*/visual_scene/node/instance_geometry/@url
    • /*/visual_scene/node[@name = 'Sketchup']/instance_geometry/@url
    • /*/visual_scene/node/instance_geometry/bind_material/technique_common/instance_material/@target

        3
  •  1
  •   bdoughan    14 年前

    EclipseLink JAXB (MOXy)

    package blog.geocode;
    
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlType;
    
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="kml")
    @XmlType(propOrder={"country", "state", "city", "street", "postalCode"})
    public class Address {
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:Thoroughfare/ns:ThoroughfareName/text()")
        private String street;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:LocalityName/text()")
        private String city;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:AdministrativeAreaName/text()")
        private String state;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:CountryNameCode/text()")
        private String country;
    
        @XmlPath("Response/Placemark/ns:AddressDetails/ns:Country/ns:AdministrativeArea/ns:SubAdministrativeArea/ns:Locality/ns:PostalCode/ns:PostalCodeNumber/text()")
        private String postalCode;
    
    }
    

        4
  •  0
  •   Pete    10 年前