I understand that XML has 5 special characters that MUST be escaped (",',<,>,&) I am trying to implement the following:
Input xml:
<?xml version = "1.0"?>
<class>
<student id = "999">
<firstname>Tes"Ting</firstname>
<lastname>He'llo</lastname>
<nickname1>W<or>ld</nickname>
<nickname2>star&wars</nickname2>
</student>
</class>
Output XML:
<?xml version = "1.0"?>
<class>
<student id = "999">
<firstname>Tes"Ting</firstname>
<lastname>He'llo</lastname>
<nickname>W<orl>d</nickname>
<nickname2>star&wars</nickname2>
</student>
</class>
Following is my code which works fine if there is single quote (') and double quotes ("). When the code finds &, <, >..the XML parser throws an error. Can anyone please suggest how to implement? any thoughts?
import org.xml.sax.SAXException;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.IOException;
import com.vordel.trace.Trace;
import org.xml.sax.InputSource;
import org.apache.commons.lang.StringEscapeUtils;
========Logic=====
def input = <input xml in string>
def temp;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(input)));
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("student");
for (temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
escapedfirstname= StringEscapeUtils.escapeXml(eElement.getElementsByTagName("firstname").item(0).getTextContent() );
escapedlastname= StringEscapeUtils.escapeXml(eElement.getElementsByTagName("lastname").item(0).getTextContent() );
}
}