2013-10-28 117 views
2

我有一個webservice,它以下面的格式返回XML。我使用XML :: LibXML來解析輸出。使用perl XML解析XML :: LibXML

<QueryResponse xmlns="http://www.exchangenetwork.net/schema/node/2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <LastSet>true</LastSet> 
    <Results> 
     <SRS:SubstanceInformation xsi:schemaLocation="http://www.exchangenetwork.net/schema/SRS/3 http://www.exchangenetwork.net/schema/SRS/3" xmlns:SRS="http://www.exchangenetwork.net/schema/SRS/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
      <SRS:ChemicalSubstance> 
       <SRS:ChemicalSubstanceIdentification> 
        <SRS:EPAChemicalInternalNumber>76109</SRS:EPAChemicalInternalNumber> 
        <SRS:CASRegistryNumber>1000-82-4</SRS:CASRegistryNumber> 
        <SRS:ChemicalSubstanceSystematicName>Urea, N-(hydroxymethyl)-</SRS:ChemicalSubstanceSystematicName> 
        <SRS:EPAChemicalRegistryName>Methylolurea</SRS:EPAChemicalRegistryName> 
        <SRS:EPAChemicalIdentifier/> 
        <SRS:ChemicalSubstanceDefinitionText/> 
        <SRS:ChemicalSubstanceCommentText/> 
        <SRS:MolecularFormulaCode>C2H6N2O2</SRS:MolecularFormulaCode> 
        <SRS:ChemicalSubstanceFormulaWeightQuantity>90.08</SRS:ChemicalSubstanceFormulaWeightQuantity> 
        <SRS:ChemicalSubstanceLinearStructureCode>O=C(NCO)N</SRS:ChemicalSubstanceLinearStructureCode> 
        <SRS:InternationalChemicalIdentifier/> 
        <SRS:FormerCASRegistryNumberList/> 
        <SRS:IncorrectlyUsedCASRegistryNumberList> 
         <SRS:CASRegistryNumber>50-00-0</SRS:CASRegistryNumber> 
        </SRS:IncorrectlyUsedCASRegistryNumberList> 
        <SRS:ClassificationList/> 
        <SRS:TechnicalPointOfContact/> 
        <SRS:SubstanceRequestor/> 
        <SRS:SubstanceCreateDate>2006-10-13 14:30:12.0</SRS:SubstanceCreateDate> 
        <SRS:SubstanceLastUpdateDate>2010-01-20 12:29:21.0</SRS:SubstanceLastUpdateDate> 
        <SRS:SubstanceStatus>A</SRS:SubstanceStatus> 
       </SRS:ChemicalSubstanceIdentification> 
       <SRS:ChemicalSubstanceSynonymList> 
        <SRS:ChemicalSubstanceSynonym> 
         <SRS:ChemicalSubstanceSynonymName>Urea, (hydroxymethyl)-</SRS:ChemicalSubstanceSynonymName> 
         <SRS:ChemicalSynonymStatusName>Reviewed</SRS:ChemicalSynonymStatusName> 
         <SRS:ChemicalSynonymSourceName>Chemical Update System (CUS) 1986</SRS:ChemicalSynonymSourceName> 
         <SRS:RegulationReasonText/> 
         <SRS:CharacteristicList/> 
         <SRS:AlternateIdentifierList/> 
        </SRS:ChemicalSubstanceSynonym> 
       </SRS:ChemicalSubstanceSynonymList> 
      </SRS:ChemicalSubstance> 
     </SRS:SubstanceInformation> 
    </Results> 
    <RowCount>1</RowCount> 
    <RowId>0</RowId> 
</QueryResponse> 

,我無法弄清楚如何去在XML ChemicalSubstanceIdentification節點。我的代碼是

my $parser = XML::LibXML->load_xml(location => 'output.xml'); 

my $doc = XML::LibXML::XPathContext->new($parser); 
$doc->registerNs('SRS', 'http://www.exchangenetwork.net/schema/SRS/3'); 
my $chemIdent = $doc->findnodes('/QueryResponse/Results/SRS:SubstanceInformation/SRS:ChemicalSubstance/SRS:ChemicalSubstanceIdentification'); 

我在做什麼是錯的。任何幫助表示讚賞。謝謝!

回答

3

XPath上的頭幾個元素位於XML文檔的http://www.exchangenetwork.net/schema/node/2名稱空間中。您必須爲QueryResponseResults元素指定名稱空間才能使XPath正常工作。

另外,如果只有一個SRS:SubstanceInformationResults無論如何,你可能只是通過//跳過QueryResponseResults

//SRS:SubstanceInformation/SRS:ChemicalSubstance/SRS:ChemicalSubstanceIdentification