I have XmlDocumentin java created using a parser Weblogic XmlDocument.
I want to replace the contents of the tag in this XmlDocumentwith my own data or insert the tag if it is not there.
<customdata>
<tag1 />
<tag2>mfkdslmlfkm</tag2>
<location />
<tag3 />
</customdata>
For example, I want to insert some URL in the location tag:
<location>http://something</location>
but otherwise leave XML as is.
I am currently using 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” way xqueryto do this, since it XmlDocumenthas a method execQuery, but did not find it very simple.
Does anyone have a better way than this? It seems a bit complicated.
source
share