javavtd-xml

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.


Solution

  • 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.