CDATA not supporting to read data from XMLList - actionscript-3

XML :
<catering>
<contents>
<![CDATA[
<title>UPCOMING EVENTS</title>
<info _title = "<font size='14' color='#ffffff'>title1</font" image="null"></info>
<info _title = "title2" image="images/events/slide1.jpg"></info>
<info _title = "title3 " image="images/events/slide2.jpg"></info>
<info _title = "title4" image="images/events/slide3.jpg"></info>
<info _title = "title5" image="images/events/slide4.jpg"></info>
]]
</contents>
</catering>
CODE
eventTitle = loadXMLC.events_1_Contents.contents.title;
xmlList_1 = loadXMLC.events_1_Contents.contents.info;
for(i = 0;i < xmlList_1.#_title.length(); i++)
{
events_0_info.push(xmlList_1.#_title[i]);
img_ary0.push(xmlList_1.#image[i]);
}
Without CDATA I can able to read the XML. What do I needs to do read the data with CDATA?
I want to use html tag for each attributes.

Here is a quick exemple :
var data : XML = <catering>
<contents>
<![CDATA[
<title>UPCOMING EVENTS</title>
<info _title = "title2" image="images/events/slide1.jpg"></info>
<info _title = "title3 " image="images/events/slide2.jpg"></info>
<info _title = "title4" image="images/events/slide3.jpg"></info>
<info _title = "title5" image="images/events/slide4.jpg"></info>
]]>
</contents>
</catering>;
The nodes between CDATA tag are seen as text (a block of text). You have to convert this text into a valid XML in order to access data.
trace(XML(data.contents.children()).nodeKind());//text
var contents : XMLList = XML("<c>"+data.contents.toString()+"</c>").children();
trace(contents.toXMLString());
I removed this line from your sample :
<info _title = "<font size='14' color='#ffffff'>title1</font" image="null"></info>
Flash was unable to convert this line into XML...

I think it's because of a typo where you're closing the CDATA tag. Try adding > at the end like so:
<catering>
<contents>
<![CDATA[
<title>UPCOMING EVENTS</title>
<info _title = "<font size='14' color='#ffffff'>title1</font" image="null"></info>
<info _title = "title2" image="images/events/slide1.jpg"></info>
<info _title = "title3 " image="images/events/slide2.jpg"></info>
<info _title = "title4" image="images/events/slide3.jpg"></info>
<info _title = "title5" image="images/events/slide4.jpg"></info>
]]>
</contents>
</catering>
and you'll be good to go.
EDIT
As #OXMO456 mentions. Flash parsing won't go past the first _title attribute. I think the way you're using the xml attributes is a bad practice. In essence attributes contain information about the element, not the content of the element itself.
Maybe you should try adjusting the xml structure a bit and try parsing that. Maybe something like this could work:
<title>UPCOMING EVENTS</title>
<info>
<title size="14" color="#ffffff">title1</title>
</info>
<info>
<title>title2</title>
<image>images/events/slide2.jpg</image>
</info>
<info>
<title>title3</title>
<image>images/events/slide3.jpg</image>
</info>
<info>
<title>title4</title>
<image>images/events/slide4.jpg</image>
</info>

Related

Unable to use <link> tag in Razor View

If I use the following code the view renders fine.
But if I change the url to the necessary RSS spec. the view will not render and throws an error saying that the tag is invalid so the error is occurring at the link tag. No matter what I try the link tag inside the razor foreach will not compile correctly.
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.RSsfeed>
#using ContentModels = Umbraco.Web.PublishedContentModels;
#{
Layout = null;
Response.ContentType = "text/xml";
var rootNode = Umbraco.TypedContentAtRoot().First();
var newsNodes = umbraco.uQuery.GetNodesByType("newsDetail");
}<?xml version="1.0"?>
<!-- News Aritcles -->
<rss version="2.0" xmlns:newsArticles="https://xxx.xxxxxxx.xxx/news">
<channel>
<title>News Aritcles</title>
<link>https://xxx.xxxxxxx.xxx/news</link>
<description>News Aritcles</description>
<language>en-us</language>
<ttl>1440</ttl>
#foreach(var newsNode in newsNodes){
var newsContent = UmbracoContext.Current.ContentCache.GetById(newsNode.Id);
string nnDescription = newsContent.GetPropertyValue("description").ToString();
string nnPublishDate = newsContent.GetPropertyValue("publishDate").ToString();
<item>
<title>#newsNode.Name</title>
<url>https://xxx.xxxxxxx.xxx#{#newsNode.Url}</url>
<description>#nnDescription</description>
<pubDate>#nnPublishDate</pubDate>
<guid>https://xxx.xxxxxxx.xxx#{#newsNode.Url}</guid>
</item>
}
</channel>
</rss>
<link/> is a void element, and so only has a start tag and no end tag - See W3C HTML Language Reference
You could output the tag like this
#("<link>" + newsNode.Url + "</link>")
Hope this helps

Displaying multiple xml information in html via ajax

I have the AirPlayHistory.xml file that shows the last 3 streamed songs and is updated every time the song changes..
AirPlayHistory:
<Event status="happened">
<Song title="Forbidden Voices">
<Artist name="Martin Garrix" ID="344518"> </Artist>
<Info StartTime="11:06:31" JazlerID="7235" PlayListerID=""/>
</Song>
<Song title="Faded">
<Artist name="Alan Walker" ID="343769"> </Artist>
<Info StartTime="11:10:28" JazlerID="7769" PlayListerID=""/>
</Song>
<Song title="Afterlife">
<Artist name="Illenium" ID="344414"> </Artist>
<Info StartTime="11:13:30" JazlerID="10668" PlayListerID=""/>
</Song>
</Event>
I have this code:
var speed = 10000; // 10 seconds
function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};
ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e) {try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};
function process(xml) {
document.getElementById('contentfile').innerHTML=xml;
var title = document.getElementById('contentfile').getElementsByTagName('Song')[0].title;
var name = document.getElementById('contentfile').getElementsByTagName('Artist')[0].name;
document.getElementById('contentfile').innerHTML='NOW ON AIR: '+name+' | '+title;
}
function checkXml() {
ajax.get('http://website.com/jazler/NowOnAir.xml',process)
}
window.onload=function() {
checkXml();
tId=setInterval('checkXml()',speed)
}
<div id="contentfile">
but he shows only one song of 3..
Clear! you have the first only you write name and title!! you simply rewrote the innerHTML BUT code line document.getElementById('contentfile').innerHTML=xml; is not good you have to loop through! now you write all inner.HTMl content with the first element title[0] name[0]

Converting this XMLList to XML

var xml:XML = new XML(
<root>
<message for="Harry" > adlfjljfa </message>
<message for="Harry" > ajf ja; jafja </message>
<message for="Akil"> difasfjlfjals </message>
var xmlList:XMLList = xml.message.(#for== "Harry" )
var nameXML:XML = XML(xmlList)
trace( nameXML )
Right now the nameXML doesnot form correctly, and throws an error : The markup in the document following the root element must be well formed.
Thanks
You didnt close the root tag in the XML declaration.
I replaced the "for" attribute because its reserved.
var xml:XML =
<root>
<message dest="Harry" > adlfjljfa </message>
<message dest="Harry" > ajf ja; jafja </message>
<message dest="Akil"> difasfjlfjals </message>
</root>
var xmlList:XMLList = xml.message.(#dest== "Harry" )
for each (var item:XML in xmlList) {
trace(item)
}
output
adlfjljfa
ajf ja; jafja
Edit
You can also keep the "for" attribute :
var xmlList:XMLList = xml.message.(attribute('for')== "Harry" )

Get prefix of XML namespace from URI

How can I get the prefix of a xml namespace from URI?
example:
<Name xmlns:tiger="http://www.census.gov">
</Name>
I've got the
"http://www.census.gov"
I want to get the prefix
tiger
How can I do this in Actionscript / Flex?
thx
EDIT
The answer doesn't work with this complex example:
<Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
tiger:poly_landmarks
</Name>
I've got empty Array.
ANSWER MY OWN Q
for EDIT example
var xml:XML = <Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
tiger:poly_landmarks
</Name>
var ns:Namespace = xml.namespace("http://www.census.gov");
if(ns.uri == "http://www.census.gov")
....
var xml:XML = <Name xmlns:tiger="http://www.census.gov"></Name>;
var ns:Array = xml.namespaceDeclarations();
trace(ns[0].prefix); //output: tiger
UPD for complex xml (output tiger as well):
var xml:XML = <Name xmlns:tiger="http://www.census.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.opengis.net/wfs" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ows="http://www.opengis.net/ows" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:it.geosolutions="http://www.geo-solutions.it" xmlns:cite="http://www.opengeospatial.net/cite" xmlns:worldWS="worldWS" xmlns:sde="http://geoserver.sf.net" xmlns:topp="http://www.openplans.org/topp" xmlns:sf="http://www.openplans.org/spearfish" xmlns:nurc="http://www.nurc.nato.int" xmlns:solWS="solWS">
tiger:poly_landmarks
</Name>
var nss:Array = xml.namespaceDeclarations();
for each(var ns:Namespace in nss)
{
if(ns.uri == "http://www.census.gov")
{
trace(ns.prefix);
break;
}
}

How do I add CDATA to an xml file?

I have an existing xml file that holds notifications I want to display on my site. A snippet follows:
<contents>
<item>
<![CDATA[
<a style="font-weight: bold;" href="http://engadget.com">Engadget</a>
]]>
</item>
<item>
<![CDATA[
<a style="font-weight: bold;" href="http://cnn.com">CNN</a>
]]>
</item>
</contents>
I'm trying to open this document and add new "items" to it, but I can't:
foreach (string s in notifications)
{
XmlElement newElement = doc.CreateElement("item");
newElement.InnerXml = "<![CDATA[ " + s + " ]]>";
doc.DocumentElement.SelectNodes("/contents")[0].AppendChild(newElement);
}
notifications is a List that I'm using to store the links. The error I'm getting is:
']]>' is not allowed in character data.
The notifications need to contain HTML, because of the way I'm displaying it. Thanks for looking, guys.
Try using
newElement.AppendChild(doc.CreateCDataSection(s));
instead of
newElement.InnerXml = "<![CDATA[ " + s + " ]]>";
Try this way:
newElement.InnerXml = "<![CDATA[ " + s + " ]]>";