You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some of the UTF that might be returned from an API call might be invalid for Karate to process.
I was getting the error: SAXParseException; 0; Content is not allowed in prolog. when parsing XML when trying to parse a XML from a API.
Even though I tried to replace part of the String, I could not get it to work.
My solution was to create a Java function and call it in my karate file. The Java function replace all invalid characters, fix the UTF and remove the namespaces making it easier to parse the XML.
In my .feature file, after making a call to an API, I am calling my Java function with: * xml response = cleanUpKarateXml(response)
My Java function in the backend looks like:
public static String cleanUpKarateXml(Object oldxml) {
try {
String xml = oldxml.toString()
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
transformer.setOutputProperty( OutputKeys.INDENT, "false" );
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
Document xmlDoc = builder.parse(inputSource);
Node root = xmlDoc.getDocumentElement();
NodeList rootchildren = root.getChildNodes();
Element newroot = xmlDoc.createElement(root.getNodeName());
for (int i = 0; i < rootchildren.getLength(); i++) {
newroot.appendChild(rootchildren.item(i).cloneNode(true));
}
xmlDoc.replaceChild(newroot, root);
DOMSource requestXMLSource = new DOMSource( xmlDoc.getDocumentElement() );
StringWriter requestXMLStringWriter = new StringWriter();
StreamResult requestXMLStreamResult = new StreamResult( requestXMLStringWriter );
transformer.transform( requestXMLSource, requestXMLStreamResult );
String modifiedRequestXML = requestXMLStringWriter.toString();
return modifiedRequestXML;
} catch (Exception e) {
System.out.println("Could not parse message as xml: " + e.getMessage());
}
return "";
}
I am raising this issue because I believe having this function as part of Karate would be very helpful to a lot of people
The text was updated successfully, but these errors were encountered:
Some of the UTF that might be returned from an API call might be invalid for Karate to process.
I was getting the error: SAXParseException; 0; Content is not allowed in prolog. when parsing XML when trying to parse a XML from a API.
Even though I tried to replace part of the String, I could not get it to work.
My solution was to create a Java function and call it in my karate file. The Java function replace all invalid characters, fix the UTF and remove the namespaces making it easier to parse the XML.
In my .feature file, after making a call to an API, I am calling my Java function with:
* xml response = cleanUpKarateXml(response)
My Java function in the backend looks like:
I am raising this issue because I believe having this function as part of Karate would be very helpful to a lot of people
The text was updated successfully, but these errors were encountered: