xmodifier 介绍
@H_502_0@ @H_502_0@xmodifier 是使用 XPATH 创建或者更 XML。XPATH 可以用来搜索 XML,但是有时候也可以用来编辑或者创建 XML:XPath | Value | Desc |
---|---|---|
/ns:root/ns:element1 |
add | |
/ns:root/ns:element2[@attr=1] |
add | |
/ns:root/ns:element2/@attr | 1 |
add |
/ns:root/ns:element1/ns:element11 |
add | |
/ns:root/ns:element3 | TEXT |
add |
/ns:root/ns:element1[ns:element12]/ns:element13 |
add | |
//PersonList/Person[2]/Name | NewName | set the second Person node's Name Text |
//PersonList/Person[2]/Name/text() | NewName | set the second Person node's Name Text |
//PersonList/Person[1]/Name(:delete) | delete this Name node | |
//PersonList/Person(:add)/Name | NewName | alway add a new Person node |
//PersonList/Person(:insertBefore(Person[Name='Name2']))/Name | NewName | add a new Person node before Person named "Name2" |
Document document = createDocument(); //empty document XModifier modifier = new XModifier(document); modifier.setNamespace("ns", "http://localhost"); // create an empty element modifier.addModify("/ns:root/ns:element1"); // create an element with attribute modifier.addModify("/ns:root/ns:element2[@attr=1]"); // append an new element to existing element1 modifier.addModify("/ns:root/ns:element1/ns:element11"); // create an element with text modifier.addModify("/ns:root/ns:element3", "TEXT"); modifier.modify();@H_502_0@XML:
<root xmlns="http://localhost"> <element1> <element11/> </element1> <element2 attr="1"/> <element3>TEXT</element3> </root>@H_502_0@修改现有的 XML: @H_502_0@原始 XML:
<root xmlns="http://localhost"> <element1> <element11></element11> </element1> <element1> <element12></element12> </element1> <element2></element2> <element3></element3> </root> Document document = readDocument("modify.xml"); XModifier modifier = new XModifier(document); modifier.setNamespace("ns", "http://localhost"); modifier.addModify("/ns:root/ns:element1[ns:element12]/ns:element13"); modifier.modify();@H_502_0@修改之后的 XML:
<root xmlns="http://localhost"> <element1> <element11/> </element1> <element1> <element12/> <element13/> </element1> <element2/> <element3/> </root>@H_502_0@添加了新元素 ns:element13
xmodifier 官网
https://github.com/shenghai/xmodifier
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。