Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在XmlDocument中插入/替换XML标记?_Java_Xml_Xquery - Fatal编程技术网

Java 如何在XmlDocument中插入/替换XML标记?

Java 如何在XmlDocument中插入/替换XML标记?,java,xml,xquery,Java,Xml,Xquery,我有一个java的XmlDocument,它是用WebLogicXMLDocument解析器创建的 我想用我自己的数据替换此XMLDocument中标记的内容,或者插入不存在的标记 <customdata> <tag1 /> <tag2>mfkdslmlfkm</tag2> <location /> <tag3 /> </customdata> 我试图找到一种“快速”xquery的方法,

我有一个java的
XmlDocument
,它是用
WebLogicXMLDocument
解析器创建的

我想用我自己的数据替换此
XMLDocument
中标记的内容,或者插入不存在的标记

<customdata>
   <tag1 />
   <tag2>mfkdslmlfkm</tag2>
   <location />
   <tag3 />
</customdata>
我试图找到一种“快速”
xquery
的方法,因为
XmlDocument
有一个
execQuery
方法,但并不容易


有人有比这更好的方法吗?这似乎有点复杂。

您应该可以通过
查询

试一试

我自己对xquery还不熟悉,我发现使用它是一种痛苦的查询语言,但一旦您完成了最初的学习过程,它确实可以安静地工作


我仍然希望有一个更简单的方法,它同样有效

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

这真的很容易

例如,您可以定义主对象,在您的示例中是CustomData(我使用公共字段来保持示例的简单性):

然后初始化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);

听起来怎么样?

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

如果您不知道该模式,那么XStream解决方案可能不是一种可行的方法。至少XStream现在在你的雷达上,将来可能会派上用场

replace不修改文档,它只替换字符串中出现的文本并返回字符串。
public class CustomData {
  public String tag1;
  public String tag2;
  public String location;
  public String tag3;
}
XStream xstream = new XStream();
// if you need to output the main tag in lowercase, use the following line
xstream.alias("customdata", CustomData.class);  
CustomData d = (CustomData)xstream.fromXML(xml);
d.location = "http://stackoverflow.com";
xml = xstream.toXML(d);
// 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 );
  }
}
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 );
}