【问题标题】:Build Xml with Groovy使用 Groovy 构建 XML
【发布时间】:2016-01-23 18:05:14
【问题描述】:

我需要从每个标签中删除“tei:”。我的原始 xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<?oxygenRNGSchema="http://www.teic.org/release/xml/tei/custom/schema/relaxng/tei_all.rn"type="xml"?>
<?xml-stylesheet type="text/xsl" href="jerome-html-proof.xsl"?>
<TEI
  xmlns="http://www.tei-c.org/ns/1.0"
  xmlns:tei="http://www.tei-c.org/ns/1.0">
  <teiHeader>
    <fileDesc>
      <titleStmt>
        <title>Chronicles (Latin working edition, based on Helm)</title>
        <author>Jerome</author>
      </titleStmt>
      <publicationStmt>
        <p>Unpublished</p>
      </publicationStmt>
      <sourceDesc>
        <p>PD online text from http://www.tertullian.org/fathers/index.htm#jeromechronicle, entitled
          "Jerome, Chronicle (2005)" and based on pages of Helm's edition indicated in milestone
          elements. </p>
        <p>Source page includes note, "This text was transcribed by JMB. All material on this page
          is in the public domain - copy freely." </p>
      </sourceDesc>
    </fileDesc>
  </teiHeader>
  <text>
    <body>
      <div
        n="preface"
        type="prefatory"> </div>
<table>    
<row role="header">
            <cell ana="abraham"/>
            <cell ana="assyrians">Regnum Assyriorum</cell>
            <cell ana="sacred-history"/>
            <cell ana="hebrews"> Hebraeorum gentis exordium</cell>
            <cell ana="sicyonians"> Regnum Sicyoniorum</cell>
            <cell ana="gentile-history"/>
            <cell ana="egyptians"> Regnum Aegyptiorum</cell>
            <cell ana="adbc"> BC</cell>
</row>   
<row role="regnal">
            <cell/>
            <cell/>
            <cell/>
            <cell/>
            <cell>Sicyoniorum III, TELCHIN, annis XX.</cell>
</row>
<row>
            <cell/>
            <cell>15</cell>
            <cell/>
            <cell>25</cell>
            <cell>1</cell>
            <cell/>
            <cell>25</cell>
            <cell>1992</cell>
</row>
</table>
</body>
</text>
</TEI>

当我运行我的脚本时,我得到了相同的输出,但每个标签中都有“tei:”:

<tei:TEI> 
<tei:text> 
<tei:body> 
<tei:div>
<tei:row role="header">...........

我正在尝试为每一行添加一个值,该值不用作标题并且不标记标尺的更改。我的代码是:

    import groovy.xml.StreamingMarkupBuilder
    import groovy.xml.XmlUtil

    def TEI = new XmlSlurper().parse(new File('file.xml'))
    def jeromeRow = new File("file-row.xml")
    def x = 0 


    for (row in TEI.text.body.div.table.row) {
    if (row.@role != 'regnal' && row.@role != 'header'){
    x = x + 1
    row.@n = 'r' + x 
    }
    }

def outputBuilder = new StreamingMarkupBuilder()
String result = outputBuilder.bind{ mkp.yield TEI }
jeromeRow << XmlUtil.serialize(result)

如何防止我的脚本对我的 xml 文件进行这种不必要的更改。

【问题讨论】:

  • 你能粘贴实际的输入和输出吗?您显示的内容中没有角色属性或 n 属性
  • 实际输入输出差不多35000行。我希望这能让你更好地了解我在看什么。谢谢。
  • 该输入不会给出您所看到的tei: 前缀...您有更好的例子吗?也许在运行问题中的代码时会出现您描述的问题? (问题中的代码目前不会执行任何操作,因为 TEI.text.body.div.table.row 由于 table 子句不包含任何内容)

标签: xml serialization groovy streamingmarkupbuilder


【解决方案1】:

除了不存在的“表”之外,您的代码看起来是正确的。当我在 groovyConsole 中运行以下命令时,它看起来还不错:

import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
def xmlText = """<TEI> 
<text> 
<body> 
<div>
<row role="header">
            <cell ana="abraham"/>
            <cell ana="assyrians">Regnum Assyriorum</cell>
            <cell ana="sacred-history"/>
            <cell ana="hebrews"> Hebraeorum gentis exordium</cell>
            <cell ana="sicyonians"> Regnum Sicyoniorum</cell>
            <cell ana="gentile-history"/>
            <cell ana="egyptians"> Regnum Aegyptiorum</cell>
            <cell ana="adbc"> BC</cell>
</row>   
<row role="regnal">
            <cell/>
            <cell/>
            <cell/>
            <cell/>
            <cell>Sicyoniorum III, TELCHIN, annis XX.</cell>
</row>
<row>
            <cell/>
            <cell>15</cell>
            <cell/>
            <cell>25</cell>
            <cell>1</cell>
            <cell/>
            <cell>25</cell>
            <cell>1992</cell>
</row>
</div>
</body>
</text>
</TEI>"""

def TEI = new XmlSlurper().parseText(xmlText)
def x=1
for (row in TEI.text.body.div.row) {
    if (row.@role != 'regnal' && row.@role != 'header'){
      row.@n = 'r' + x++
    }
}
def outputBuilder = new StreamingMarkupBuilder()
String result = outputBuilder.bind{ mkp.yield TEI }

println XmlUtil.serialize(result)

再次查看您的代码,我看到您最后将数据附加到文件末尾。

jeromeRow << XmlUtil.serialize(result)

您是否出于某种原因(在未提交的代码中)将空数据附加到已经不正确的文件中?

【讨论】:

  • 正如我上面评论的,这个问题缺少一些东西
  • 蒂姆是对的。我确实省略了 元素。我只是想避免在问题中加入数千行,而那一行从我身边溜走了。我运行了您的脚本 Joachim,但仍然遇到了同样的问题。每个元素标签都添加了“tei:”。但我会在编辑中添加我的 xml 文件的标题。
  • @mpk 这很有趣,在我的版本中没有。所以我认为这是一个版本问题。我的版本:“Groovy 版本:2.4.5 JVM:1.8.0_65 供应商:Oracle Corporation OS:Linux”。我在 groovyConsole 中运行代码
  • 我使用的是 2.4.5,但在 OSX 上的终端中运行代码。我试图从 groovyConsole 运行代码,移动了我的文件,但无法让代码找到文件。我会再玩一些。感谢您的帮助。
【解决方案2】:

如果你改变了

def TEI = new XmlSlurper().parse(new File('file.xml'))

def TEI = new XmlSlurper(false, false).parse(new File('file.xml'))

它会关闭 slurper 中的验证和命名空间处理,您应该会得到预期的结果

【讨论】:

  • 不要忘记感谢 tim 解决了您的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 2010-12-04
  • 2011-05-19
相关资源
最近更新 更多