5

我有一个XmlDocumentWeblogic XmlDocument解析器创建的java。

我想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方法,但发现它并不容易。

有人有比这更好的方法吗?好像有点精致。

4

4 回答 4

5

基于 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 );
}
于 2008-08-19T11:31:10.433 回答
2

面向对象的方法怎么样?您可以将 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);

听起来怎么样?

于 2008-08-19T10:45:20.620 回答
1

如果您不知道架构,XStream 解决方案可能不是可行的方法。至少 XStream 现在在您的雷达上,将来可能会派上用场!

于 2008-08-19T15:15:36.267 回答
0

你应该能够做到这一点query

尝试

 fn:replace(string,pattern,replace)

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

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

于 2008-08-19T10:38:08.807 回答