You can use apache common text library to escape a string.
org.apache.commons.text.StringEscapeUtils
# For XML 1.0
String escapedXml = StringEscapeUtils.escapeXml10("the data might contain & or ! or % or ' or # etc");
# For XML 1.1
String escapedXml = StringEscapeUtils.escapeXml11("the data might contain & or ! or % or ' or # etc");
But what you are looking for is a way to convert any string into a valid XML tag name. For ASCII characters, XML tag name must begin with one of _:a-zA-Z and followed by any number of character in _:a-zA-Z0-9.-
I believe there is no library to do this for you so you have to implement your own function to convert from any string to match this pattern or alternatively make it into a value of attritbue.
<property name="no more need to be encoded, it should be handled by XML library">0.0</property>
Answer from gigadot on Stack OverflowYou can use apache common text library to escape a string.
org.apache.commons.text.StringEscapeUtils
# For XML 1.0
String escapedXml = StringEscapeUtils.escapeXml10("the data might contain & or ! or % or ' or # etc");
# For XML 1.1
String escapedXml = StringEscapeUtils.escapeXml11("the data might contain & or ! or % or ' or # etc");
But what you are looking for is a way to convert any string into a valid XML tag name. For ASCII characters, XML tag name must begin with one of _:a-zA-Z and followed by any number of character in _:a-zA-Z0-9.-
I believe there is no library to do this for you so you have to implement your own function to convert from any string to match this pattern or alternatively make it into a value of attritbue.
<property name="no more need to be encoded, it should be handled by XML library">0.0</property>
public class RssParser {
int length;
URL url;
URLConnection urlConn;
NodeList nodeList;
Document doc;
Node node;
Element firstEle;
NodeList titleList;
Element ele;
NodeList txtEleList;
String retVal, urlStrToParse, rootNodeName;
public RssParser(String urlStrToParse, String rootNodeName){
this.urlStrToParse = urlStrToParse;
this.rootNodeName = rootNodeName;
url=null;
urlConn=null;
nodeList=null;
doc=null;
node=null;
firstEle=null;
titleList=null;
ele=null;
txtEleList=null;
retVal=null;
doc = null;
try {
url = new URL(this.urlStrToParse);
// dis is path of url which v'll parse
urlConn = url.openConnection();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
String s = isToString(urlConn.getInputStream());
s = s.replace("&", "&");
StringBuilder sb =
new StringBuilder
("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
sb.append("\n"+s);
System.out.println("STR: \n"+sb.toString());
s = sb.toString();
doc = db.parse(urlConn.getInputStream());
nodeList = doc.getElementsByTagName(this.rootNodeName);
// dis is d first node which
// contains other inner element-nodes
length =nodeList.getLength();
firstEle=doc.getDocumentElement();
}
catch (ParserConfigurationException pce) {
System.out.println("Could not Parse XML: " + pce.getMessage());
}
catch (SAXException se) {
System.out.println("Could not Parse XML: " + se.getMessage());
}
catch (IOException ioe) {
System.out.println("Invalid XML: " + ioe.getMessage());
}
catch(Exception e){
System.out.println("Error: "+e.toString());
}
}
public String isToString(InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[512];
for (int i; (i = in.read(b)) != -1;) {
out.append(new String(b, 0, i));
}
return out.toString();
}
public String getVal(int i, String param){
node =nodeList.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE)
{
System.out.println("Param: "+param);
titleList = firstEle.getElementsByTagName(param);
if(firstEle.hasAttribute("id"))
System.out.println("hasAttrib----------------");
else System.out.println("Has NOTNOT NOT");
System.out.println("titleList: "+titleList.toString());
ele = (Element)titleList.item(i);
System.out.println("ele: "+ele);
txtEleList = ele.getChildNodes();
retVal=(((Node)txtEleList.item(0)).getNodeValue()).toString();
if (retVal == null)
return null;
System.out.println("retVal: "+retVal);
}
return retVal;
}
}
As others have mentioned, using an XML library is the easiest way. If you do want to escape yourself, you could look into StringEscapeUtils from the Apache Commons Lang library.
Very simply: use an XML library. That way it will actually be right instead of requiring detailed knowledge of bits of the XML spec.
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 & Pat
Escaped : > than 10000
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 & Pat</personName>
<sal>> than 10000</sal>
</person>
To match you can use regular expression:
(?:<)(?<=<)(\/?\w*)(?=.*(?<=<\/html))(?:>)
(?:<)Match but don't capture<.(?<=<)Positive lookbehind for<.(\/?\w*)Capture tag name. Optional/and word characters.(?=.*(?<=<\/html))Positive lookahead, then positive lookbehind for closing tag.(?:>)Match but don't capture>.
To replace you can use:
<$1>
Where $1 is the result of the capture group in the regular expression.
You can test the regular expression interactively here.
Using the following Java code:
public static void main(String []args){
String xml = "<Message text=\"<html>Welcome User, <br> Happy to have you. <br>.</html>\" Multi=\"false\"><Meta source=\"system\" dest=\"any\"></Meta></Message>";
String newxml = replaceChars(xml);
System.out.println(newxml);
}
private static String replaceChars(String xml)
{
xml = xml.replaceAll("(?:<)(?<=<)(\/?\w*)(?=.*(?<=<\/html))(?:>)", "<$1>");
return xml;
}
The output is:
"<Message text="<html>Welcome User, <br> Happy to have you. </html>" Multi="false"> <Meta source="system" dest="any"></Meta></Message>"
Please do not use regular expressions to escape special characters in XML.
Can you guarantee that this will work for all possible html input with all of HTML and XML quirks (very extensive specs!!!) ?
Just use one of many utilities out there to escape XML strings.
Apache Commons is quite popular - please see this example