如何使用Stax解析器解析XML

时间:2016-06-28 04:56:39

标签: java xml xml-parsing stax

我想使用STAX Parser解析XML。

1)加载XML内容并将其打印在控制台上。

预期输出:

    ObjectID = OR:wt.part.WTPart:117313:425251666-1466176866678-1329564238-1-0-0-127@tibco.ptc.com

Class = com.ptc.windchill.esi.Part

LastChangedBy = LastChangedBy

等等XML中的标记。

我发现了一些here,但它对我没有帮助, 我的Xml:

<?xml version="1.0" encoding="UTF-8"?>
<wc:COLLECTION xmlns:wc="http://www.ptc.com/infoengine/1.0">
<Release NAME="Release" TYPE="Unknown" STATUS="0">
</Release>
<DeletedECN NAME="DeletedECN" TYPE="Unknown" STATUS="0">
</DeletedECN>
<AddedECN NAME="AddedECN" TYPE="Unknown" STATUS="0">
</AddedECN>
<ChangedECN NAME="ChangedECN" TYPE="Unknown" STATUS="0">
</ChangedECN>
<UnchangedECN NAME="UnchangedECN" TYPE="Unknown" STATUS="0">
</UnchangedECN>
<DeletedParts NAME="DeletedParts" TYPE="Unknown" STATUS="0">
</DeletedParts>
<AddedParts NAME="AddedParts" TYPE="Unknown" STATUS="0">
<Part>
    <ObjectID>OR:wt.part.WTPart:117313:425251666-1466176866678-1329564238-1-0-0-127@tibco.ptc.com</ObjectID>
    <Class>com.ptc.windchill.esi.Part</Class>
    <LastChangedBy>Administrator</LastChangedBy>
    <Number>0000000032</Number>
    <StartEffectivity></StartEffectivity>
    <EndEffectivity></EndEffectivity>
    <StartSerialNumberEffectivity></StartSerialNumberEffectivity>
    <EndSerialNumberEffectivity></EndSerialNumberEffectivity>
    <StartLotNumberEffectivity></StartLotNumberEffectivity>
    <EndLotNumberEffectivity></EndLotNumberEffectivity>
    <SerialNumberEffectivityCxtPartNumber></SerialNumberEffectivityCxtPartNumber>
    <LotNumberEffectivityCxtPartNumber></LotNumberEffectivityCxtPartNumber>
    <DefaultUnit>ea</DefaultUnit>
    <Name>test16</Name>
    <PartType>separable</PartType>
    <Source>make</Source>
    <State>INWORK</State>
    <IsPhantom>false</IsPhantom>
    <Version>A</Version>
    <Iteration>1</Iteration>
    <PreviousVersion></PreviousVersion>
    <IsConfigurable>standard</IsConfigurable>
    <IsCollapsible>false</IsCollapsible>
    <TargetID>1</TargetID>
</Part>
</AddedParts>
<ChangedParts NAME="ChangedParts" TYPE="Unknown" STATUS="0">
</ChangedParts>
<UnchangedParts NAME="UnchangedParts" TYPE="Unknown" STATUS="0">
</UnchangedParts>
<DeletedAlternateLinks NAME="DeletedAlternateLinks" TYPE="Unknown" STATUS="0">
</DeletedAlternateLinks>
<AddedAlternateLinks NAME="AddedAlternateLinks" TYPE="Unknown" STATUS="0">
</AddedAlternateLinks>
<DeletedDocuments NAME="DeletedDocuments" TYPE="Unknown" STATUS="0">
</DeletedDocuments>
<AddedDocuments NAME="AddedDocuments" TYPE="Unknown" STATUS="0">
</AddedDocuments>
<ChangedDocuments NAME="ChangedDocuments" TYPE="Unknown" STATUS="0">
</ChangedDocuments>
<UnchangedDocuments NAME="UnchangedDocuments" TYPE="Unknown" STATUS="0">
</UnchangedDocuments>
<DeletedDocumentLinks NAME="DeletedDocumentLinks" TYPE="Unknown" STATUS="0">
</DeletedDocumentLinks>
<AddedDocumentLinks NAME="AddedDocumentLinks" TYPE="Unknown" STATUS="0">
</AddedDocumentLinks>
<ChangedDocumentLinks NAME="ChangedDocumentLinks" TYPE="Unknown" STATUS="0">
</ChangedDocumentLinks>
<UnchangedDocumentLinks NAME="UnchangedDocumentLinks" TYPE="Unknown" STATUS="0">
</UnchangedDocumentLinks>
<DeletedBOMs NAME="DeletedBOMs" TYPE="Unknown" STATUS="0">
</DeletedBOMs>
<AddedBOMs NAME="AddedBOMs" TYPE="Unknown" STATUS="0">
</AddedBOMs>
<ChangedBOMs NAME="ChangedBOMs" TYPE="Unknown" STATUS="0">
</ChangedBOMs>
<UnchangedBOMs NAME="UnchangedBOMs" TYPE="Unknown" STATUS="0">
</UnchangedBOMs>
<DeletedBOMComponents NAME="DeletedBOMComponents" TYPE="Unknown" STATUS="0">
</DeletedBOMComponents>
<AddedBOMComponents NAME="AddedBOMComponents" TYPE="Unknown" STATUS="0">
</AddedBOMComponents>
<ChangedBOMComponents NAME="ChangedBOMComponents" TYPE="Unknown" STATUS="0">
</ChangedBOMComponents>
<UnchangedBOMComponents NAME="UnchangedBOMComponents" TYPE="Unknown" STATUS="0">
</UnchangedBOMComponents>
<DeletedSubstitutes NAME="DeletedSubstitutes" TYPE="Unknown" STATUS="0">
</DeletedSubstitutes>
<AddedSubstitutes NAME="AddedSubstitutes" TYPE="Unknown" STATUS="0">
</AddedSubstitutes>
<DeletedReferenceDesignators NAME="DeletedReferenceDesignators" TYPE="Unknown" STATUS="0">
</DeletedReferenceDesignators>
<AddedReferenceDesignators NAME="AddedReferenceDesignators" TYPE="Unknown" STATUS="0">
</AddedReferenceDesignators>
<DeletedProcessMaterials NAME="DeletedProcessMaterials" TYPE="Unknown" STATUS="0">
</DeletedProcessMaterials>
<AddedProcessMaterials NAME="AddedProcessMaterials" TYPE="Unknown" STATUS="0">
</AddedProcessMaterials>
<ChangedProcessMaterials NAME="ChangedProcessMaterials" TYPE="Unknown" STATUS="0">
</ChangedProcessMaterials>
<UnchangedProcessMaterials NAME="UnchangedProcessMaterials" TYPE="Unknown" STATUS="0">
</UnchangedProcessMaterials>
<DeletedToolings NAME="DeletedToolings" TYPE="Unknown" STATUS="0">
</DeletedToolings>
<AddedToolings NAME="AddedToolings" TYPE="Unknown" STATUS="0">
</AddedToolings>
<ChangedToolings NAME="ChangedToolings" TYPE="Unknown" STATUS="0">
</ChangedToolings>
<UnchangedToolings NAME="UnchangedToolings" TYPE="Unknown" STATUS="0">
</UnchangedToolings>
<DeletedProcessPlans NAME="DeletedProcessPlans" TYPE="Unknown" STATUS="0">
</DeletedProcessPlans>
<AddedProcessPlans NAME="AddedProcessPlans" TYPE="Unknown" STATUS="0">
</AddedProcessPlans>
<ChangedProcessPlans NAME="ChangedProcessPlans" TYPE="Unknown" STATUS="0">
</ChangedProcessPlans>
<UnchangedProcessPlans NAME="UnchangedProcessPlans" TYPE="Unknown" STATUS="0">
</UnchangedProcessPlans>
<DeletedOperations NAME="DeletedOperations" TYPE="Unknown" STATUS="0">
</DeletedOperations>
<AddedOperations NAME="AddedOperations" TYPE="Unknown" STATUS="0">
</AddedOperations>
<ChangedOperations NAME="ChangedOperations" TYPE="Unknown" STATUS="0">
</ChangedOperations>
<UnchangedOperations NAME="UnchangedOperations" TYPE="Unknown" STATUS="0">
</UnchangedOperations>
<DeletedOperationUsageLinks NAME="DeletedOperationUsageLinks" TYPE="Unknown" STATUS="0">
</DeletedOperationUsageLinks>
<AddedOperationUsageLinks NAME="AddedOperationUsageLinks" TYPE="Unknown" STATUS="0">
</AddedOperationUsageLinks>
<ChangedOperationUsageLinks NAME="ChangedOperationUsageLinks" TYPE="Unknown" STATUS="0">
</ChangedOperationUsageLinks>
<UnchangedOperationUsageLinks NAME="UnchangedOperationUsageLinks" TYPE="Unknown" STATUS="0">
</UnchangedOperationUsageLinks>
<DeletedProcessPlanPartLinks NAME="DeletedProcessPlanPartLinks" TYPE="Unknown" STATUS="0">
</DeletedProcessPlanPartLinks>
<AddedProcessPlanPartLinks NAME="AddedProcessPlanPartLinks" TYPE="Unknown" STATUS="0">
</AddedProcessPlanPartLinks>
<ChangedProcessPlanPartLinks NAME="ChangedProcessPlanPartLinks" TYPE="Unknown" STATUS="0">
</ChangedProcessPlanPartLinks>
<UnchangedProcessPlanPartLinks NAME="UnchangedProcessPlanPartLinks" TYPE="Unknown" STATUS="0">
</UnchangedProcessPlanPartLinks>
<DeletedSequences NAME="DeletedSequences" TYPE="Unknown" STATUS="0">
</DeletedSequences>
<AddedSequences NAME="AddedSequences" TYPE="Unknown" STATUS="0">
</AddedSequences>
<ChangedSequences NAME="ChangedSequences" TYPE="Unknown" STATUS="0">
</ChangedSequences>
<UnchangedSequences NAME="UnchangedSequences" TYPE="Unknown" STATUS="0">
</UnchangedSequences>
<DeletedOprConsumableResourceLinks NAME="DeletedOprConsumableResourceLinks" TYPE="Unknown" STATUS="0">
</DeletedOprConsumableResourceLinks>
<AddedOprConsumableResourceLinks NAME="AddedOprConsumableResourceLinks" TYPE="Unknown" STATUS="0">
</AddedOprConsumableResourceLinks>
<ChangedOprConsumableResourceLinks NAME="ChangedOprConsumableResourceLinks" TYPE="Unknown" STATUS="0">
</ChangedOprConsumableResourceLinks>
<UnchangedOprConsumableResourceLinks NAME="UnchangedOprConsumableResourceLinks" TYPE="Unknown" STATUS="0">
</UnchangedOprConsumableResourceLinks>
<DeletedOprWorkCenterLinks NAME="DeletedOprWorkCenterLinks" TYPE="Unknown" STATUS="0">
</DeletedOprWorkCenterLinks>
<AddedOprWorkCenterLinks NAME="AddedOprWorkCenterLinks" TYPE="Unknown" STATUS="0">
</AddedOprWorkCenterLinks>
<ChangedOprWorkCenterLinks NAME="ChangedOprWorkCenterLinks" TYPE="Unknown" STATUS="0">
</ChangedOprWorkCenterLinks>
<UnchangedOprWorkCenterLinks NAME="UnchangedOprWorkCenterLinks" TYPE="Unknown" STATUS="0">
</UnchangedOprWorkCenterLinks>
<DeletedOprBomAllocatedParts NAME="DeletedOprBomAllocatedParts" TYPE="Unknown" STATUS="0">
</DeletedOprBomAllocatedParts>
<AddedOprBomAllocatedParts NAME="AddedOprBomAllocatedParts" TYPE="Unknown" STATUS="0">
</AddedOprBomAllocatedParts>
<ChangedOprBomAllocatedParts NAME="ChangedOprBomAllocatedParts" TYPE="Unknown" STATUS="0">
</ChangedOprBomAllocatedParts>
<UnchangedOprBomAllocatedParts NAME="UnchangedOprBomAllocatedParts" TYPE="Unknown" STATUS="0">
</UnchangedOprBomAllocatedParts>
<DeletedOprOperatedOnParts NAME="DeletedOprOperatedOnParts" TYPE="Unknown" STATUS="0">
</DeletedOprOperatedOnParts>
<AddedOprOperatedOnParts NAME="AddedOprOperatedOnParts" TYPE="Unknown" STATUS="0">
</AddedOprOperatedOnParts>
<ChangedOprOperatedOnParts NAME="ChangedOprOperatedOnParts" TYPE="Unknown" STATUS="0">
</ChangedOprOperatedOnParts>
<UnchangedOprOperatedOnParts NAME="UnchangedOprOperatedOnParts" TYPE="Unknown" STATUS="0">
</UnchangedOprOperatedOnParts>
<DeletedStandardProcedureLinks NAME="DeletedStandardProcedureLinks" TYPE="Unknown" STATUS="0">
</DeletedStandardProcedureLinks>
<AddedStandardProcedureLinks NAME="AddedStandardProcedureLinks" TYPE="Unknown" STATUS="0">
</AddedStandardProcedureLinks>
<ChangedStandardProcedureLinks NAME="ChangedStandardProcedureLinks" TYPE="Unknown" STATUS="0">
</ChangedStandardProcedureLinks>
<UnchangedStandardProcedureLinks NAME="UnchangedStandardProcedureLinks" TYPE="Unknown" STATUS="0">
</UnchangedStandardProcedureLinks>
<DeletedSkills NAME="DeletedSkills" TYPE="Unknown" STATUS="0">
</DeletedSkills>
<AddedSkills NAME="AddedSkills" TYPE="Unknown" STATUS="0">
</AddedSkills>
<ChangedSkills NAME="ChangedSkills" TYPE="Unknown" STATUS="0">
</ChangedSkills>
<UnchangedSkills NAME="UnchangedSkills" TYPE="Unknown" STATUS="0">
</UnchangedSkills>
<DeletedPlants NAME="DeletedPlants" TYPE="Unknown" STATUS="0">
</DeletedPlants>
<AddedPlants NAME="AddedPlants" TYPE="Unknown" STATUS="0">
</AddedPlants>
<ChangedPlants NAME="ChangedPlants" TYPE="Unknown" STATUS="0">
</ChangedPlants>
<UnchangedPlants NAME="UnchangedPlants" TYPE="Unknown" STATUS="0">
</UnchangedPlants>
<DeletedResourceGroups NAME="DeletedResourceGroups" TYPE="Unknown" STATUS="0">
</DeletedResourceGroups>
<AddedResourceGroups NAME="AddedResourceGroups" TYPE="Unknown" STATUS="0">
</AddedResourceGroups>
<ChangedResourceGroups NAME="ChangedResourceGroups" TYPE="Unknown" STATUS="0">
</ChangedResourceGroups>
<UnchangedResourceGroups NAME="UnchangedResourceGroups" TYPE="Unknown" STATUS="0">
</UnchangedResourceGroups>
<DeletedWorkCenters NAME="DeletedWorkCenters" TYPE="Unknown" STATUS="0">
</DeletedWorkCenters>
<AddedWorkCenters NAME="AddedWorkCenters" TYPE="Unknown" STATUS="0">
</AddedWorkCenters>
<ChangedWorkCenters NAME="ChangedWorkCenters" TYPE="Unknown" STATUS="0">
</ChangedWorkCenters>
<UnchangedWorkCenters NAME="UnchangedWorkCenters" TYPE="Unknown" STATUS="0">
</UnchangedWorkCenters>
<DeletedPlantAssignmentLinks NAME="DeletedPlantAssignmentLinks" TYPE="Unknown" STATUS="0">
</DeletedPlantAssignmentLinks>
<AddedPlantAssignmentLinks NAME="AddedPlantAssignmentLinks" TYPE="Unknown" STATUS="0">
</AddedPlantAssignmentLinks>
<ChangedPlantAssignmentLinks NAME="ChangedPlantAssignmentLinks" TYPE="Unknown" STATUS="0">
</ChangedPlantAssignmentLinks>
<UnchangedPlantAssignmentLinks NAME="UnchangedPlantAssignmentLinks" TYPE="Unknown" STATUS="0">
</UnchangedPlantAssignmentLinks>
<DeletedResourceUsageLinks NAME="DeletedResourceUsageLinks" TYPE="Unknown" STATUS="0">
</DeletedResourceUsageLinks>
<AddedResourceUsageLinks NAME="AddedResourceUsageLinks" TYPE="Unknown" STATUS="0">
</AddedResourceUsageLinks>
<ChangedResourceUsageLinks NAME="ChangedResourceUsageLinks" TYPE="Unknown" STATUS="0">
</ChangedResourceUsageLinks>
<UnchangedResourceUsageLinks NAME="UnchangedResourceUsageLinks" TYPE="Unknown" STATUS="0">
</UnchangedResourceUsageLinks>
<CurrentAUMs NAME="CurrentAUMs" TYPE="Unknown" STATUS="0">
</CurrentAUMs>
<AddedEPMRepresentations NAME="AddedEPMRepresentations" TYPE="Unknown" STATUS="0">
</AddedEPMRepresentations>
<DeletedControlCharacteristics NAME="DeletedControlCharacteristics" TYPE="Unknown" STATUS="0">
</DeletedControlCharacteristics>
<AddedControlCharacteristics NAME="AddedControlCharacteristics" TYPE="Unknown" STATUS="0">
</AddedControlCharacteristics>
<ChangedControlCharacteristics NAME="ChangedControlCharacteristics" TYPE="Unknown" STATUS="0">
</ChangedControlCharacteristics>
<UnchangedControlCharacteristics NAME="UnchangedControlCharacteristics" TYPE="Unknown" STATUS="0">
</UnchangedControlCharacteristics>
<ModelItems NAME="ModelItems" TYPE="Unknown" STATUS="0">
</ModelItems>
<DeletedQualityLinks NAME="DeletedQualityLinks" TYPE="Unknown" STATUS="0">
</DeletedQualityLinks>
<AddedQualityLinks NAME="AddedQualityLinks" TYPE="Unknown" STATUS="0">
</AddedQualityLinks>
<ChangedQualityLinks NAME="ChangedQualityLinks" TYPE="Unknown" STATUS="0">
</ChangedQualityLinks>
<UnchangedQualityLinks NAME="UnchangedQualityLinks" TYPE="Unknown" STATUS="0">
</UnchangedQualityLinks>
<DeletedOptionSets NAME="DeletedOptionSets" TYPE="Unknown" STATUS="0">
</DeletedOptionSets>
<AddedOptionSets NAME="AddedOptionSets" TYPE="Unknown" STATUS="0">
</AddedOptionSets>
<ChangedOptionSets NAME="ChangedOptionSets" TYPE="Unknown" STATUS="0">
</ChangedOptionSets>
<UnchangedOptionSets NAME="UnchangedOptionSets" TYPE="Unknown" STATUS="0">
</UnchangedOptionSets>
<DeletedOptions NAME="DeletedOptions" TYPE="Unknown" STATUS="0">
</DeletedOptions>
<AddedOptions NAME="AddedOptions" TYPE="Unknown" STATUS="0">
</AddedOptions>
<ChangedOptions NAME="ChangedOptions" TYPE="Unknown" STATUS="0">
</ChangedOptions>
<UnchangedOptions NAME="UnchangedOptions" TYPE="Unknown" STATUS="0">
</UnchangedOptions>
<DeletedChoices NAME="DeletedChoices" TYPE="Unknown" STATUS="0">
</DeletedChoices>
<AddedChoices NAME="AddedChoices" TYPE="Unknown" STATUS="0">
</AddedChoices>
<ChangedChoices NAME="ChangedChoices" TYPE="Unknown" STATUS="0">
</ChangedChoices>
<UnchangedChoices NAME="UnchangedChoices" TYPE="Unknown" STATUS="0">
</UnchangedChoices>
<DeletedOptionSetOptionLinks NAME="DeletedOptionSetOptionLinks" TYPE="Unknown" STATUS="0">
</DeletedOptionSetOptionLinks>
<AddedOptionSetOptionLinks NAME="AddedOptionSetOptionLinks" TYPE="Unknown" STATUS="0">
</AddedOptionSetOptionLinks>
<DeletedOptionSetMemberLinks NAME="DeletedOptionSetMemberLinks" TYPE="Unknown" STATUS="0">
</DeletedOptionSetMemberLinks>
<AddedOptionSetMemberLinks NAME="AddedOptionSetMemberLinks" TYPE="Unknown" STATUS="0">
</AddedOptionSetMemberLinks>
<ChangedOptionSetMemberLinks NAME="ChangedOptionSetMemberLinks" TYPE="Unknown" STATUS="0">
</ChangedOptionSetMemberLinks>
<UnchangedOptionSetMemberLinks NAME="UnchangedOptionSetMemberLinks" TYPE="Unknown" STATUS="0">
</UnchangedOptionSetMemberLinks>
<DeletedAssociatedOptionSetLinks NAME="DeletedAssociatedOptionSetLinks" TYPE="Unknown" STATUS="0">
</DeletedAssociatedOptionSetLinks>
<AddedAssociatedOptionSetLinks NAME="AddedAssociatedOptionSetLinks" TYPE="Unknown" STATUS="0">
</AddedAssociatedOptionSetLinks>
<DeletedChoiceMappableChoiceLinks NAME="DeletedChoiceMappableChoiceLinks" TYPE="Unknown" STATUS="0">
</DeletedChoiceMappableChoiceLinks>
<AddedChoiceMappableChoiceLinks NAME="AddedChoiceMappableChoiceLinks" TYPE="Unknown" STATUS="0">
</AddedChoiceMappableChoiceLinks>
<ChangedChoiceMappableChoiceLinks NAME="ChangedChoiceMappableChoiceLinks" TYPE="Unknown" STATUS="0">
</ChangedChoiceMappableChoiceLinks>
<UnchangedChoiceMappableChoiceLinks NAME="UnchangedChoiceMappableChoiceLinks" TYPE="Unknown" STATUS="0">
</UnchangedChoiceMappableChoiceLinks>
<DeletedOptionSetRuleLinks NAME="DeletedOptionSetRuleLinks" TYPE="Unknown" STATUS="0">
</DeletedOptionSetRuleLinks>
<AddedOptionSetRuleLinks NAME="AddedOptionSetRuleLinks" TYPE="Unknown" STATUS="0">
</AddedOptionSetRuleLinks>
<ChangedOptionSetRuleLinks NAME="ChangedOptionSetRuleLinks" TYPE="Unknown" STATUS="0">
</ChangedOptionSetRuleLinks>
<UnchangedOptionSetRuleLinks NAME="UnchangedOptionSetRuleLinks" TYPE="Unknown" STATUS="0">
</UnchangedOptionSetRuleLinks>
<DeletedRules NAME="DeletedRules" TYPE="Unknown" STATUS="0">
</DeletedRules>
<AddedRules NAME="AddedRules" TYPE="Unknown" STATUS="0">
</AddedRules>
<ChangedRules NAME="ChangedRules" TYPE="Unknown" STATUS="0">
</ChangedRules>
<UnchangedRules NAME="UnchangedRules" TYPE="Unknown" STATUS="0">
</UnchangedRules>
<DeletedRuleMemberLinks NAME="DeletedRuleMemberLinks" TYPE="Unknown" STATUS="0">
</DeletedRuleMemberLinks>
<AddedRuleMemberLinks NAME="AddedRuleMemberLinks" TYPE="Unknown" STATUS="0">
</AddedRuleMemberLinks>
<ChangedRuleMemberLinks NAME="ChangedRuleMemberLinks" TYPE="Unknown" STATUS="0">
</ChangedRuleMemberLinks>
<UnchangedRuleMemberLinks NAME="UnchangedRuleMemberLinks" TYPE="Unknown" STATUS="0">
</UnchangedRuleMemberLinks>
<DeletedRuleMemberActionLinks NAME="DeletedRuleMemberActionLinks" TYPE="Unknown" STATUS="0">
</DeletedRuleMemberActionLinks>
<AddedRuleMemberActionLinks NAME="AddedRuleMemberActionLinks" TYPE="Unknown" STATUS="0">
</AddedRuleMemberActionLinks>
<ChangedRuleMemberActionLinks NAME="ChangedRuleMemberActionLinks" TYPE="Unknown" STATUS="0">
</ChangedRuleMemberActionLinks>
<UnchangedRuleMemberActionLinks NAME="UnchangedRuleMemberActionLinks" TYPE="Unknown" STATUS="0">
</UnchangedRuleMemberActionLinks>
<DeletedPlantLocalizationLinks NAME="DeletedPlantLocalizationLinks" TYPE="Unknown" STATUS="0">
</DeletedPlantLocalizationLinks>
<AddedPlantLocalizationLinks NAME="AddedPlantLocalizationLinks" TYPE="Unknown" STATUS="0">
</AddedPlantLocalizationLinks>
<ChangedPlantLocalizationLinks NAME="ChangedPlantLocalizationLinks" TYPE="Unknown" STATUS="0">
</ChangedPlantLocalizationLinks>
<UnchangedPlantLocalizationLinks NAME="UnchangedPlantLocalizationLinks" TYPE="Unknown" STATUS="0">
</UnchangedPlantLocalizationLinks>
<DeletedProcessPlanLocalizationLinks NAME="DeletedProcessPlanLocalizationLinks" TYPE="Unknown" STATUS="0">
</DeletedProcessPlanLocalizationLinks>
<AddedProcessPlanLocalizationLinks NAME="AddedProcessPlanLocalizationLinks" TYPE="Unknown" STATUS="0">
</AddedProcessPlanLocalizationLinks>
<ChangedProcessPlanLocalizationLinks NAME="ChangedProcessPlanLocalizationLinks" TYPE="Unknown" STATUS="0">
</ChangedProcessPlanLocalizationLinks>
<UnchangedProcessPlanLocalizationLinks NAME="UnchangedProcessPlanLocalizationLinks" TYPE="Unknown" STATUS="0">
</UnchangedProcessPlanLocalizationLinks>
</wc:COLLECTION>

我做的事情是:

import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;


public class StaxParserDemo 
{

    public static void main(String[] args) throws XMLStreamException 
    {
        List<WcCOLLECTION> empList = null;
        WcCOLLECTION currEmp = null;
        String tagContent = null;
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = 
            factory.createXMLStreamReader(
            ClassLoader.getSystemResourceAsStream("c:\\file.xml"));

        while(reader.hasNext()){
          int event = reader.next();

          switch(event){
            case XMLStreamConstants.START_ELEMENT: 
              if ("Part".equals(reader.getLocalName())){
                currEmp = new WcCOLLECTION();
                currEmp.ObjectID = reader.getAttributeValue(0);
              }
              if("Part".equals(reader.getLocalName())){
                empList = new ArrayList<>();
              }
              break;

            case XMLStreamConstants.CHARACTERS:
              tagContent = reader.getText().trim();
              break;

            case XMLStreamConstants.END_ELEMENT:
              switch(reader.getLocalName()){
                case "Part":
                  empList.add(currEmp);
                  break;
                case "ObjectID":
                  currEmp.ObjectID = tagContent;
                  break;
                case "Class":
                  currEmp.Class = tagContent;
                  break;
                case "LastChangedBy":
                  currEmp.LastChangedBy = tagContent;
                  break;
                case "StartEffectivity":
                      currEmp.StartEffectivity = tagContent;
                      break;
              }
              break;

            case XMLStreamConstants.START_DOCUMENT:
              empList = new ArrayList<>();
              break;
          }

        }

        //Print the employee list populated from XML
        for ( WcCOLLECTION emp : empList){
          System.out.println(emp);
        }

      }
    }

    class WcCOLLECTION
    {
        String ObjectID;
        String Class;
        String LastChangedBy;
        String Number;
        String StartEffectivity;
        String EndEffectivity;
        String StartSerialNumberEffectivity;
        String EndSerialNumberEffectivity;
        String StartLotNumberEffectivity;
        String EndLotNumberEffectivity;
        String SerialNumberEffectivityCxtPartNumber;
        String LotNumberEffectivityCxtPartNumber;
        String DefaultUnit;
        String Name;
        String PartType;
        String Source;
        String State;
        String IsPhantom;
        String Version;
        String Iteration;
        String PreviousVersion;
        String IsConfigurable;
        String IsCollapsible;
        String TargetID;

      @Override
      public String toString()
      {
        //return Class+" "+LastChangedBy+"("+ObjectID+") "+StartEffectivity+" "+EndEffectivity+" "+StartSerialNumberEffectivity+" "+EndSerialNumberEffectivity+" "+SerialNumberEffectivityCxtPartNumber+" "+LotNumberEffectivityCxtPartNumber+" "+DefaultUnit+" "+Name+" "+PartType+" "+Source+" "+State+" "+IsPhantom+" "+State+" "+Version+" "+Iteration+" "+PreviousVersion+" "+IsConfigurable+" "+IsCollapsible+" "+TargetID;

          return Class+" "+LastChangedBy+"("+ObjectID+") "+StartEffectivity;
      }

}

2 个答案:

答案 0 :(得分:1)

我编写了一些java代码,它解析了XML使用STAX Parser,它运行正常:

public class ErpStAXParser 
{
    public static void main(String[] args) 
    {

        // all the required attributes from the xml file which will need to print 
          boolean ObjectID = false;
          boolean Class = false;
          boolean LastChangedBy = false;
          boolean Number = false;
          boolean StartEffectivity = false;
          boolean EndEffectivity = false;
          boolean StartSerialNumberEffectivity = false;
          boolean EndSerialNumberEffectivity = false;
          boolean StartLotNumberEffectivity = false;
          boolean EndLotNumberEffectivity = false;
          boolean SerialNumberEffectivityCxtPartNumber = false;
          boolean LotNumberEffectivityCxtPartNumber = false;
          boolean DefaultUnit = false;
          boolean Name = false;
          boolean PartType = false;
          boolean Source = false;
          boolean State = false;
          boolean IsPhantom = false;
          boolean Version = false;
          boolean Iteration = false;
          boolean PreviousVersion = false;
          boolean IsConfigurable = false;
          boolean IsCollapsible = false;
          boolean TargetID = false;

          try 
          {
                 XMLInputFactory factory = XMLInputFactory.newInstance();
                 XMLEventReader eventReader =
                 factory.createXMLEventReader(
                    new FileReader("c:\\staxTestFile.xml"));
                 while(eventReader.hasNext())
                 {
                       XMLEvent event = eventReader.nextEvent();
                       switch(event.getEventType())
                       {
                          case XMLStreamConstants.START_ELEMENT:
                              StartElement startElement = event.asStartElement();
                                 String qName = startElement.getName().getLocalPart();
                                 if (qName.equalsIgnoreCase("Part")) 
                                 {
                                        System.out.println("Start Element : Part");
                                        Iterator<Attribute> attributes = startElement.getAttributes();

                                 }
                                 else if (qName.equalsIgnoreCase("ObjectID")) 
                                 {
                                     ObjectID = true;
                                 } else if (qName.equalsIgnoreCase("Class")) 
                                 {
                                     Class = true;
                                 } else if (qName.equalsIgnoreCase("LastChangedBy")) 
                                 {
                                     LastChangedBy = true;
                                 }
                                 else if (qName.equalsIgnoreCase("Number")) 
                                 {
                                     Number = true;
                                 }
                                 else if (qName.equalsIgnoreCase("StartEffectivity")) 
                                 {
                                     StartEffectivity = true;
                                 } else if (qName.equalsIgnoreCase("EndSerialNumberEffectivity")) 
                                 {
                                     EndSerialNumberEffectivity = true;
                                 } else if (qName.equalsIgnoreCase("StartSerialNumberEffectivity")) 
                                 {
                                     StartSerialNumberEffectivity = true;
                                 }
                                 else if (qName.equalsIgnoreCase("EndSerialNumberEffectivity")) 
                                 {
                                     EndSerialNumberEffectivity = true;
                                 }
                                 else if (qName.equalsIgnoreCase("StartLotNumberEffectivity")) 
                                 {
                                     StartLotNumberEffectivity = true;
                                 } else if (qName.equalsIgnoreCase("EndLotNumberEffectivity")) 
                                 {
                                     EndLotNumberEffectivity = true;
                                 } else if (qName.equalsIgnoreCase("SerialNumberEffectivityCxtPartNumber")) 
                                 {
                                     SerialNumberEffectivityCxtPartNumber = true;
                                 }
                                 else if (qName.equalsIgnoreCase("LotNumberEffectivityCxtPartNumber")) 
                                 {
                                     LotNumberEffectivityCxtPartNumber = true;
                                 }
                                 else if (qName.equalsIgnoreCase("DefaultUnit")) 
                                 {
                                     DefaultUnit = true;
                                 } else if (qName.equalsIgnoreCase("Name")) 
                                 {
                                     Name = true;
                                 } else if (qName.equalsIgnoreCase("PartType")) 
                                 {
                                     PartType = true;
                                 }
                                 else if (qName.equalsIgnoreCase("Source")) 
                                 {
                                     Source = true;
                                 }
                                 else if (qName.equalsIgnoreCase("State")) 
                                 {
                                     State = true;
                                 } else if (qName.equalsIgnoreCase("IsPhantom")) 
                                 {
                                     IsPhantom = true;
                                 } else if (qName.equalsIgnoreCase("Version")) 
                                 {
                                     Version = true;
                                 }
                                 else if (qName.equalsIgnoreCase("Iteration")) 
                                 {
                                     Iteration = true;
                                 }
                                 else if (qName.equalsIgnoreCase("PreviousVersion")) 
                                 {
                                     PreviousVersion = true;
                                 } else if (qName.equalsIgnoreCase("IsConfigurable")) 
                                 {
                                     IsConfigurable = true;
                                 } else if (qName.equalsIgnoreCase("IsCollapsible")) 
                                 {
                                     IsCollapsible = true;
                                 }
                                 else if (qName.equalsIgnoreCase("TargetID")) 
                                 {
                                     TargetID = true;
                                 }
                                 break;

                          case XMLStreamConstants.CHARACTERS:
                          Characters characters = event.asCharacters();

                          if(ObjectID){
                                    System.out.println("ObjectID: " 
                                    + characters.getData());
                                    ObjectID = false;
                                 }
                                 if(Class){
                                    System.out.println("Class: " 
                                    + characters.getData());
                                    Class = false;
                                 }
                                 if(LastChangedBy){
                                    System.out.println("LastChangedBy " 
                                    + characters.getData());
                                    LastChangedBy = false;
                                 }
                                 if(Number){
                                    System.out.println("Number: " 
                                    + characters.getData());
                                    Number = false;
                                 }
                                 if(StartEffectivity){
                                        System.out.println("StartEffectivity: " 
                                        + characters.getData());
                                        StartEffectivity = false;
                                     }
                                     if(EndEffectivity){
                                        System.out.println("EndEffectivity " 
                                        + characters.getData());
                                        EndEffectivity = false;
                                     }
                                     if(StartSerialNumberEffectivity){
                                        System.out.println("StartSerialNumberEffectivity " 
                                        + characters.getData());
                                        StartSerialNumberEffectivity = false;
                                     }
                                     if(EndSerialNumberEffectivity){
                                        System.out.println("EndSerialNumberEffectivity: " 
                                        + characters.getData());
                                        EndSerialNumberEffectivity = false;
                                     }
                                     if(StartLotNumberEffectivity){
                                            System.out.println("StartLotNumberEffectivity" 
                                            + characters.getData());
                                            StartLotNumberEffectivity = false;
                                         }
                                         if(EndLotNumberEffectivity){
                                            System.out.println("EndLotNumberEffectivity: " 
                                            + characters.getData());
                                            EndLotNumberEffectivity = false;
                                         }
                                         if(SerialNumberEffectivityCxtPartNumber){
                                            System.out.println("SerialNumberEffectivityCxtPartNumber " 
                                            + characters.getData());
                                            SerialNumberEffectivityCxtPartNumber = false;
                                         }
                                         if(LotNumberEffectivityCxtPartNumber){
                                            System.out.println("LotNumberEffectivityCxtPartNumber: " 
                                            + characters.getData());
                                            LotNumberEffectivityCxtPartNumber = false;
                                         }
                                         if(DefaultUnit){
                                                System.out.println("DefaultUnit: " 
                                                + characters.getData());
                                                DefaultUnit = false;
                                             }
                                             if(Name){
                                                System.out.println("Name: " 
                                                + characters.getData());
                                                Name = false;
                                             }
                                             if(PartType){
                                                System.out.println("PartType: " 
                                                + characters.getData());
                                                PartType = false;
                                             }
                                             if(Source){
                                                System.out.println("Source: " 
                                                + characters.getData());
                                                Source = false;
                                             }
                                             if(State){
                                                    System.out.println("State: " 
                                                    + characters.getData());
                                                    State = false;
                                                 }
                                                 if(IsPhantom){
                                                    System.out.println("IsPhantom: " 
                                                    + characters.getData());
                                                    IsPhantom = false;
                                                 }
                                                 if(Version){
                                                    System.out.println("Version: " 
                                                    + characters.getData());
                                                    Version = false;
                                                 }
                                                 if(Iteration){
                                                    System.out.println("Iteration: " 
                                                    + characters.getData());
                                                    Iteration = false;
                                                 }
                                                 if(PreviousVersion){
                                                        System.out.println("PreviousVersion: " 
                                                        + characters.getData());
                                                        PreviousVersion = false;
                                                     }
                                                     if(IsConfigurable){
                                                        System.out.println("IsConfigurable: " 
                                                        + characters.getData());
                                                        IsConfigurable = false;
                                                     }
                                                     if(IsCollapsible){
                                                        System.out.println("IsCollapsible: " 
                                                        + characters.getData());
                                                        IsCollapsible = false;
                                                     }
                                                     if(TargetID){
                                                        System.out.println("TargetID: " 
                                                        + characters.getData());
                                                        TargetID = false;
                                                     }
                                                     break;
                          case  XMLStreamConstants.END_ELEMENT:
                                 EndElement endElement = event.asEndElement();
                                 if(endElement.getName().getLocalPart().equalsIgnoreCase("Part")){
                                        System.out.println("End Element : Part");
                                        System.out.println();
                                     }
                                     break;
                       }//switch



          }//while
          }//try
          catch(Exception e)
          {
              e.printStackTrace();
          }




}//main
}//class

答案 1 :(得分:1)

以下是基于XPath和VTD-XML的代码示例。它会将所有元素及其内容转储到零件节点下。虽然它会将所有内容加载到内存中,但它不会像DOM一样爆炸。

import com.ximpleware.*;

public class dumpKeyValue {

    public static void main(String[] s) throws VTDException{
        VTDGen vg = new VTDGen();
        AutoPilot ap = new AutoPilot();
        ap.selectXPath("//part/*[text()]");
        if (!vg.parseFile("d:\\xml\\soap.xml", false)){
            System.out.println("invalid xml");
            System.exit(0);
        }
        VTDNav vn = vg.getNav();
        ap.bind(vn);
        int i=0;
        while((i=ap.evalXPath())!=-1){
            System.out.println(" "+vn.toRawString(i)+" = "+vn.toNormalizedString(vn.getText()));
        }
    }
}