1

给定以下xml:

<language>en-US</language>
<provider>VenturesLLC</provider>
<video>
    <original_spoken_locale>en-US</original_spoken_locale>
    <vendor_offer_code>TEST_VENDOR</vendor_offer_code>
    <release_date>2011-01-15</release_date>
    <title>Moving Forward</title>
    <vendor_id>ASDF_ING_2012</vendor_id>
</video>

我正在寻找检索整个 <video>块。但是,当我这样做时:

>>> f=open('metadata.xml')
>>> contents=f.read()
>>> node=etree.fromstring(contents)
>>> node.xpath("//*[local-name()='video']")[0].text
'\n

请注意,如果我做了类似的事情,node.xpath("//*[local-name()='original_spoken_locale']")[0].text我会得到正确的'en-US'. 我将如何提取这个完整的文本以便我可以得到:

text = """    
<video>
    <original_spoken_locale>en-US</original_spoken_locale>
    <vendor_offer_code>TEST_VENDOR</vendor_offer_code>
    <release_date>2011-01-15</release_date>
    <title>Moving Forward</title>
    <vendor_id>ASDF_ING_2012</vendor_id>
</video>"""
4

1 回答 1

2

您的.text呼叫不起作用,因为您的视频节点没有文本 - 它有其他节点子节点。您需要使用将这些节点转换为字符串tostring

In [1]: from lxml import etree

In [2]: xml = '''<xml>
   ...: <language>en-US</language>
   ...: <provider>VenturesLLC</provider>
   ...: <video>
   ...:     <original_spoken_locale>en-US</original_spoken_locale>
   ...:     <vendor_offer_code>TEST_VENDOR</vendor_offer_code>
   ...:     <release_date>2011-01-15</release_date>
   ...:     <title>Moving Forward</title>
   ...:     <vendor_id>ASDF_ING_2012</vendor_id>
   ...: </video></xml>'''

In [3]: tree = etree.fromstring(xml)

In [4]: vid = tree.xpath('//video')[0]

In [5]: etree.tostring(vid, pretty_print=True)
Out[5]: '<video>\n  <original_spoken_locale>en-US</original_spoken_locale>\n  <vendor_offer_code>TEST_VENDOR</vendor_offer_code>\n  <release_date>2011-01-15</release_date>\n  <title>Moving Forward</title>\n  <vendor_id>ASDF_ING_2012</vendor_id>\n</video>\n'

In [6]: print _
<video>
  <original_spoken_locale>en-US</original_spoken_locale>
  <vendor_offer_code>TEST_VENDOR</vendor_offer_code>
  <release_date>2011-01-15</release_date>
  <title>Moving Forward</title>
  <vendor_id>ASDF_ING_2012</vendor_id>
</video>
于 2012-06-22T19:03:28.883 回答