
Hi, I have a problem, in while parsing the xml with special characters apstrophe('). I am getting the exception Caused by: javax.xml.transform.TransformerException: misquoted literal... expected single quote!
I am passing the String adam's. when i parse this string in xml path. i am not able to fetch the adam's string in the xml.
Below is the example :
String titlepath=/InvestmentProposal/ProposedAssetAllocations/Scenarios/Scenario[IsSelected = '1']/Accounts/Account[AccountTitle='adam's']/Sleeve[IsModel='1']/AccountId/text()
method to fetch the xml: xmlManipulator.parseXPath(doc,titlePath);
here doc is nothing but the xml.
xmlManipulator.java
public class XMLManipulator {
private final Document doc; private final XPath xpath;
public String[] parseXPath(Document doc1,String xmlPath) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
int i;
XPathExpression expr = xpath.compile(xmlPath);
Object obj = expr.evaluate(doc1, XPathConstants.NODESET);
NodeList nodes = (NodeList) obj;
String[] result;
int nodeLength = nodes.getLength();
if (nodeLength == 0) {
return null;
} else {
result = new String[nodeLength];
for (i = 0; i < nodeLength; i++) {
result[i] = nodes.item(i).getNodeValue();
}
return result;
}
}
}
Please solve the issue that it in the xml it is searching as string adam's'.but in Xml is adam's only. How to unescape the apstophe in while passing . I tried to unescape the character (').
below is the sample one:
private String escape(String input) {
String result = "";
for (int i = 0; i < input.length(); i++) {
if ("'".indexOf(input.charAt(i)) >= 0) {
result += "\'";
} else {
result += input.charAt(i);
}
}
return result;
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.