Page 1 of 1

HowTo get CDATA value from XMLNode

PostPosted: Tue Apr 16, 2019 3:28 pm
by derk.hulshof
Hi all,

I use the plugins.XmlReader.readXmlDocumentFromString
Then looping through the childs (getChildNodes())

One of the values inside the childnodes is CDATA[] .
But can't figure out how to get the value from it with the options from the plugin.

There is only getTextValue method, this one returns null.

Someone figured this out already?

Tnx

Re: HowTo get CDATA value from XMLNode

PostPosted: Sat Apr 27, 2019 6:22 am
by Joas
I don't think the XMLReader plugin can do that.
But why not use the built-in XML support of javascript (E4X)? It is not used anymore in modern browsers, but In Servoy/Rhino it works great.

You can do something like this:
Code: Select all
   var xml = <xml><foo>No CDATA</foo><bar><![CDATA[Value inside CDATA]]></bar></xml>;
   
   for each (var node in xml.*) {
      application.output(node.valueOf());
   }

And you will just get your values, the CDATA tags are stripped away automatically:
e4x.png
e4x.png (2.99 KiB) Viewed 3008 times


See this for more info.

Re: HowTo get CDATA value from XMLNode

PostPosted: Tue Apr 30, 2019 1:33 pm
by derk.hulshof
Hi Joas,
Tnx for your reply.

I call an external api url
var _client = plugins.http.createNewHttpClient()
var _post = _client.createPostRequest( _url );
_post.setBodyContent( _xmlpost , 'text/xml' )
var _response = _post.executeRequest();
var _answer = _response.getResponseBody();

Now the ResponseBody gives me "<?xml.......><tag1>value<tag1><entries><tagchild1>value</tagchild1></entries>"

What is the best way to get the XML from this. So to get: <tag1>value<tag1><entries><tagchild1>value</tagchild1></entries>
To read it like json/the way above you mentioned

Because jquery is not available DOMParser is not available.
Or am I missing something

Re: HowTo get CDATA value from XMLNode

PostPosted: Tue Apr 30, 2019 8:03 pm
by Joas
First you need to remove the <?xml...> tag and then you can use the XML() constructor:
Code: Select all
   _answer = _answer.replace(/<?.*?>/, "");
   var _xml = new XML(_answer);

Re: HowTo get CDATA value from XMLNode

PostPosted: Mon May 06, 2019 10:37 am
by derk.hulshof
Hi Joas,

The new XML didnt work.

But fixed it with some replace all searches.
Then putted it into an array.

Thanks for the advice, it is working now.