java vtd-xml,Java中的VTD-XML - 在XMLModifier.insertAfterElement之后查找索引

I've started using VTD (I guess VTD-XML) in Java, and for XPath reads it's excellent. Where i'm hitting an issue now is with inserting data. Lets say I am doing the following:

VTDNav nav = preExistingGen.getNav();

AutoPilot pilot = new AutoPilot(nav);

pilot.selectXPath("/Something/SomethingElse");

if (pilot.evalXPath() != -1) {

XMLModifier modifier = new XMLModifier(nav);

modifier.insertAfterElement("content");

}

What I had assumed was this was a real-time update, which would be reflected in the VTDNav. It looks like my understanding is incorrect, since simply inserting the element content does nothing to the nav (if I output the VTDNav, it still contains my original xml). The only way I can seem to get a handle on the new xml, is by outputting it from the XMLModifier.

modifier.outputAndReparse(); // Gives me a new VTDNav with the new content

Is there something i'm missing here? Is there an easier way of doing this? I wanted to be able to insert the new content, and then immediately get the new index. My existing code (using the standard DOM classes) has a ton of inserts and updates, and I also need to know where the last inserted element existed in the document. Having to outputAndReparse() everytime and then find the inserted element (which I may not even be able to guarantee) doesn't seem like a plausible solution.

解决方案

I think the answer is to plan your modification and subsequent access to new content carefully. If you insert the new content, and try to access the new content immediately afterwards, insertAndParse() is the way to go. But as you can see, it is rather slow because of the reparsing. My suggestion is that you plan as much as insert all at once, then call reparsing just once, it will be a lot more efficient this way.

The spirit is that VTD-XML is not trying to be DOM, it has its own strengths and weaknesses... and this is one of the weakness, but you can work around it ...

And when you try to merge multiple xml files, vtd-xml will certainly shine....

Also if you tag this question with vtd-xml, i will be able to find it much easier.

xmodifier 是使用 XPATH 创建或者更 XML。XPATH 可以用来搜索 XML,但是有时候也可以用来编辑或者创建 XML: XPath Value Desc /ns:root/ns:element1 add <ns:element1/> /ns:root/ns:element2[@attr=1] add <ns:element2 attr="1"/> /ns:root/ns:element2/@attr 1 add <ns:element2 attr="1"/> /ns:root/ns:element1/ns:element11 add <ns:element11/> /ns:root/ns:element3 TEXT add <ns:element3>TEXT</ns:element3> /ns:root/ns:element1[ns:element12]/ns:element13 add <ns:element13/> //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" 代码示例: 创建新的 XML 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(); XML: <root  XML: 原始 XML: <root  XML: <root  ns:element13 标签:xmodifier
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值