【问题标题】:parsing XML using perl XML::LibXML使用 perl XML::LibXML 解析 XML
【发布时间】:2013-10-28 03:21:29
【问题描述】:

我有一个以以下格式返回 XML 的 Web 服务。我正在使用 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');

我在做什么有问题。任何帮助表示赞赏。谢谢!

【问题讨论】:

    标签: xml perl xml-libxml


    【解决方案1】:

    XPath 上的前几个元素位于 XML 文档的 http://www.exchangenetwork.net/schema/node/2 命名空间中。您必须为QueryResponse 和Results 元素指定该名称空间才能使XPath 工作。

    或者,如果Results 中只有一个SRS:SubstanceInformation,您可以通过// 跳过QueryResponse 和Results:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 2015-02-04
      • 2012-07-08
      • 2021-08-15
      相关资源
      最近更新 更多