如何使用lxml和python漂亮地打印xml文件的子树? [英] How to use lxml and python to pretty print a subtree of an xml file?

查看:81
本文介绍了如何使用lxml和python漂亮地打印xml文件的子树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code using python with lxml to pretty print the file 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

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

I'm using lxml because it is important that I preserve the fidelity of the original file, including preserving the CDATA idioms. Here's the file example.xml that I'm using it on:

<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>

它将生成以下输出:

<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>

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

This is nearly what I want except that I'd like to get a subtree. I'd like to be able to get just the subtree <project name="helloworld"...> thru </project>. How would I modify the above Python code based on lxml to do that?

推荐答案

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

You can use tree.find to get the xml element you need extracted. Them convert it to element tree. Then you can issue a write statement on the resulting elementtree (et) in this case.

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, pretty_print = True)'

这篇关于如何使用lxml和python漂亮地打印xml文件的子树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆