Groovy编辑XML文件,保留注释,换行符 [英] Groovy edit XML file, keep comments, line breaks

查看:816
本文介绍了Groovy编辑XML文件,保留注释,换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编辑一个现有的XML文件,同时保留它的原始布局。这包括新行,注释等。编辑包括在XML中查找元素并修改文本值。



我的第一次尝试是将XMLParser与XmlUtil.serialize一起使用,但是不符合要求。

任何人都知道XMLParser的任何替代方案都是原始XML字符串的原位编辑吗?如果没有,也许有一个库使用XPath / GPath执行搜索,并返回查找的位置,以便我可以执行StringBuilder.replace。



编辑

现在我做了这个函数,找到XML节点的字符串索引(我可以使用xpath找到),然后我正在替换索引。对于简单节点
< node>值< / node>:

  def find_location_by_node(xmlString,root_xml,节点)
{
current_index = 0;


{
节点名称= current_node.name()。getLocalPart()
CURRENT_INDEX = xmlString.indexOf('<(在root_xml.depthFirst()CURRENT_NODE) ;'+ node_name,current_index);

if(current_node == node)
{
end_tag ='< /'+ node_name +'>';
end_tag_index = xmlString.indexOf(end_tag,current_index)+ end_tag.length();

return [current_index,end_tag_index];
}
}

返回-1;


解决方案

您可以用DOMCategory 更新XML。 DOM将保留原始布局。

  import groovy.xml.DOMBuilder 

def input ='' '
<购物>
< category type =groceries>
< item>巧克力< / item>
< item> Coffee< / item>
< / category>
< category type =supplies>
< item>纸张< / item>
< item quantity =4>钢笔< / item>
< / category>
< category type =present>
< item when =Aug 10>凯瑟琳的生日< / item>
< item>巧克力< / item>
< / category>
< / shopping>
$ b $''

def doc = DOMBuilder.parse(new StringReader(input))
def root = doc.documentElement
use(groovy。 xml.dom.DOMCategory){
def chocolate = root.depthFirst()。grep {it.text()==Chocolate}
巧克力* .value =Nutella
}

def result = groovy.xml.dom.DOMUtil.serialize(root)
println结果


I would like to edit an existing XML file while preserving it's original layout. This includes new lines, comments etc. Editing is composed of finding elements inside the XML and modifying the text value.

My first try was using XMLParser with XmlUtil.serialize but that does not meet the requirement.

Anyone is aware of any alternative to XMLParser where edits are "in place" of the original XML string? If not, perhaps there is a library that performs search using XPath/GPath and just return the location of the find so I can do StringBuilder.replace.

EDIT:

For now I made this function, to find string indexes of XML node (that I can find using xpath) then I am doing replace on the indexes. Works fine for simple nodes <node>value</node>:

def find_location_by_node(xmlString, root_xml, node)
{
    current_index = 0;

    for(current_node in root_xml.depthFirst())
    {
      node_name = current_node.name().getLocalPart()
      current_index = xmlString.indexOf('<' + node_name, current_index);

      if(current_node == node)
      {
        end_tag = '</' + node_name + '>';
        end_tag_index = xmlString.indexOf(end_tag, current_index) + end_tag.length();

        return [current_index, end_tag_index];
      }
    }

  return -1;
}

解决方案

You could update your XML with DOMCategory. DOM will keep your original layout.

import groovy.xml.DOMBuilder

def input = '''
<shopping>
    <category type="groceries">
        <item>Chocolate</item>
        <item>Coffee</item>
    </category>
    <category type="supplies">
        <item>Paper</item>
        <item quantity="4">Pens</item>
    </category>
    <category type="present">
        <item when="Aug 10">Kathryn's Birthday</item>
        <item>Chocolate</item>
    </category>
</shopping>

'''

def doc = DOMBuilder.parse(new StringReader(input))
def root = doc.documentElement
use(groovy.xml.dom.DOMCategory) {
    def chocolate = root.depthFirst().grep{it.text() == "Chocolate"}
    chocolate*.value = "Nutella"
}

def result = groovy.xml.dom.DOMUtil.serialize(root)
println result

这篇关于Groovy编辑XML文件,保留注释,换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆