Escape '<' and '>' sign while using XPath in Java
I’m using the Java XPath API to modify and work with XML nodes. Reading in the document using DocumentBuilder and writing back to the document using the Transformer API.
try { DocumentBuilderFactory f = DocumentBuilderFactory.newInstance(); DocumentBuilder b = f.newDocumentBuilder(); String pathToFile = ReadConfigFile.readPathToSampleFactsFile(); Document doc = b.parse(new File(pathToFile)); XPath xPath = XPathFactory.newInstance().newXPath(); Node maxSubjects = (Node) xPath.compile("//*[contains(@name, 'max_subjects')]").evaluate(doc, XPathConstants.NODE); maxSubjects.setTextContent("<xml>"); Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty(OutputKeys.METHOD, "xml"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5"); DOMSource domSource = new DOMSource(doc); String fileName = BuildXML.buildXMLFileName(simPar)+".facts"; StreamResult sr = new StreamResult(new File(fileName)); tf.transform(domSource, sr); }
I would like to set the text content of a node with some XML tag. I have used an example here for the maxSubjects node. I am not able to sucessfully escape the lesser-than and greater-than symbols though.
I tried escaping the lesser-than symbol with < and with the Unicode representation. However, it is not properly represented in the XML generated. The < get represented as < in the generated XML
I would like to set the text content of a node with some XML tag
There’s some confused thinking there. A node can contain child nodes, and it can have text content, but the text content is a string. Characters in the string such as "<" are ordinary characters, not markup. You can either add child nodes, or you can set the text content, but you can’t supply lexical XML as the text content and expect it to be magically parsed into a tree of nodes.
You cannot do what you want this way. This is a common issue when learning how to think when writing XSLT.
What you see is how literal <
and >
in text should be encoded to ensure that when parsed is passed up correctly as the two characters.
As you are running a XSLT transformation inside your Java program, you should just write your XSLT to have a template matching the text node and then replace it with the first part of the text, followed by the XML tags you need, and then the remainder of the text.