0

尝试添加&为元素值之一,但StreamMarkupBuilder无法识别&&
代码为

def buildXml {
   def requestXml = {
     RootElement {
        Element1("&Value1")
        Element2("Value2")
     }
   }
 return new StreamingMarkupBuilder().bind(requestXml)
}

输出是

<RootElement>
  <Element1>&amp;Value1</Element1>
  <Element2>Value2</Element2>
</RootElement>

预期产出

<RootElement>
      <Element1>&Value1</Element1>
      <Element2>Value2</Element2>
</RootElement>

我遇到了MarkupBuildersetEscapeAttributes()方法。医生说

Defaults to true. If set to false then you must escape any special characters within attribute values such as '&', '<', CR/LF, single and double quotes etc. manually as needed. The builder will not guard against producing invalid XML when in this mode and the output may not be able to be parsed/round-tripped but it does give you full control when producing for instance HTML output.

寻找类似的东西StreamMarkupBuilder

4

1 回答 1

1

你应该善待你所拥有的。我支持 Jim 和 mzjn。最终,遗留系统将解析 xml 以获得预期的开头&

def requestXml = {
  RootElement {
     Element1("&Value1")
     Element2("Value2")
  }
}

def xml = new groovy.xml.StreamingMarkupBuilder().bind(requestXml)

//Legacy backend reading/parsing the xml 
//should reads escaped characters appropriately.
def slurper = new XmlSlurper().parseText(xml.toString())
assert slurper.Element1 == "&Value1"
于 2013-10-09T18:09:42.960 回答