需要使用java从xml中提取特定标记及其值

时间:2013-02-08 16:53:03

标签: java xml-parsing

大家好我是Java的新蜜蜂 需要从xml中提取标签及其值

代码:

Document doc = loadXml("data.xml");  
NodeList nodeList = doc.getElementsByTagName("CreateResponse");  
for (int i = 0; i < nodeList.getLength(); i++) {  
    Node node = nodeList.item(i);  
    if (node.getNodeType() == Node.ELEMENT_NODE) {  
        Element element = (Element) node;  
        NodeList ROWList = element.getElementsByTagName("Row");  
        Element weekElement = (Element) RowList.item(0);  
        NodeList textElementList = weekElement.getChildNodes();
        System.out.println("Row:"+((Node)textElementList.item(0)).getNodeValue().trim());
}

以下是我的输入

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"          
               xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <CreateResponse xmlns="http://jerseytelecom.com/">
     <CreateResult> 
     <ISD_XMLGateway>
         <Entity>NGN_LPI</Entity> 
         <Params>
         <Param Name="ResponseCode" Value="0" /> 
      <Param Name="ResponseText" Value="OK" /> 
        <Param Name="Response" Value="NGN_LPI_Response">
        <NGN_LPI>
      <Response>
       <Table>
        <Row>
   <RouteRecordID>xxxxx</RouteRecordID> 
   <LineTypeID>xxxxxx</LineTypeID> 
   <RouteNodeElementID>xxxxxxx</RouteNodeElementID> 
   <RouteNodeElementPosition>xx</RouteNodeElementPosition> 
   <RouteNodeElementBlock /> 
   <RouteNodeID>xxxxxx</RouteNodeID> 
   <RouteNodeElementTypeCode>xxx</RouteNodeElementTypeCode> 
   <Circuit>xxxxxx</Circuit> 
   <Suffix>x</Suffix> 
   <SortOrder>x</SortOrder> 
   <RouteNodeElementTypeID>x</RouteNodeElementTypeID> 
   <RouteOrder>x</RouteOrder> 
   <NodeID>xxxxxxxxxxx xxx x</NodeID> 
   <ParentNodeTypeID>x</ParentNodeTypeID> 
   <StatusID>x</StatusID> 
   <RouteStatusID>x</RouteStatusID> 
   <ProcessorID /> 
   <ExchangeID>x</ExchangeID> 
   <SchemeID /> 
   <SchemeNumber /> 
   <RouteConditionID>x</RouteConditionID> 
   <Spare>xxxxx</Spare> 
   <DesignationID>x</DesignationID> 
   <Organisation /> 
   <AddressLine1>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</AddressLine1> 
   <AddressLine2>xxxxxxxxxxxxx</AddressLine2> 
   <AddressLine3>xxxxxxxxxxxx</AddressLine3> 
   <AddressLine4>xxxxxxxx</AddressLine4> 
   <AddressLine5>xxxxxx</AddressLine5> 
   <PostCode>xxxxxxx</PostCode> 
   <AddressID>xxxxxx</AddressID> 
   <CustomerID>xxxx</CustomerID> 
   <CustomerOrganisation /> 
   <Title>x</Title> 
   <Forename /> 
   <Surname /> 
   <CircuitID>xxxxxx</CircuitID> 
   <CableID /> 
   <CableIDIn>x</CableIDIn> 
   <DASS>xxxxx</DASS> 
   <RouteTypeID>x</RouteTypeID> 
   <ChildNodeID>xxxxxxx</ChildNodeID> 
   <ChildExchangeID>x</ChildExchangeID> 
   <ChildRouteNodeElementTypeID>x</ChildRouteNodeElementTypeID> 
   <ChildNodeTypeID>xx</ChildNodeTypeID> 
   <CAMIVPositionID>xxxxxx</CAMIVPositionID> 
   <MDFSuiteID>x</MDFSuiteID> 
   <MDFModuleID>x</MDFModuleID> 
   <ModuleNumber>x</ModuleNumber> 
   <Shelf>x</Shelf> 
   <Slot>x</Slot> 
   <Position>xx</Position> 
   <SuiteName /> 
   <TiePairID>x</TiePairID> 
   <LCT>x</LCT> 
   <ConnectionStatusID>x</ConnectionStatusID> 
   <CeaseDate>dd/mm/yyyy hh:mi:ss</CeaseDate> 
   <FaultTypeID>x</FaultTypeID> 
   <CAMIVPositionIDOut>x</CAMIVPositionIDOut> 
   <MDFSuiteIDOut /> 
   <MDFModuleIDOut /> 
   <ModuleNumberOut /> 
   <ShelfOut /> 
   <SlotOut /> 
   <PositionOut /> 
   <SuiteNameOut /> 
   <CableNodeIDOut /> 
   <LoopResistanceOut /> 
   <DBLossOut /> 
   <PairCountOut /> 
   <FaultyPairCountOut /> 
   <CurrentPairUsageOut /> 
   <CableNodeIDIn /> 
   <LoopResistanceIn /> 
   <DBLossIn /> 
   <PairCountIn /> 
   <FaultyPairCountIn /> 
   <CurrentPairUsageIn /> 
   <HouseNumber /> 
   <WorkflowPending>xxxxx</WorkflowPending> 
   <OrderNumber>xxxxxxx</OrderNumber> 
   <EquipmentTypeID>xxxx</EquipmentTypeID> 
   <StrowgerVertIn>x</StrowgerVertIn> 
   <StrowgerVertOut /> 
   <JobListID>x</JobListID> 
   <ShortCircuit>xxxxxx</ShortCircuit> 
   <CablePair>x</CablePair> 
   <FibreModule>x</FibreModule> 
   <FibreShelf>x</FibreShelf> 
   <SchemeStatusID /> 
   <ABSDSB>xx</ABSDSB> 
   <RequiredDate>dd/mm/yyyy hh:mi:ss</RequiredDate> 
   <ParentExchangeID>x</ParentExchangeID> 
   <ElementExchangeID>x</ElementExchangeID> 
  </Row>
  <Row>
   <RouteRecordID>xxxxx</RouteRecordID> 
   <LineTypeID>xxxxxx</LineTypeID> 
   <RouteNodeElementID>xxxxxxx</RouteNodeElementID> 
   <RouteNodeElementPosition>xxx</RouteNodeElementPosition> 
   <RouteNodeElementBlock>x</RouteNodeElementBlock> 
   <RouteNodeID>xxxxxx</RouteNodeID> 
   <RouteNodeElementTypeCode>xxx</RouteNodeElementTypeCode> 
   <Circuit>xxxxxx</Circuit> 
   <Suffix>x</Suffix> 
   <SortOrder>x</SortOrder> 
   <RouteNodeElementTypeID>x</RouteNodeElementTypeID> 
   <RouteOrder>x</RouteOrder> 
   <NodeID>xxxxxxx</NodeID> 
   <ParentNodeTypeID>x</ParentNodeTypeID> 
   <StatusID>x</StatusID> 
   <RouteStatusID>x</RouteStatusID> 
   <ProcessorID /> 
   <ExchangeID>x</ExchangeID> 
   <SchemeID /> 
   <SchemeNumber /> 
   <RouteConditionID>x</RouteConditionID> 
   <Spare>xxxxx</Spare> 
   <DesignationID>x</DesignationID> 
   <Organisation /> 
   <AddressLine1>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</AddressLine1> 
   <AddressLine2>xxxxxxxxxxxxxxxxx</AddressLine2> 
   <AddressLine3>xxxxxxxxxxxxxxxx</AddressLine3> 
   <AddressLine4>xxxxxxxxxx</AddressLine4> 
   <AddressLine5>xxxxxx</AddressLine5> 
   <PostCode>xxxxxxx</PostCode> 
   <AddressID>xxxxxx</AddressID> 
   <CustomerID>xxxx</CustomerID> 
   <CustomerOrganisation /> 
   <Title>x</Title> 
   <Forename /> 
   <Surname /> 
   <CircuitID>xxxxxx</CircuitID> 
   <CableID>xxxxxx</CableID> 
   <CableIDIn>x</CableIDIn> 
   <DASS>xxxxx</DASS> 
   <RouteTypeID>x</RouteTypeID> 
   <ChildNodeID /> 
   <ChildExchangeID>x</ChildExchangeID> 
   <ChildRouteNodeElementTypeID>x</ChildRouteNodeElementTypeID> 
   <ChildNodeTypeID>x</ChildNodeTypeID> 
   <CAMIVPositionID>x</CAMIVPositionID> 
   <MDFSuiteID /> 
   <MDFModuleID /> 
   <ModuleNumber /> 
   <Shelf /> 
   <Slot /> 
   <Position /> 
   <SuiteName /> 
   <TiePairID>x</TiePairID> 
   <LCT>x</LCT> 
   <ConnectionStatusID>x</ConnectionStatusID> 
   <CeaseDate>dd/mm/yyyy hh:mi:ss</CeaseDate> 
   <FaultTypeID>x</FaultTypeID> 
   <CAMIVPositionIDOut>x</CAMIVPositionIDOut> 
   <MDFSuiteIDOut /> 
   <MDFModuleIDOut /> 
   <ModuleNumberOut /> 
   <ShelfOut /> 
   <SlotOut /> 
   <PositionOut /> 
   <SuiteNameOut /> 
   <CableNodeIDOut>xxxxx</CableNodeIDOut> 
   <LoopResistanceOut>xxxxxxxxxxxxxxx</LoopResistanceOut> 
   <DBLossOut>xxxxxxxxxxxxxxxxxxxx</DBLossOut> 
   <PairCountOut>xxx</PairCountOut> 
   <FaultyPairCountOut>x</FaultyPairCountOut> 
   <CurrentPairUsageOut>xxx</CurrentPairUsageOut> 
   <CableNodeIDIn /> 
   <LoopResistanceIn /> 
   <DBLossIn /> 
   <PairCountIn /> 
   <FaultyPairCountIn /> 
   <CurrentPairUsageIn /> 
   <HouseNumber /> 
   <WorkflowPending>xxxxx</WorkflowPending> 
   <OrderNumber>xxxxxxx</OrderNumber> 
   <EquipmentTypeID>x</EquipmentTypeID> 
   <StrowgerVertIn /> 
   <StrowgerVertOut /> 
   <JobListID>x</JobListID> 
   <ShortCircuit>xxxxxx</ShortCircuit> 
   <CablePair>x</CablePair> 
   <FibreModule>x</FibreModule> 
   <FibreShelf>x</FibreShelf> 
   <SchemeStatusID /> 
   <ABSDSB>x</ABSDSB> 
   <RequiredDate>dd/mm/yyyy hh:mi:ss</RequiredDate> 
   <ParentExchangeID>x</ParentExchangeID> 
   <ElementExchangeID>x</ElementExchangeID> 
  </Row>
  <Row>
   <RouteRecordID>xxxxx</RouteRecordID> 
   <LineTypeID>xxxxxx</LineTypeID> 
   <RouteNodeElementID>xxxxxxx</RouteNodeElementID> 
   <RouteNodeElementPosition>xxx</RouteNodeElementPosition> 
   <RouteNodeElementBlock>x</RouteNodeElementBlock> 
   <RouteNodeID>xxxxxx</RouteNodeID> 
   <RouteNodeElementTypeCode>xxxxx</RouteNodeElementTypeCode> 
   <Circuit>xxxxxx</Circuit> 
   <Suffix>x</Suffix> 
   <SortOrder>x</SortOrder> 
   <RouteNodeElementTypeID>xx</RouteNodeElementTypeID> 
   <RouteOrder>x</RouteOrder> 
   <NodeID>xxxxxxxx</NodeID> 
   <ParentNodeTypeID>xx</ParentNodeTypeID> 
   <StatusID>x</StatusID> 
   <RouteStatusID>x</RouteStatusID> 
   <ProcessorID /> 
   <ExchangeID>x</ExchangeID> 
   <SchemeID /> 
   <SchemeNumber /> 
   <RouteConditionID>x</RouteConditionID> 
   <Spare>xxxxx</Spare> 
   <DesignationID>x</DesignationID> 
   <Organisation /> 
   <AddressLine1>xxxxxxxxxxxxxxxxxxxxxxxxxxxx</AddressLine1> 
   <AddressLine2>xxxxxxxxxxxxxxxx</AddressLine2> 
   <AddressLine3>xxxxxxxxxxxxxxx</AddressLine3> 
   <AddressLine4>xxxxxxxxxx</AddressLine4> 
   <AddressLine5>xxxxxx</AddressLine5> 
   <PostCode>xxxxxxx</PostCode> 
   <AddressID>xxxxxx</AddressID> 
   <CustomerID>xxxx</CustomerID> 
   <CustomerOrganisation /> 
   <Title>x</Title> 
   <Forename /> 
   <Surname /> 
   <CircuitID>xxxxxx</CircuitID> 
   <CableID>xxxxxx</CableID> 
   <CableIDIn>xxxxxx</CableIDIn> 
   <DASS>xxxxx</DASS> 
   <RouteTypeID>x</RouteTypeID> 
   <ChildNodeID>xxxxxxx</ChildNodeID> 
   <ChildExchangeID>x</ChildExchangeID> 
   <ChildRouteNodeElementTypeID>x</ChildRouteNodeElementTypeID> 
   <ChildNodeTypeID>x</ChildNodeTypeID> 
   <CAMIVPositionID>x</CAMIVPositionID> 
   <MDFSuiteID /> 
   <MDFModuleID /> 
   <ModuleNumber /> 
   <Shelf /> 
   <Slot /> 
   <Position /> 
   <SuiteName /> 
   <TiePairID>x</TiePairID> 
   <LCT>x</LCT> 
   <ConnectionStatusID>x</ConnectionStatusID> 
   <CeaseDate>dd/mm/yyyy hh:mi:ss</CeaseDate> 
   <FaultTypeID>x</FaultTypeID> 
   <CAMIVPositionIDOut>x</CAMIVPositionIDOut> 
   <MDFSuiteIDOut /> 
   <MDFModuleIDOut /> 
   <ModuleNumberOut /> 
   <ShelfOut /> 
   <SlotOut /> 
   <PositionOut /> 
   <SuiteNameOut /> 
   <CableNodeIDOut>xxxxx</CableNodeIDOut> 
   <LoopResistanceOut>xxxxxxxxxxxxxxxxxx</LoopResistanceOut> 
   <DBLossOut>xxxxxxxxxxxxxxxxxxxxx</DBLossOut> 
   <PairCountOut>xxx</PairCountOut> 
   <FaultyPairCountOut>x</FaultyPairCountOut> 
   <CurrentPairUsageOut>xxx</CurrentPairUsageOut> 
   <CableNodeIDIn>xxxxx</CableNodeIDIn> 
   <LoopResistanceIn>xxxxxxxxxxxxxxxxxxx</LoopResistanceIn> 
   <DBLossIn>xxxxxxxxxxxxxxxxxxxxx</DBLossIn> 
   <PairCountIn>xxx</PairCountIn> 
   <FaultyPairCountIn>x</FaultyPairCountIn> 
   <CurrentPairUsageIn>xxx</CurrentPairUsageIn> 
   <HouseNumber /> 
   <WorkflowPending>xxxxx</WorkflowPending> 
   <OrderNumber>xxxxxxx</OrderNumber> 
   <EquipmentTypeID>x</EquipmentTypeID> 
   <StrowgerVertIn /> 
   <StrowgerVertOut /> 
   <JobListID>x</JobListID> 
   <ShortCircuit>xxxxxx</ShortCircuit> 
   <CablePair>x</CablePair> 
   <FibreModule>x</FibreModule> 
   <FibreShelf>x</FibreShelf> 
   <SchemeStatusID /> 
   <ABSDSB>x</ABSDSB> 
   <RequiredDate>dd/mm/yyyy hh:mi:ss</RequiredDate> 
   <ParentExchangeID>x</ParentExchangeID> 
   <ElementExchangeID>x</ElementExchangeID> 
  </Row>
  <Row>
   <RouteRecordID>xxxxx</RouteRecordID> 
   <LineTypeID>xxxxx</LineTypeID> 
   <RouteNodeElementID>xxxxxxx</RouteNodeElementID> 
   <RouteNodeElementPosition>xxx</RouteNodeElementPosition> 
   <RouteNodeElementBlock /> 
   <RouteNodeID>xxxxxx</RouteNodeID> 
   <RouteNodeElementTypeCode>xx</RouteNodeElementTypeCode> 
   <Circuit>xxxxxx</Circuit> 
   <Suffix>x</Suffix> 
   <SortOrder>x</SortOrder> 
   <RouteNodeElementTypeID>x</RouteNodeElementTypeID> 
   <RouteOrder>x</RouteOrder> 
   <NodeID>xxxx</NodeID> 
   <ParentNodeTypeID>x</ParentNodeTypeID> 
   <StatusID>x</StatusID> 
   <RouteStatusID>x</RouteStatusID> 
   <ProcessorID /> 
   <ExchangeID>x</ExchangeID> 
   <SchemeID /> 
   <SchemeNumber /> 
   <RouteConditionID>x</RouteConditionID> 
   <Spare>xxxxx</Spare> 
   <DesignationID>x</DesignationID> 
   <Organisation /> 
   <AddressLine1>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</AddressLine1> 
   <AddressLine2>xxxxxxxxxxxxxxxxx</AddressLine2> 
   <AddressLine3>xxxxxxxxxxxxxxx</AddressLine3> 
   <AddressLine4>xxxxxx</AddressLine4> 
   <AddressLine5>xxxxxx</AddressLine5> 
   <PostCode>xxxxxxx</PostCode> 
   <AddressID>xxxxxx</AddressID> 
   <CustomerID>xxxx</CustomerID> 
   <CustomerOrganisation /> 
   <Title>x</Title> 
   <Forename /> 
   <Surname /> 
   <CircuitID>xxxxxx</CircuitID> 
   <CableID /> 
   <CableIDIn>xxxxxx</CableIDIn> 
   <DASS>xxxxx</DASS> 
   <RouteTypeID>x</RouteTypeID> 
   <ChildNodeID /> 
   <ChildExchangeID>x</ChildExchangeID> 
   <ChildRouteNodeElementTypeID>x</ChildRouteNodeElementTypeID> 
   <ChildNodeTypeID>x</ChildNodeTypeID> 
   <CAMIVPositionID>x</CAMIVPositionID> 
   <MDFSuiteID /> 
   <MDFModuleID /> 
   <ModuleNumber /> 
   <Shelf /> 
   <Slot /> 
   <Position /> 
   <SuiteName /> 
   <TiePairID>x</TiePairID> 
   <LCT>x</LCT> 
   <ConnectionStatusID>x</ConnectionStatusID> 
   <CeaseDate>dd/mm/yyyy hh:mi:ss</CeaseDate> 
   <FaultTypeID>x</FaultTypeID> 
   <CAMIVPositionIDOut>x</CAMIVPositionIDOut> 
   <MDFSuiteIDOut /> 
   <MDFModuleIDOut /> 
   <ModuleNumberOut /> 
   <ShelfOut /> 
   <SlotOut /> 
   <PositionOut /> 
   <SuiteNameOut /> 
   <CableNodeIDOut /> 
   <LoopResistanceOut /> 
   <DBLossOut /> 
   <PairCountOut /> 
   <FaultyPairCountOut /> 
   <CurrentPairUsageOut /> 
   <CableNodeIDIn>xxxxx</CableNodeIDIn> 
   <LoopResistanceIn>xxxxxxxxxxxxxxxxxxxx</LoopResistanceIn> 
   <DBLossIn>xxxxxxxxxxxxxxxxx</DBLossIn> 
   <PairCountIn>xxx</PairCountIn> 
   <FaultyPairCountIn>x</FaultyPairCountIn> 
   <CurrentPairUsageIn>xxx</CurrentPairUsageIn> 
   <HouseNumber /> 
   <WorkflowPending>xxxxx</WorkflowPending> 
   <OrderNumber>xxxxxxx</OrderNumber> 
   <EquipmentTypeID>x</EquipmentTypeID> 
   <StrowgerVertIn /> 
   <StrowgerVertOut /> 
   <JobListID>x</JobListID> 
   <ShortCircuit>xxxxxx</ShortCircuit> 
   <CablePair>x</CablePair> 
   <FibreModule>x</FibreModule> 
   <FibreShelf>x</FibreShelf> 
   <SchemeStatusID /> 
   <ABSDSB>x</ABSDSB> 
   <RequiredDate>dd/mm/yyyy hh:mi:ss</RequiredDate> 
   <ParentExchangeID>x</ParentExchangeID> 
   <ElementExchangeID>x</ElementExchangeID> 
    </Row>
     </Table>
       </Response>
      <ResponseCode>0</ResponseCode> 
      <ResponseText>Ok</ResponseText> 
    </NGN_LPI>
     </Param>
    </Params>
    </ISD_XMLGateway>
   </CreateResult> 
        </CreateResponse>
        </soap:Body>
        </soap:Envelope>.

我需要使用所需的标签及其值示例

来扩展xml
             <Slot>XXXXX</Slot><Shelf>yyyy</Shelf>

我尝试使用其中一个frnds建议的下面的代码,但它没有达到我的目的

1 个答案:

答案 0 :(得分:1)

尝试使用XPath:

XPathExpression expr = xpath.compile("//*[self::Slot or self::Shelf]");
NodeList nodeList = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i=0; i<nodeList.getLength(); i++) {
  Node node = nodeList.item(i);
  node.getLocalName(); // The element name, e.g. "Slot"
  node.getTextContent(); // The element text content, e.g. "XXXXX"
  // ...
}