如何使用 lxml 和 python 漂亮地打印 xml 文件的子树?

Ste*_*ige 5 python lxml

我有以下代码,使用pythonlxml来漂亮地打印文件example.xml

python -c '
from lxml import etree;
from sys import stdout, stdin;

parser=etree.XMLParser(remove_blank_text=True, strip_cdata=False);
tree=etree.parse(stdin, parser)
tree.write(stdout, pretty_print = True)' < example.xml
Run Code Online (Sandbox Code Playgroud)

我使用 lxml 是因为保留原始文件的保真度(包括保留 CDATA 习惯用法)非常重要。这是我正在使用的文件example.xml :

<projects><project name="helloworld" threads="1" pubsub="auto" heartbeat-interval="1">
<description><![CDATA[This is a sample project]]></description>  <metadata>    <meta id="studioUploadedBy">anonymous</meta>
<meta id="studioUploaded">1550863090439</meta>    <meta id="studioModifiedBy">anonymous</meta>
<meta id="studioModified">1550863175384</meta>    <meta id="studioTags">helloworld</meta>
<meta id="studioVersionNotes">This is just a sample project</meta>    <meta id="layout">{"cq1":{"Source1":{"x":50,"y":-290}}}</meta>
</metadata>  <contqueries>    <contquery name="cq1">      <windows>        <window-source pubsub="true" name="Source1">
<schema>            <fields>              <field name="name" type="string" key="true"/>            </fields>
</schema>        </window-source>      </windows>    </contquery>  </contqueries> </project></projects>
Run Code Online (Sandbox Code Playgroud)

它生成以下输出:

<projects>
  <project name="helloworld" threads="1" pubsub="auto" heartbeat-interval="1">
    <description><![CDATA[This is a sample project]]></description>
    <metadata>
      <meta id="studioUploadedBy">anonymous</meta>
      <meta id="studioUploaded">1550863090439</meta>
      <meta id="studioModifiedBy">anonymous</meta>
      <meta id="studioModified">1550863175384</meta>
      <meta id="studioTags">helloworld</meta>
      <meta id="studioVersionNotes">This is just a sample project</meta>
      <meta id="layout">{"cq1":{"Source1":{"x":50,"y":-290}}}</meta>
    </metadata>
    <contqueries>
      <contquery name="cq1">
        <windows>
          <window-source pubsub="true" name="Source1">
            <schema>
              <fields>
                <field name="name" type="string" key="true"/>
              </fields>
            </schema>
          </window-source>
        </windows>
      </contquery>
    </contqueries>
  </project>
</projects>
Run Code Online (Sandbox Code Playgroud)

这几乎是我想要的,只是我想得到一个子树。我希望能够只<project name="helloworld"...>通过子树</project>。我如何修改上述基于lxml 的Python 代码来做到这一点?

F. *_* K. 2

您可以使用tree.find来获取需要提取的xml元素。他们将其转换为元素树。然后,在这种情况下,您可以在生成的元素树 (et) 上发出写入语句。

python -c '
           from lxml import etree;
           from sys import stdout, stdin;
           parser=etree.XMLParser(remove_blank_text=True,strip_cdata=False);
           tree=etree.parse(stdin, parser)
           e = tree.find("project")
           et = etree.ElementTree(e)                                                                                                                                                                             
           et.write(stdout.buffer, pretty_print = True)'
Run Code Online (Sandbox Code Playgroud)

[注意:对于Python 2,只需使用“stdout”而不是“stdout.buffer”]