Jos*_*ten 146 xml scripting command-line
在进行shell脚本编写时,通常数据将存储在单行记录的文件中,如csv.用grep和处理这些数据真的很简单sed.但我经常需要处理XML,所以我真的想通过命令行来编写对XML数据的访问权限.什么是最好的工具?
小智 103
我发现xmlstarlet非常擅长这种事情.
http://xmlstar.sourceforge.net/
应该也可以在大多数发行版存储库中使用.这里有一个介绍性的教程:
http://www.ibm.com/developerworks/library/x-starlet.html
Jos*_*ten 35
一些有前景的工具
nokogiri:使用XPath和CSS选择器解析ruby中的HTML/XML DOM
hpricot:已弃用
fxgrep:使用自己的XPath语法来查询文档.用SML编写,因此安装可能很困难.
LT XML:从SGML工具,包括衍生XML工具箱sggrep,sgsort,
xmlnorm和其他人.使用自己的查询语法.文件
非常正式.用C. LT XML 2声明支持XPath,XInclude和其他W3C标准.
xmlgrep2:使用XPath进行简单而强大的搜索.使用XML :: LibXML和libxml2在Perl中编写.
XQSharp:支持XQuery,XPath的扩展.为.NET Framework编写.
xml-coreutils:Laird Breyer的工具包相当于GNU coreutils.在一篇有趣的文章中讨论了理想工具包应包含的内容.
xmldiff:用于比较两个xml文件的简单工具.
xmltk:在debian,ubuntu,fedora或macports中似乎没有包,自2007年以来没有发布过,并且使用非可移植的构建自动化.
xml-coreutils似乎是最好的文档和大多数面向UNIX的.
bor*_*yer 24
对于Joseph Holsten的优秀列表,我添加了Perl库XML :: XPath附带的xpath命令行脚本.从XML文件中提取信息的好方法:
xpath -q -e '/entry[@xml:lang="fr"]' *xml
Run Code Online (Sandbox Code Playgroud)
Vi.*_*Vi. 24
还有xml2和2xml对.它将允许通常的字符串编辑工具来处理XML.
例.q.xml:
<?xml version="1.0"?>
<foo>
text
more text
<textnode>ddd</textnode><textnode a="bv">dsss</textnode>
<![CDATA[ asfdasdsa <foo> sdfsdfdsf <bar> ]]>
</foo>
Run Code Online (Sandbox Code Playgroud)
xml2 < q.xml
/foo=
/foo= text
/foo= more text
/foo=
/foo/textnode=ddd
/foo/textnode
/foo/textnode/@a=bv
/foo/textnode=dsss
/foo=
/foo= asfdasdsa <foo> sdfsdfdsf <bar>
/foo=
Run Code Online (Sandbox Code Playgroud)
xml2 < q.xml | grep textnode | sed 's!/foo!/bar/baz!' | 2xml
<bar><baz><textnode>ddd</textnode><textnode a="bv">dsss</textnode></baz></bar>
Run Code Online (Sandbox Code Playgroud)
PS还有html2/ 2html.
Dav*_*vis 13
你可以使用xmllint:
xmllint --xpath //title books.xml
Run Code Online (Sandbox Code Playgroud)
应与大多数发行版捆绑在一起,并与Cygwin捆绑在一起.
$ xmllint --version
xmllint: using libxml version 20900
Run Code Online (Sandbox Code Playgroud)
看到:
$ xmllint
Usage : xmllint [options] XMLfiles ...
Parse the XML files and output the result of the parsing
--version : display the version of the XML library used
--debug : dump a debug tree of the in-memory document
...
--schematron schema : do validation against a schematron
--sax1: use the old SAX1 interfaces for processing
--sax: do not build a tree but work just at the SAX level
--oldxml10: use XML-1.0 parsing rules before the 5th edition
--xpath expr: evaluate the XPath expression, inply --noout
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找Windows上的解决方案,Powershell具有读写XML的内置功能.
的test.xml:
<root>
<one>I like applesauce</one>
<two>You sure bet I do!</two>
</root>
Run Code Online (Sandbox Code Playgroud)
Powershell脚本:
# load XML file into local variable and cast as XML type.
$doc = [xml](Get-Content ./test.xml)
$doc.root.one #echoes "I like applesauce"
$doc.root.one = "Who doesn't like applesauce?" #replace inner text of <one> node
# create new node...
$newNode = $doc.CreateElement("three")
$newNode.set_InnerText("And don't you forget it!")
# ...and position it in the hierarchy
$doc.root.AppendChild($newNode)
# write results to disk
$doc.save("./testNew.xml")
Run Code Online (Sandbox Code Playgroud)
testNew.xml:
<root>
<one>Who likes applesauce?</one>
<two>You sure bet I do!</two>
<three>And don't you forget it!</three>
</root>
Run Code Online (Sandbox Code Playgroud)
资料来源:https://serverfault.com/questions/26976/update-xml-from-the-command-line-windows
D. Bohdan 维护着一个开源 GitHub 存储库,其中保存了结构化文本工具的命令行工具列表,其中有一个用于 XML/HTML 工具的部分:
https://github.com/dbohdan/structured-text-tools#xml-html
| 归档时间: |
|
| 查看次数: |
66212 次 |
| 最近记录: |