【问题标题】:How to insert/replace XML tag in XmlDocument?如何在 XmlDocument 中插入/替换 XML 标签?
【发布时间】:2010-09-06 03:58:13
【问题描述】:

我在 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 方法,但并不容易。

还有比这更好的方法吗?看起来有点复杂。

【问题讨论】:

    标签: java xml xquery


    【解决方案1】:

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

    【讨论】:

      【解决方案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 构造一个对象,在对象上设置位置字段并重新生成 XML:

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

      听起来怎么样?

      【讨论】:

        【解决方案3】:

        如果您不知道架构,那么 XStream 解决方案可能不是最佳选择。至少 XStream 现在在你的雷达上,将来可能会派上用场!

        【讨论】:

          【解决方案4】:

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

          试试

           fn:replace(string,pattern,replace)
          

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

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

          【讨论】:

          • replace 不会修改文档,它只是替换字符串中出现的文本并返回字符串。
          猜你喜欢
          • 1970-01-01
          • 2012-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-15
          • 2013-07-03
          • 1970-01-01
          • 2017-03-31
          相关资源
          最近更新 更多