Разобрать глубоко вложенный XML в pandas dataframe - PullRequest
0 голосов
/ 16 апреля 2019

Я пытаюсь получить определенные части файла XML и переместить его в кадр данных pandas. После некоторых уроков из xml.etree я все еще застрял в получении результата. До сих пор мне удалось найти дочерние узлы, но я не могу получить к ним доступ (т.е. не могу получить из них фактические данные). Итак, вот что у меня есть.

tree=ET.parse('data.xml')
root=tree_edu.getroot()
root.tag
#find all nodes within xml data
tree_edu.findall(".//")
#access the node
tree.findall(".//{http://someUrl.nl/schema/enterprise/program}programSummaryText")

Я хочу получить данные от узла programDescriptions и, в частности, от ребенка programDescriptionText xml:lang="nl", и, конечно, пару дополнительных. Но сначала сосредоточимся на этом.

Некоторые данные для работы:

<?xml version="1.0" encoding="UTF-8"?>
<programs xmlns="http://someUrl.nl/schema/enterprise/program">
<program xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://someUrl.nl/schema/enterprise/program http://someUrl.nl/schema/enterprise/program.xsd">
<customizableOnRequest>true</customizableOnRequest>
<editor>webmaster@url</editor>
<expires>2019-04-21</expires>
<format>Edu-dex 1.0</format>
<generator>www.Url.com</generator>
<includeInCatalog>Catalogs</includeInCatalog>
<inPublication>true</inPublication>
<lastEdited>2019-04-12T20:03:09Z</lastEdited>
<programAdmission>
    <applicationOpen>true</applicationOpen>
    <applicationType>individual</applicationType>
    <maxNumberOfParticipants>12</maxNumberOfParticipants>
    <minNumberOfParticipants>8</minNumberOfParticipants>
    <paymentDue>up-front</paymentDue>
    <requiredLevel>academic bachelor</requiredLevel>
    <startDateDetermination>fixed starting date</startDateDetermination>
</programAdmission>
<programCurriculum>
    <instructionMode>training</instructionMode>
    <teacher>
        <id>{D83FFC12-0863-44A6-BDBB-ED618627F09D}</id>
        <name>SomeName</name>
        <summary xml:lang="nl">
        Long text of the summary. Not needed.
        </summary>
    </teacher>
    <studyLoad period="hour">26</studyLoad>
</programCurriculum>
<programDescriptions>
    <programName xml:lang="nl">Program Course Name</programName>
    <programSummaryText xml:lang="nl">short Program Course Name summary</programSummaryText>
    <programSummaryHtml xml:lang="nl">short Program Course Name summary in HTML format</programSummaryHtml>
    <programDescriptionText xml:lang="nl">This part is needed from the XML.
        Big program description text. This part is needed to parse from the XML file.
    </programDescriptionText>
    <programDescriptionHtml xml:lang="nl">Not needed;
        Not needed as well;
    </programDescriptionHtml>
    <subjectText>
        <subject>curriculum</subject>
        <header1 xml:lang="nl">Beschrijving</header1>
        <descriptionHtml xml:lang="nl">Yet another HTML desscription;
            Not necessarily needed;</descriptionHtml>
        </subjectText>
    <searchword xml:lang="nl">search word</searchword>
    <webLink xml:lang="nl">website-url</webLink>
</programDescriptions>
<programSchedule>
    <programRun>
        <id>PR-019514</id>
        <status>application opened</status>
        <startDate isFinal="true">2019-06-26</startDate>
        <endDate isFinal="true">2020-02-11</endDate>
    </programRun>
</programSchedule>
</program>
</programs>

1 Ответ

1 голос
/ 16 апреля 2019

Попробуйте код ниже: (55703748.xml содержит опубликованный вами xml)

import xml.etree.ElementTree as ET

tree = ET.parse('55703748.xml')
root = tree.getroot()
nodes = root.findall(".//{http://someUrl.nl/schema/enterprise/program}programSummaryText")
for node in nodes:
    print(node.text)

Вывод

short Program Course Name summary
...