4

I have an object which I write to an xml. The xml has escape characters like "&", "<" etc. Now before I process this xml I want a utility to escape these special characters so that the resultant xml has & followed by "amp;" for "&" and "&" followed by "lt;" for "<". I tried StringUtils, XMLWriter and few more but they convert the "<" in opening and closing tags as well which I dont want. I only want "<" in the attribute values to be replaced. Please help. Example;

I have the input xml as this

<?xml version="1.0" encoding="UTF-8"?>
<personName><firstName>Sam & Pat </firstName>
<sal> > than 10000 </sal>
</personName>

And the expected xml should be `

<?xml version="1.0" encoding="UTF-8"?>
<personName><firstName>Sam &amp; Pat </firstName>
<sal> &lt; than 10000 </sal>
</personName>

If I am using StringUtils, it converts all the "<" characters like this

&lt;sal&gt; &lt; than 10000 &lt;/sal&gt;

EDIT: I can't actually use JaxB. I am using FreeMarkerTemplate to do this. Here is the code .

File tempFile = File.createTempFile(fileName, ".tmp");
try (FileWriter writer = new FileWriter(tempFile)) {
freeMarkerConfig.setOutputEncoding(UTF_8);
    Template template = freeMarkerConfig.getTemplate(templateName);
    template.process(data, writer);
}              `                                                                                                    
The resultant file which get created should have the handled escape characters.
3
  • 1
    Add a concrete example of what you want to achieve. Input and Output. Please read stackoverflow.com/help/how-to-ask Commented Aug 5, 2015 at 6:01
  • hi, I have added the example. Commented Aug 5, 2015 at 6:08
  • IMO After reading through this thread I believe your source should be responsible for sending a valid XML file. You can encode, escape the data as you wish, but a valid XML structure should not be your concern. Commented Feb 8, 2021 at 7:39

4 Answers 4

4

You can also use Apache Commons Lang Library for escaping the characters:

Example:

    String escapeString1 = "Sam & Pat ";
    System.out.println("Escaped : " + StringEscapeUtils.escapeXml11(escapeString1));

    String escapeString2 = " > than 10000";
    System.out.println("Escaped : " + StringEscapeUtils.escapeXml11(escapeString2));

Output:

Escaped : Sam &amp; Pat 
Escaped :  &gt; than 10000
Sign up to request clarification or add additional context in comments.

1 Comment

StringEscapeUtils is fine to use if its just a string. But using this api for xml string converts every occurrence of '<' and '>' which makes it a malformed xml. Example :- &lt;?xml version="1.0" encoding="UTF-8"?> &lt;personName> &lt;firstName>Sam &amp; Pat &lt;/firstName&gt; &lt;/personName&gt; &lt;sal&gt; &lt; than 10000 &lt;/sal&gt;
0

You can use JAXB for the XML generation. Annotate your Model-Class with @XmlRootElement

Then you can use JAXB for marshalling the XML-Object:

try {
  JAXBContext context = JAXBContext.newInstance(Person.class);

  Marshaller m = context.createMarshaller();
  m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

  Person object = new Person();
  object.setPersonName("Sam & Pat");
  object.setSal("> than 10000");

  m.marshal(object, System.out);
} catch (JAXBException e) {
  e.printStackTrace();
}

The output will be

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
    <personName>Sam &amp; Pat</personName>
    <sal>&gt; than 10000</sal>
</person>

1 Comment

I cant use JaxB as we have to use FreeMarkerTemplate to generate the xml file based on a *.ftl template. I have added the code above for that. I cant find any any way to achieve with freemarker.
0

Using CDATA will fix your problem.

Such as <![CDATA[abc]]>

1 Comment

I am not sure how to use CDATA as we get XML from the client and we cannot change it. Can you please elaborate this?
0

You can include XML special characters in XPL. XPL has exactly the same structure as XML, but allows the special characters in text fields. http://hll.nu/

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.