如何使用 lxml 和 python 漂亮地打印 xml 文件的子树?
How to use lxml and python to pretty print a subtree of an xml file?
我有以下代码,使用 python 和 lxml 来漂亮地打印文件 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:
<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 代码来做到这一点?
我们可以使用 xpath 捕获嵌套元素。元素对象不提供相同的 .write()
功能,因此我们需要不同的输出机制。
怎么样...
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)
# assuming there will be exactly 1 project
project=tree.xpath("project")[0]
print etree.tostring(project, pretty_print = True)' < example.xml
您可以使用 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, pretty_print = True)'
我有以下代码,使用 python 和 lxml 来漂亮地打印文件 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:
<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 代码来做到这一点?
我们可以使用 xpath 捕获嵌套元素。元素对象不提供相同的 .write()
功能,因此我们需要不同的输出机制。
怎么样...
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)
# assuming there will be exactly 1 project
project=tree.xpath("project")[0]
print etree.tostring(project, pretty_print = True)' < example.xml
您可以使用 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, pretty_print = True)'