与AWK打印XML元素 [英] Print XML element with AWK
问题描述
我如何打印的XML元素的内容 - 从开始标记到结束标记 - 使用AWK
How do I print the contents of an XML element - from the starting tag to the closing tag - using AWK?
例如,请考虑以下XML:
For example, consider the following XML:
<flight>
<airline>Delta</airline>
<flightno>22</flightno>
<origin>Atlanta</origin>
<destination>Paris</destination>
<departure>5:40pm</departure>
<arrival>8:10am</arrival>
</flight>
<city id="AT">
<cityname>Athens</cityname>
<state>GA</state>
<description> Home of the University of Georgia</description>
<population>100,000</population>
<location>Located about 60 miles Northeast of Atlanta</location>
<latitude>33 57' 39" N</latitude>
<longitude>83 22' 42" W</longitude>
</city>
所需的输出可能是城市
元素的内容,可以从&LT;城市...&GT;
来&LT; /城市的方式&gt;
The desired output could be contents of the city
element, from <city...>
to </city>
.
推荐答案
这解析像AWK工具,XML和sed是不完美的解决方案。你不能依赖于XML总是有一个人类可读的布局。例如,某些Web服务会忽略新线,导致出现在一行整个XML文档英寸
Solutions that parse XML with tools like awk and sed are imperfect. You cannot rely on XML always having a human readable layout. For example some web services will omit new-lines, resulting in the entire XML document appearing on one line.
我会建议使用xmllint,其中有选择使用XPATH,专为XML查询语言节点。
I would recommend using xmllint, which has the ability to select nodes using XPATH, a query language designed for XML.
以下命令将选择城市标签:
The following command will select the city tags:
xmllint --xpath "//city" data.xml
XPath是非常有用的。它使XML文档寻址的每一个部分:
XPath is extremely useful. It makes the every part of the XML document addressable:
xmllint --xpath "string(//city[1]/@id)" data.xml
返回字符串AT。
Returns the string "AT".
这一次回到城市标签的第一次出现。 xmllint也可以用来pretty打印结果
This time return the first occurrence of the "city" tag. xmllint can also be used to pretty print the result:
$ xmllint --xpath "//city[1]" data.xml | xmllint -format -
<?xml version="1.0"?>
<city id="AT">
<cityname>Athens</cityname>
<state>GA</state>
<description> Home of the University of Georgia</description>
<population>100,000</population>
<location>Located about 60 miles Northeast of Atlanta</location>
<latitude>33 57' 39" N</latitude>
<longitude>83 22' 42" W</longitude>
</city>
data.xml中
在此相同的数据的第一个城市的标签似乎都在一行。这是有效的XML。
data.xml
In this same data the first "city" tag appears all on one line. This is valid XML.
<data>
<flight>
<airline>Delta</airline>
<flightno>22</flightno>
<origin>Atlanta</origin>
<destination>Paris</destination>
<departure>5:40pm</departure>
<arrival>8:10am</arrival>
</flight>
<city id="AT"> <cityname>Athens</cityname> <state>GA</state> <description> Home of the University of Georgia</description> <population>100,000</population> <location>Located about 60 miles Northeast of Atlanta</location> <latitude>33 57' 39" N</latitude> <longitude>83 22' 42" W</longitude> </city>
<city id="DUB">
<cityname>Dublin</cityname>
<state>Dub</state>
<description> Dublin</description>
<population>1,500,000</population>
<location>Ireland</location>
<latitude>NA</latitude>
<longitude>NA</longitude>
</city>
</data>
这篇关于与AWK打印XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!