Insert / replace XML tag in XmlDocument

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.

+3
source share
4 answers

XPath? , . .

XML- org.w3c.dom.Document( ), - :

// get the list of customdata nodes
NodeList customDataNodeSet = findNodes(document, "//customdata" );

for (int i=0 ; i < customDataNodeSet.getLength() ; i++) {
  Node customDataNode = customDataNodeSet.item( i );

  // get the location nodes (if any) within this one customdata node
  NodeList locationNodeSet = findNodes(customDataNode, "location" );

  if (locationNodeSet.getLength() > 0) {
    // replace
    locationNodeSet.item( 0 ).setTextContent( "http://stackoverflow.com/" );
  }
  else {
    // insert
    Element newLocationNode = document.createElement( "location" );
    newLocationNode.setTextContent("http://stackoverflow.com/" );
    customDataNode.appendChild( newLocationNode );
  }
}

findNodes, XPath.

private NodeList findNodes( Object obj, String xPathString )
  throws XPathExpressionException {

  XPath xPath = XPathFactory.newInstance().newXPath();
  XPathExpression expression = xPath.compile( xPathString );
  return (NodeList) expression.evaluate( obj, XPathConstants.NODESET );
}
+4

- ? XML , , XML.

XStream .

, , CustomData ( , ):

public class CustomData {
  public String tag1;
  public String tag2;
  public String location;
  public String tag3;
}

XStream:

XStream xstream = new XStream();
// if you need to output the main tag in lowercase, use the following line
xstream.alias("customdata", CustomData.class);  

XML, XML:

CustomData d = (CustomData)xstream.fromXML(xml);
d.location = "http://stackoverflow.com";
xml = xstream.toXML(d);

?

+1

, XStream, , . , XStream , !

+1

query

 fn:replace(string,pattern,replace)

I'm new to xquery myself, and I found it to be a painful query language to work with, but it works well when you switch to the initial learning curve.

I still want an easier way that is just as effective?

0
source

Source: https://habr.com/ru/post/1696445/


All Articles