如何获得Python中没有根节点的XML

如何获得Python中没有根节点的XML,python,xml,xml-parsing,Python,Xml,Xml Parsing,鉴于以下数据: <rdf:RDF xmlns="http://purl.org/rss/1.0/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1. <channel rdf:about="http://w

鉴于以下数据:

<rdf:RDF
    xmlns="http://purl.org/rss/1.0/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:dc="http://purl.org/dc/elements/1.
<channel rdf:about="http://www.gmanews.tv/">
        <title>GMANews.TV</title>
        <description> GMA News.tv bring you the latest news from GMA News teams and highlights of your favorite shows. Subscribe now and stay up-to-date with GMA News.tv.</description>
        <link>http://www.gmanews.tv/</link>
</channel>

<item rdf:about="http://www.gmanews.tv/story/232365/world/magnitude-59-quake-hits-chilean-coast-no-damage">
        <dc:format>text/html</dc:format>
        <dc:date>2011-09-14T16:39:22+08:00</dc:date>
        <dc:source>http://www.gmanews.tv/story/232365/world/magnitude-59-quake-hits-chilean-coast-no-damage </dc:source>
                <title><![CDATA[Magnitude-5.9 quake hits Chilean coast, no damage]]></title>
        <link>http://www.gmanews.tv/story/232365/world/magnitude-59-quake-hits-chilean-coast-no-damage </link>
        <description><![CDATA[SANTIAGO - A magnitude 5.9 quake hit just off the coast of central Chile early on Wednesday, but the state emergency office said there were no reports of damage.]]></description>
    </item>
        <item rdf:about="http://www.gmanews.tv/story/232362/nation/house-minority-blames-pnoys-advisers-for-legal-setbacks">
        <dc:format>text/html</dc:format>
        <dc:date>2011-09-14T16:04:51+08:00</dc:date>
        <dc:source>http://www.gmanews.tv/story/232362/nation/house-minority-blames-pnoys-advisers-for-legal-setbacks </dc:source>
                <title><![CDATA[House minority blames PNoy's advisers for legal 'setbacks']]></title>
        <link>http://www.gmanews.tv/story/232362/nation/house-minority-blames-pnoys-advisers-for-legal-setbacks </link>
        <description><![CDATA[Members of the opposition at the House of Representatives on Wednesday blamed President Benigno Aquino III's advisers for the various legal "setbacks&quot; suffered by his administration and advised him to consider replacing some of his advisers.]]></description>
    </item>
        <item rdf:about="http://www.gmanews.tv/story/232356/nation/ex-sharia-judge-20-others-may-testify-in-poll-fraud-probe">
        <dc:format>text/html</dc:format>
        <dc:date>2011-09-14T15:19:45+08:00</dc:date>
        <dc:source>http://www.gmanews.tv/story/232356/nation/ex-sharia-judge-20-others-may-testify-in-poll-fraud-probe </dc:source>
                <title><![CDATA[Ex-Shari'a judge, 20 others may testify in poll fraud probe]]></title>
        <link>http://www.gmanews.tv/story/232356/nation/ex-sharia-judge-20-others-may-testify-in-poll-fraud-probe </link>
        <description><![CDATA[The former Shari'a court judge who claimed to have helped Gloria Macapagal-Arroyo cheat in the 2004 presidential elections and at least 20 others may serve as witnesses in the joint investigation by the Commission on Elections and Department of Justice on the alleged poll fraud, Comelec chief Sixto Brillantes Jr. said Wednesday.]]></description>
    </item>
</rdf:RDF>
提供了一种处理XML中所有内容的好方法。您发布的XML示例如下:

from lxml import etree

document = etree.parse('your-example-xml.rdf')
root = document.getroot()

# Namespace shortcuts
ns = root.nsmap.get(None)
rdf = root.nsmap.get('rdf')

for item in root.xpath('purl:item', namespaces={'purl': ns}):
    print item.attrib.get('{%s}about' % rdf)
    print item.xpath('purl:description/text()', namespaces={'purl': ns})
    print
但是,如果只解析RDF,可能会有RDF特定的库可用。

提供了一种处理XML所有内容的好方法。您发布的XML示例如下:

from lxml import etree

document = etree.parse('your-example-xml.rdf')
root = document.getroot()

# Namespace shortcuts
ns = root.nsmap.get(None)
rdf = root.nsmap.get('rdf')

for item in root.xpath('purl:item', namespaces={'purl': ns}):
    print item.attrib.get('{%s}about' % rdf)
    print item.xpath('purl:description/text()', namespaces={'purl': ns})
    print

但是,如果您只解析RDF,则可能有RDF特定的库可用。

由于第三方库不是一个选项,以下是使用相同的代码:


由于第三方库不是一个选项,以下代码与使用相同:


看起来你的开始标签坏了,你是这样取回的吗?@MattH,是的,我们从你的开始标签坏了,你是这样取回的吗?@MattH,是的,我们从我不能使用lxml得到它,因为代码将在嵌入式系统上运行systems@Subhen您应该在问题中提到这一要求。我不能使用lxml,因为代码将在嵌入式系统上运行systems@Subhen你应该在问题中提到这一要求。