Reputation: 593
I run into strange problem and I cant find anything on web for that.
Im using xpath to locate my xml chunk and then replace it with something else ex:
// input
<html>
...
<style type="">
background: yellow;
</style>
<link href="path" />
</html>
// output
<html>
...
<link href="pathToFileWithBackground" />
<link href="path_other" />
</html>
I use XMLModifier to actually update token (using updateToken method :]) and it works for attributes manipulation or for text (body) of element.
I have problem with style element, I want to process its body and then replace whole element with link to some file.
obviously updating text token will result with style token with link tag inside it.
<style type=""><link href="path" />
</style>
so I tried moving navigator to style element and got this
<<link href="path" /> type="">
background: yellow;
</style>
I also tried inserting link element before style element and removing it but I got ModifyException("Invalid insertion/deletion condition detected between offset .. and offset") from XMLModifier when I try to save modifier's data. here is the code for that
ap.selectXPath("//style/text());
vn.toElement(VTDNav.PARENT);
modifier.insertBeforeElement(replaceString);
modifier.remove();
I tried to use remove(long l) and remove(int offset, int lengtt) version but got same exception
any ideas on that
Upvotes: 1
Views: 743
Reputation: 593
OK so the problem was with
vn.getElementFragment()
returned wrong length. maybe because (in my test file) style element was followed by comments and comment characters were included and length was bigger than it should be
this fixed everything
int o = (int)vn.getElementFragment();
int l = (int)(vn.getElementFragment() >> 32);
final String s = new String(vn.getXML().getBytes(o, l));
l = s.substring(0, s.indexOf("</style>")+8).length(); // ;]
modifier.removeContent(o, l);
modifier.insertBeforeElement(replaceString);
hope it will help someone
Upvotes: 3