java解析XML的包有jdom,jaxp等。你可以上sun的网站查找jaxp,这个应该是最新的xml解析工具

解决方案 »

  1.   

    package app;import java.io.File;
    import org.w3c.dom.Document;
    import org.w3c.dom.*;import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.DocumentBuilder;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;    public class ReadAndPrintXMLFile{
          public static void main (String argv []){
            try {          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
              DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
              Document doc = docBuilder.parse (new File("book.xml"));        // normalize text representation
            doc.getDocumentElement ().normalize ();
            System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());        NodeList listOfPersons = doc.getElementsByTagName("person");
            int totalPersons = listOfPersons.getLength();
            System.out.println("Total no of people : " + totalPersons);
            for(int s=0; s<listOfPersons.getLength() ; s++){          Node firstPersonNode = listOfPersons.item(s);
              if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){            Element firstPersonElement = (Element)firstPersonNode;
             //-------
              NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
              Element firstNameElement = (Element)firstNameList.item(0);          NodeList textFNList = firstNameElement.getChildNodes();
              System.out.println("First Name : " + ((Node)textFNList.item(0)).getNodeValue().trim());
            //-------
            NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
            Element lastNameElement = (Element)lastNameList.item(0);        NodeList textLNList = lastNameElement.getChildNodes();
            System.out.println("Last Name : " + ((Node)textLNList.item(0)).getNodeValue().trim());
            //----
            NodeList ageList = firstPersonElement.getElementsByTagName("age");
            Element ageElement = (Element)ageList.item(0);        NodeList textAgeList = ageElement.getChildNodes();
            System.out.println("Age : " + ((Node)textAgeList.item(0)).getNodeValue().trim());
            //------
            }//end of if clause
          }//end of for loop with s var
        }catch (SAXParseException err) {
          System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
          System.out.println(" " + err.getMessage ());    }catch (SAXException e) {
          Exception x = e.getException ();
          ((x == null) ? e : x).printStackTrace ();    }catch (Throwable t) {
          t.printStackTrace ();
        }
        //System.exit (0);
      }//end of main
    }
      

  2.   

    TO:: newste(旭林) : EQsay
    谢谢你们的热情回答.
    我的XML文档资料太复杂了,还是不太好完成.
    贴点内容给你们看看.
    /////////////////////////
    - <cims:ClassCategory rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#CIM">
      <rdfs:label xml:lang="en">CIM</rdfs:label> 
      <rdfs:comment>The Common Information Model (CIM) represents a common logical view of EMS public data. The CIM definition consists of a comprehensive information model. This information model is largely independent of any specific technological implementation methods.</rdfs:comment> 
      </cims:ClassCategory>
    - <rdfs:Class rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#CimVersion">
      <rdfs:label xml:lang="en">CimVersion</rdfs:label> 
      <rdfs:comment>This is the CIM version number assigned to this UML model file. The Version naming convention is to use the last major revision file name followed by the date of the last revision 9e.g., cimu08b_001212).</rdfs:comment> 
      <cims:profile>Nerc</cims:profile> 
      <cims:belongsToCategory rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#CIM" /> 
      </rdfs:Class>
    - <rdf:Property rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#CimVersion.Version">
      <rdfs:label xml:lang="en">Version</rdfs:label> 
      <rdfs:comment /> 
      <rdfs:domain rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#CimVersion" /> 
      <cims:dataType rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#String" /> 
      </rdf:Property>
    - <cims:ClassCategory rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#Core">
      <rdfs:label xml:lang="en">Core</rdfs:label> 
      <rdfs:comment>Contains the core PowerSystemResource and ConductingEquipment entities shared by all applications plus common collections of those entities. Not all applications require all the Core entities.</rdfs:comment> 
      </cims:ClassCategory>
    - <rdfs:Class rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#Bay">
      <rdfs:label xml:lang="en">Bay</rdfs:label> 
      <rdfs:comment>A collection of power system resources (within a given substation) including conducting equipment, protection relays, measurements, and telemetry. The typeName indicates bay classification code according to application, e.g., (OH = Overheadline) (CA = Capacitor).</rdfs:comment> 
      <cims:profile>Nerc</cims:profile> 
      <cims:belongsToCategory rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#Core" /> 
      <rdfs:subClassOf rdf:resource="http://iec.ch/TC57/2001/CIM-schema-cim10#EquipmentContainer" /> 
      </rdfs:Class>
    - <rdf:Property rdf:about="http://iec.ch/TC57/2001/CIM-schema-cim10#Bay.bayEnergyMeasFlag">
      <rdfs:label xml:lang="en">bayEnergyMeasFlag</rdfs:label> 
      <rdfs:comment>Indicates the presence/absence of kWh/kvarh 
    .....
      

  3.   

    你把xml的文档资料贴出来看看
      

  4.   

    你这个是rdf的Schema,用jena包来解析最好了。
      

  5.   

    TO: flyforlove
    能够解释一下jena包吗?
    说详细一点,谢谢!
      

  6.   

    jena原来是hp工作室开发的,现在作为开源项目,
    你可以在http://jena.sourceforge.net/找到它所有的信息。
      

  7.   

    TO: flyforlove
    我要解析的的确是RDF文件,
    我下载了Jena-2.1.
    发现用不起来.
    你能指点一下吗?
    谢谢!
      

  8.   

    你要在classpath里面配置下Jena-2.1的路径