네임 스페이스가 보존 된 그루비 노드를 어떻게 인쇄합니까?
-
03-07-2019 - |
문제
이 코드를 사용하여 일부 XML을 출력 할 때. XmlParser
XmlParser parser = new XmlParser()
def root = parser.parseText(feedUrl.toURL().text)
def writer = new StringWriter()
new XmlNodePrinter(new PrintWriter(writer)).print(root)
println writer.toString()
루트 노드의 네임 스페이스 선언은 toString()
의 뿌리... 어떤 아이디어?
해결책
실제로 네임 스페이스 컨텍스트가 필요한 노드와 함께 네임 스페이스 컨텍스트를 포함하여 출력을 제거하고 네임 스페이스 컨텍스트를 포함하는 것처럼 보입니다.
예를 들어,이 질문의 웹 페이지에는 CreativeCommons 네임 스페이스가 포함됩니다.
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:thr="http://purl.org/syndication/thread/1.0">
<!-- snip -->
<creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf</creativeCommons:license>
<!-- snip -->
</feed>
이 스크립트를 사용하여 XML을 출력 할 때 :
def root = new XmlParser().parseText("http://stackoverflow.com/feeds/question/227447".toURL().text)
println new XmlNodePrinter().print(root)
네임 스페이스를 해당 네임 스페이스가 필요한 라이센스 노드로 이동하게됩니다. 해당 네임 스페이스에 단일 노드 만 있기 때문에이 경우에는 큰 거래가 아닙니다. XML의 대부분이 네임 스패닝 된 경우, 아마도 조금 더 부풀어 오른 것일 것입니다.
<feed xmlns="http://www.w3.org/2005/Atom">
<!-- snip -->
<creativeCommons:license xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule">
http://www.creativecommons.org/licenses/by-nc/2.5/rdf
</creativeCommons:license>
<!-- snip -->
</feed>
실제로 노드가 정규화되기를 원한다면 XMLNodePrinter를 조정하여 XML을 통과하여 2 개의 패스를 수행하여 먼저 사용 된 모든 네임 스페이스를 수집하고 2nd를 모아서 각 네임 스패닝 노드가 아닌 상단에서 출력해야합니다. 그루비 소스 코드는 실제로 읽기 쉬우 며 실제로 필요한 경우 수정하기가 어렵지 않습니다.
다른 팁
나는 방금 같은 문제를 겪었고 약간의 충격을받은 후 해결 방법을 찾았습니다.
당신은 사용합니다 xmlsluper 대신 XMLPARSER 그리고 사용 StreamingMarkupBuilder 대신에 xmlnodeprinter. 그런 다음 폐쇄를 활용합니다 묶다 그리고 사용하십시오 MKP 내장 변수 네임 스페이스를 선언 할 수 있습니다.
예를 들어; 위의 TED의 소스 XML 예제 사용 :
def root = new XmlSlurper().parseText("http://stackoverflow.com/feeds/question/227447".toURL().text))
def outputBuilder = new StreamingMarkupBuilder()
String result = XmlUtil.serialize(outputBuilder.bind {
mkp.declareNamespace('':'http://www.w3.org/2005/Atom')
mkp.declareNamespace('creativeCommons':'http://backend.userland.com/creativeCommonsRssModule')
mkp.declareNamespace('re':'http://purl.org/atompub/rank/1.0')
mkp.yield root }
)
println result
결과 :
<?xml version="1.0" encoding="UTF-8"?><feed xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns="http://www.w3.org/2005/Atom" xmlns:re="http://purl.org/atompub/rank/1.0">
<title type="text">How do I print a groovy Node with namespace preserved? - Stack Overflow </title>
<link rel="self" type="application/atom+xml" href="http://stackoverflow.com/feeds/question/227447"/>
<link rel="alternate" type="text/html" href="http://stackoverflow.com/questions/227447"/>
<subtitle>most recent 30 from stackoverflow.com</subtitle>
<updated>2011-02-16T05:13:17Z</updated>
<id>http://stackoverflow.com/feeds/question/227447</id>
<creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf</creativeCommons:license>
<entry>
<id>http://stackoverflow.com/questions/227447/how-do-i-print-a-groovy-node-with-namespace-preserved</id>
<re:rank scheme="http://stackoverflow.com">2</re:rank>