没有 XML 声明的 GPathResult 到字符串
GPathResult to String without XML declaration
我正在使用
将GPathResult
转换为String
def gPathResult = new XmlSlurper().parseText('<node/>')
XmlUtil.serialize(gPathResult)
它工作正常,但我在 XML
前面得到了 XML 声明
<?xml version="1.0" encoding="UTF-8"?><node/>
如何在开头没有 <?xml version="1.0" encoding="UTF-8"?>
的情况下将 GPathResult
转换为 String
?
使用XmlParser
代替XmlSlurper
:
def root = new XmlParser().parseText('<node/>')
new XmlNodePrinter().print(root)
使用 new XmlNodePrinter(preserveWhitespace: true)
可能是您尝试做的事情的朋友。请参阅文档中的其余选项:http://docs.groovy-lang.org/latest/html/gapi/groovy/util/XmlNodePrinter.html.
这是 XmlUtil class 中的代码。您会注意到它在 xml 声明之前,因此只需复制并删除它就足够容易了:
private static String asString(GPathResult node) {
try {
Object builder = Class.forName("groovy.xml.StreamingMarkupBuilder").newInstance();
InvokerHelper.setProperty(builder, "encoding", "UTF-8");
Writable w = (Writable) InvokerHelper.invokeMethod(builder, "bindNode", node);
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + w.toString();
} catch (Exception e) {
return "Couldn't convert node to string because: " + e.getMessage();
}
}
您可以使用 XmlNodePrinter 并传递自定义编写器,因此它不会打印输出,而是打印到字符串:
public static String convert(Node xml) {
StringWriter stringWriter = new StringWriter()
XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(stringWriter))
nodePrinter.print(xml)
return stringWriter.toString()
}
您仍然可以使用 XmlSlurper 而不是使用序列化它并先替换 replaceFirst
def oSalesOrderCollection = new XmlSlurper(false,false).parse(xas)
def xml = XmlUtil.serialize(oSalesOrderSOAPMarkup).replaceFirst("<\?xml version=\"1.0\".*\?>", "");
//Test the output or just print it
File outFile = new File("aes.txt")
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF8"));
out.append(xml.toString())
out.flush()
out.close()
GroovyCore 片段 :
/**
* Transforms the element to its text equivalent.
* (The resulting string does not contain a xml declaration. Use {@code XmlUtil.serialize(element)} if you need the declaration.)
*
* @param element the element to serialize
* @return the string representation of the element
* @since 2.1
*/
public static String serialize(Element element) {
return XmlUtil.serialize(element).replaceFirst("<\?xml version=\"1.0\".*\?>", "");
}
}
我正在使用
将GPathResult
转换为String
def gPathResult = new XmlSlurper().parseText('<node/>')
XmlUtil.serialize(gPathResult)
它工作正常,但我在 XML
前面得到了 XML 声明<?xml version="1.0" encoding="UTF-8"?><node/>
如何在开头没有 <?xml version="1.0" encoding="UTF-8"?>
的情况下将 GPathResult
转换为 String
?
使用XmlParser
代替XmlSlurper
:
def root = new XmlParser().parseText('<node/>')
new XmlNodePrinter().print(root)
使用 new XmlNodePrinter(preserveWhitespace: true)
可能是您尝试做的事情的朋友。请参阅文档中的其余选项:http://docs.groovy-lang.org/latest/html/gapi/groovy/util/XmlNodePrinter.html.
这是 XmlUtil class 中的代码。您会注意到它在 xml 声明之前,因此只需复制并删除它就足够容易了:
private static String asString(GPathResult node) {
try {
Object builder = Class.forName("groovy.xml.StreamingMarkupBuilder").newInstance();
InvokerHelper.setProperty(builder, "encoding", "UTF-8");
Writable w = (Writable) InvokerHelper.invokeMethod(builder, "bindNode", node);
return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + w.toString();
} catch (Exception e) {
return "Couldn't convert node to string because: " + e.getMessage();
}
}
您可以使用 XmlNodePrinter 并传递自定义编写器,因此它不会打印输出,而是打印到字符串:
public static String convert(Node xml) {
StringWriter stringWriter = new StringWriter()
XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(stringWriter))
nodePrinter.print(xml)
return stringWriter.toString()
}
您仍然可以使用 XmlSlurper 而不是使用序列化它并先替换 replaceFirst
def oSalesOrderCollection = new XmlSlurper(false,false).parse(xas)
def xml = XmlUtil.serialize(oSalesOrderSOAPMarkup).replaceFirst("<\?xml version=\"1.0\".*\?>", "");
//Test the output or just print it
File outFile = new File("aes.txt")
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF8"));
out.append(xml.toString())
out.flush()
out.close()
GroovyCore 片段 :
/**
* Transforms the element to its text equivalent.
* (The resulting string does not contain a xml declaration. Use {@code XmlUtil.serialize(element)} if you need the declaration.)
*
* @param element the element to serialize
* @return the string representation of the element
* @since 2.1
*/
public static String serialize(Element element) {
return XmlUtil.serialize(element).replaceFirst("<\?xml version=\"1.0\".*\?>", "");
}
}