0

I am fetching a part of xml using XmlSlurper, when xml has <node> </node>(value with 2 space), its converting it as <node/>, but i want to keep it as original XML

Original XML <node> </node>

Converted by XmlSlurper: <node/>

how to avoid XML conversion to <node/>

i am using below code

XmlSlurper slurper = new XmlSlurper();
def xml = slurper.parseText(testxml);
def node=XmlUtil.serialize(xml.MessageRequest.LineSize[1]);
msg.put("node",node);

1 Answer 1

2

use slurper.setKeepWhitespace(true) method to instruct slurper to keep whitespaces in values

import groovy.xml.*

def testxml = '''<root><a><node>  </node></a></root>'''

XmlSlurper slurper = new XmlSlurper();
slurper.setKeepWhitespace(true);         //<<------ this line
def xml = slurper.parseText(testxml);
def node=XmlUtil.serialize(xml.a);

result:

<?xml version="1.0" encoding="UTF-8"?><a>
  <node>  </node>
</a>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.