我有一个 XmlDocument 在java中,用创建 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 方法,但发现不是很容易。

还有人有比这更好的方法吗?好像有点复杂。

有帮助吗?

解决方案

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

其他提示

面向对象的方法怎么样?您可以将 XML 反序列化为对象,设置该对象的位置值,然后序列化回 XML。

流媒体 让这变得非常容易。

例如,您可以定义主对象,在您的例子中是 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);

听上去怎么样?

如果您不了解架构,XStream 解决方案可能不是最佳选择。至少 XStream 现在在您的关注范围内,将来可能会派上用场!

你应该能够做到这一点 query

尝试

 fn:replace(string,pattern,replace)

我自己是 xquery 的新手,我发现它是一种使用起来很痛苦的查询语言,但是一旦你克服了最初的学习曲线,它确实可以很好地工作。

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top