I have a XmlDocument in java, created with the Weblogic XmlDocument parser.
I want to replace the content of a tag in this xmldocument with my own data, or insert the tag if it isnt there.
<customdata>
<tag1 />
<tag2>mfkdslmlfkm</tag2>
<location />
<tag3 />
</customdata>
I for example want to insert some url in the location tag:
<location><http://something></location>
but otherwise leave the xml as is.
Currently I use a xmlcursor: XmlObject xmlobj = XmlObject.Factory.parse(a.getCustomData(), options); XmlCursor xmlcur = xmlobj.newCursor();
while (xmlcur.hasNextToken()) {
boolean found = false;
if (xmlcur.isStart() && "schema-location".equals(xmlcur.getName().toString())) {
xmlcur.setTextValue("http://replaced");
System.out.println("replaced");
found = true;
} else if (xmlcur.isStart() && "customdata".equals(xmlcur.getName().toString())) {
xmlcur.push();
} else if (xmlcur.isEnddoc()) {
if (!found) {
xmlcur.pop();
xmlcur.toEndToken();
xmlcur.insertElementWithText("schema-location", "http://inserted");
System.out.println("inserted");
}
}
xmlcur.toNextToken();
}
I tried to find a "quick" xquery way to do this since the XmlDocument has an execQuery method, but didnt find it very easy.
Do anyone have a better way than this? It seems a bit elaborate.