HowTo get CDATA value from XMLNode

Questions and answers on designing your Servoy solutions, database modelling and other 'how do I do this' that don't fit in any of the other categories

HowTo get CDATA value from XMLNode

Postby derk.hulshof » Tue Apr 16, 2019 3:28 pm

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
derk.hulshof
 
Posts: 96
Joined: Tue Dec 03, 2013 9:18 am
Location: Netherlands

Re: HowTo get CDATA value from XMLNode

Postby Joas » Sat Apr 27, 2019 6:22 am

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 369 times


See this for more info.
Joas de Haan
Yield Software Development: Need help on your project?
yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 810
Joined: Mon Mar 20, 2006 4:07 pm
Location: Amersfoort, NL

Re: HowTo get CDATA value from XMLNode

Postby derk.hulshof » Tue Apr 30, 2019 1:33 pm

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
derk.hulshof
 
Posts: 96
Joined: Tue Dec 03, 2013 9:18 am
Location: Netherlands

Re: HowTo get CDATA value from XMLNode

Postby Joas » Tue Apr 30, 2019 8:03 pm

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);
Joas de Haan
Yield Software Development: Need help on your project?
yieldsd.com
User avatar
Joas
Site Admin
 
Posts: 810
Joined: Mon Mar 20, 2006 4:07 pm
Location: Amersfoort, NL

Re: HowTo get CDATA value from XMLNode

Postby derk.hulshof » Mon May 06, 2019 10:37 am

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.
derk.hulshof
 
Posts: 96
Joined: Tue Dec 03, 2013 9:18 am
Location: Netherlands


Return to Programming with Servoy

Who is online

Users browsing this forum: No registered users and 4 guests