We have a webapp (standard Spring MVC, Java) - that I've written - that returns a response to our client (browser) as XML. The response is marshalled using JAXB and so on.
Within the ui - to interact with 3rd party software we need to pass part of the XML as is into a javascript method.
I.e everything from '<' in thirdPartyStuff to the final closing '>' - needs to be extracted.
After umpteen attempts and much debugging - I've come to a conclusion that this is the wrong way to do this. I believe I should wrap that part of the response in CDATA and extract it as .text() on what ever object... currently we're using jquery... the response comes back on an AJAX call...
But maybe it can be done .. currently we're tripping over trying to call .html() on an element - which as its XML - falls in a heap.
TIA.
:-)
Below is the kind of response structure app data for "us" and 'xml' for the third part. Only (& including) from " ... to " - needs to be extracted and passed as is (text string) to a 3rd party method.
<xml>
<someNode>
<ourStuff veryUseful="true"/>
<thirdPartyStuff a="1" b="2">
<moreStuff/>
<evenMoreStuff/>
</thirdPartyStuff>
</someNode>
</xml>
Below is a failing test, basically illustrating my ignorance. So of course the console.log must fail.
In reality the data comes back via an $.ajax() call - but this test illustrates the behaviour. It generates a "Uncaught TypeError: Cannot call method 'replace' of undefined " - as there is no innerHtml on an XML "node" - which of course I believe is absolutely correct, and documented as such on the jquery API docs.
Note - this fails for me in Chrome & IE on Windows, but works on Firefox (Mac OS)... :-|
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
function hackit() {
var data = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<xml>" +
"<thirdParty>" +
"<blah a=\"0\" b=\"0\">" +
"</blah>" +
"</thirdParty>" +
"</xml>";
$xmlData = $.parseXML(data);
$data = $($xmlData);
var thing = $data.find("thirdParty");
console.log(thing.html());
}
</script>
</head>
<body onload="hackit()">
Loaded.
</body>
</html>
I think I will end up going this route.