简体   繁体   中英

Update attribute value using vtd-xml

Given an xpath to an attribute and a new value, I am looking to update the attribute value to the new value.

I have followed the example here: http://vtd-xml.sourceforge.net/codeSample/cs7.html and come up with the following:

autoPilot.selectXPath(xpath);
modifier.updateToken(vtdNav.getAttrVal(vtdNav.toString(autoPilot.evalXPath())), newContent);

...my tests all pass but perhaps because I am not used to the "tokenized" way that vtd-xml works, it doesn't "feel" right so I am just looking for affirmation that I've done the correct thing.

Your code will work just fine... assume you will call modifier.output().

but it is not optimal...

This statement

modifier.updateToken(vtdNav.getAttrVal(vtdNav.toString(autoPilot.evalXPath())), newContent);

Can be written as

modifier.updateToken(autoPilot.evalXPath()+1, newContent);

Because if the attribute name has an index value of i (!=-1), then the attrinute value is always i+1... as attr val immediately follows an attr name. No conditional check is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM