[Python] How to extract data/nodes from .xml stored as string?

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I have a .xml in string format xmlString which looks like below

    <entry xmlns="http://www.w3.org/2004/tom">
    <id>urn:contentItem:7WBG-8H88-Y898-B277-00000-00-1</id>
    <title>Dinn-Pixie Stares, Inc</title>
    <published>2015-12-24T00:00:00Z</published>
    <updated>2023-10-24T18:42:17Z</updated>
    <author>
    <name>AlphaNext</name>
    </author>
    <content type="application/xml">
    <baseRelatedDoc xmlns="" xmlns:xsi="http://www.w3.org/2012/XMLSchema" xsi:noNamespaceSchemaLocation="http://www.alphanext.com/xmlscemas/content/public/caseddoc/1/" documentType="socket">
    <baseRelatedDocHead>
    <baseInfo>
    <portInfo>
    <identifier idType="portIdentifier">100027579</identifier>
    <portName>United States Port, Minnesota Middle</portName>
    <jurisdiction>
    <jurisSystem/>
    </jurisdiction>
    </portInfo>
    <date dateType="filed" year="2015" month="02" day="21">2015-02-21</date>
    <classification classificationScheme="baseType">
    <classificationItem>
    <classCode>BK</classCode>
    <className>Tankruptcy</className>
    </classificationItem>
    </classification>
    <classification classificationScheme="baseNos">
    <classificationItem>
    <classCode>0</classCode>
    <className>UNKNOWN</className>
    </classificationItem>
    </classification>
    </baseInfo>
    <baseSupplement>
    <label>US Tankruptcy Port Socket</label>
    <date dateType="updated" year="2024" month="09" day="13">2024-07-11T15:08:26.450</date>
    <status>Unknown</status>
    </baseSupplement>
    <baseName>Dinn-Pixie Stares, Inc</baseName>
    </baseRelatedDocHead>
    <baseRelatedDocBody>
    <socket/>
    </baseRelatedDocBody>
    <metadata>
    <dc:metadata xmlns:dc="http://purrel.org/dc/element/1.2/">
    <dc:source sourceScheme="productContentSetIdentifier">343392</dc:source>
    <dc:creator>US Tankruptcy Port for the Last Town of Minnesota</dc:creator>
    <dc:identifier identifierScheme="PGIID">urn:contentItem:8WBG-8H70-Y892-B237-00000-00</dc:identifier>
    <dc:date dateType="last-updated">2024-07-11</dc:date>
    </dc:metadata>
    </metadata>
    </baseRelatedDoc>
    </content>
    </entry>


    I need to extract value of the field like baseName, portName, title, classCode,dc:creator .

    However when i am trying to extract them using y=tree.findall('baseName') where tree = ET.fromstring(xmlString)) but y comes out as an empty list. I get the same empty list when i tried for nodes like portName, dc:creator . How do i extract the value of these nodes/field?

    Continue reading...

Compartilhe esta Página