在XmlDocument中插入/替换XML标签

时间:2020-03-05 18:40:40  来源:igfitidea点击:

我在Java中有一个XmlDocument,是由Weblogic XmlDocument解析器创建的。

我想用自己的数据替换该XMLDocument中的标签内容,或者如果不存在则插入标签。

<customdata>
   <tag1 />
   <tag2>mfkdslmlfkm</tag2>
   <location />
   <tag3 />
</customdata>

例如,我想在位置标记中插入一些URL:

<location>http://something</location>

否则保留XML。

目前我使用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();
    }

我试图找到一种"快速"的" xquery"方法来执行此操作,因为" XmlDocument"具有一种" execQuery"方法,但是并没有发现它很容易。

有谁能比这更好的方法?似乎有点复杂。

解决方案

回答

我们应该可以使用query做到这一点

尝试

fn:replace(string,pattern,replace)

我本人对xquery还是陌生的,我发现它是一种痛苦的查询语言,但是一旦我们克服了最初的学习过程,它确实可以安静地工作。

我仍然希望有一种更简单有效的方法吗?

回答

面向对象的方法怎么样?我们可以将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构造一个对象,在该对象上设置location字段,然后重新生成XML:

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

听上去怎么样?

回答

基于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 );
  }
}

这是执行XPath搜索的辅助方法findNodes。

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 );
}

回答

如果我们不了解架构,那么XStream解决方案可能不是理想之选。至少现在我们正在关注XStream,将来可能会派上用场!