Get prefix of XML namespace from URI - actionscript-3
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;
}
}
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" )
Display placemark name in openstreetmap from an external KML file using Openlayers
I am trying to load a KML file of a particular state and display the counties with its name .I am using the following code . map = new OpenLayers.Map({ div: "map_canvas", allOverlays: true }); var osm = new OpenLayers.Layer.OSM(); var styleMap = new OpenLayers.StyleMap({'default':{ label : "${name}" }}); var sundials = new OpenLayers.Layer.Vector("KML", { projection: map.displayProjection, strategies: [new OpenLayers.Strategy.Fixed()], protocol: new OpenLayers.Protocol.HTTP({ url: "Minnesota.kml", format: new OpenLayers.Format.KML({ // extractStyles: true, extractAttributes: true // kvpAttributes: false }), styleMap: styleMap }) }); map.addLayers([osm,sundials]); map.addControl(new OpenLayers.Control.LayerSwitcher()); map.setCenter(new OpenLayers.LonLat(0, 0), 3); Now the KML file is shown properly but the county names are not shown.(resulting image is attached)! The XML file looks like <Document> <name>United States Counties</name> <Style id='Style3-polygon-1'> <LabelStyle> <scale>0.8</scale> </LabelStyle> <LineStyle> <color>ffa8d7b6</color> <width>1</width> </LineStyle> <PolyStyle> <color>ff29c773</color> </PolyStyle> <BalloonStyle> <text>$[description]</text> </BalloonStyle> </Style> <Style id='Style3-polygon-1-hover'> <LineStyle> <color>ffa8d7b6</color> <width>1</width> </LineStyle> <PolyStyle> <color>ff29c773</color> </PolyStyle> <BalloonStyle> <text>$[description]</text> </BalloonStyle> </Style> <StyleMap id='Style3-polygon-1-map'> <Pair> <key>normal</key> <styleUrl>#Style3-polygon-1</styleUrl> </Pair> <Pair> <key>highlight</key> <styleUrl>#Style3-polygon-1-hover</styleUrl> </Pair> </StyleMap> <Placemark> <name>Aitkin</name> <snippet></snippet> <description><![CDATA[<div> MN-Aitkin </div>]]></description> <styleUrl>#Style3-polygon-1-map</styleUrl> <ExtendedData> <Data name='State Abbr.'> <value>MN-Aitkin</value> </Data> <Data name='County Name'> <value>mn</value> </Data> <Data name='State-County'> <value>MN</value> </Data> <Data name='value'> <value>526.0226226</value> </Data> <Data name='GEO_ID'> <value>05000US27001</value> </Data> <Data name='GEO_ID2'> <value>27001</value> </Data> <Data name='Geographic Name'> <value>Aitkin County, Minnesota</value> </Data> <Data name='STATE num'> <value>27</value> </Data> <Data name='COUNTY num'> <value>1</value> </Data> <Data name='FIPS formula'> <value>27001.0</value> </Data> <Data name='Has error'> <value></value> </Data> </ExtendedData><Polygon><outerBoundaryIs><LinearRing><coordinates>-93.05408,46.20649 -93.05421,46.20823 -93.05419,46.20965 -93.05412,46.21283 -93.05409,46.2138 -93.05403,46.21847 -93.054,46.2261 -93.05412,46.24161 -93.05419,46.24676 -93.0541,46.26842 -93.05407,46.28187 -93.0538,46.31176 -93.05374,46.32576 -93.05378,46.32944 -93.05371,46.33036 -93.05376,46.33888 -93.05407,46.36721 -93.05412,46.3804 -93.05418,46.38731 -93.05473,46.4148 -93.05485,46.41927 -93.05489,46.42117 -93.05533,46.43976 -93.05533,46.44373 -93.05531,46.45141 -93.05525,46.46333 -93.05529,46.47313 -93.05536,46.52685 -93.05541,46.53137 -93.05551,46.5389 -93.05575,46.56708 -93.05481,46.59258 -93.06371,46.62103 -93.06388,46.62991 -93.06377,46.63054 -93.06385,46.63295 -93.06447,46.6547 -93.0648,46.67386 -93.0647,46.68015 -93.06454,46.68285 -93.06459,46.68415 -93.06442,46.68815 -93.06414,46.6919 -93.0638,46.69672 -93.0635,46.70887 -93.06286,46.7377 -93.06179,46.76189 -93.06153,46.76655 -93.06153,46.7666 -93.06113,46.78424 -93.06103,46.78869 -93.06101,46.78939 -93.06083,46.79786 -93.05986,46.8421 -93.05947,46.85365 -93.05932,46.85812 -93.05894,46.86951 -93.05884,46.87494 -93.05894,46.88162 -93.05893,46.88191 -93.05889,46.88584 -93.05875,46.8944 -93.05861,46.89914 -93.05827,46.9106 -93.05826,46.91086 -93.05798,46.92368 -93.0579,46.93014 -93.05777,46.93965 -93.05756,46.94973 -93.057,46.98125 -93.05672,46.99994 -93.05658,47.00732 -93.05629,47.0161 -93.05601,47.0244 -93.05594,47.02641 -93.056,47.02641 -93.07411,47.02637 -93.09664,47.02632 -93.17387,47.02546 -93.18728,47.02525 -93.23808,47.02615 -93.25289,47.02641 -93.27663,47.02684 -93.28055,47.02691 -93.28582,47.02701 -93.29009,47.02719 -93.29575,47.02742 -93.30887,47.02797 -93.32855,47.02827 -93.43947,47.02838 -93.56249,47.02944 -93.57709,47.02941 -93.58646,47.02954 -93.59338,47.02966 -93.59822,47.02955 -93.60745,47.0295 -93.62885,47.02934 -93.6533,47.02898 -93.66564,47.02902 -93.67942,47.02914 -93.76285,47.03025 -93.77526,47.03042 -93.77528,47.02869 -93.77492,47.00431 -93.77477,46.98089 -93.77487,46.97538 -93.77503,46.96505 -93.77511,46.95953 -93.77521,46.95727 -93.77521,46.95458 -93.77522,46.95212 -93.77521,46.94862 -93.77525,46.9432 -93.77517,46.93924 -93.77508,46.93543 -93.77502,46.93207 -93.77497,46.92857 -93.77489,46.91516 -93.77508,46.88938 -93.77516,46.88185 -93.77525,46.87495 -93.77531,46.86655 -93.77536,46.86175 -93.77552,46.85637 -93.77555,46.85117 -93.77555,46.84822 -93.77586,46.81778 -93.77601,46.80405 -93.77602,46.80289 -93.77685,46.77329 -93.77714,46.73442 -93.77803,46.67766 -93.77829,46.63291 -93.77821,46.6137 -93.77791,46.59013 -93.79094,46.58774 -93.8028,46.58513 -93.81137,46.56775 -93.81092,46.55284 -93.81076,46.53363 -93.8114,46.51584 -93.81217,46.50219 -93.81169,46.48339 -93.81117,46.45739 -93.81078,46.44177 -93.81015,46.41986 -93.81018,46.41236 -93.81019,46.40837 -93.8103,46.4021 -93.81061,46.34182 -93.81067,46.31164 -93.81042,46.25105 -93.80718,46.24811 -93.80691,46.24797 -93.79946,46.24878 -93.79628,46.2432 -93.79615,46.2432 -93.75984,46.24373 -93.72728,46.24409 -93.70808,46.24442 -93.70126,46.24453 -93.69065,46.24468 -93.68406,46.24478 -93.66084,46.24512 -93.63685,46.24555 -93.62916,46.2456 -93.54353,46.24701 -93.5268,46.24713 -93.505,46.24693 -93.49318,46.24678 -93.43065,46.24641 -93.43047,46.23706 -93.43042,46.23021 -93.43044,46.22114 -93.43011,46.20857 -93.42982,46.19637 -93.4296,46.1832 -93.42989,46.17337 -93.43016,46.16611 -93.43034,46.16178 -93.43129,46.15656 -93.43182,46.15418 -93.43183,46.15399 -93.42058,46.15427 -93.40637,46.15462 -93.38262,46.1557 -93.3268,46.15804 -93.30847,46.15886 -93.30489,46.15891 -93.28945,46.1593 -93.27752,46.15939 -93.2729,46.15937 -93.26847,46.15939 -93.26439,46.15946 -93.25833,46.15921 -93.23084,46.15876 -93.17898,46.15855 -93.15425,46.15833 -93.13983,46.15824 -93.13535,46.15822 -93.12296,46.15829 -93.11547,46.1584 -93.10791,46.15838 -93.10298,46.15835 -93.09377,46.15833 -93.08377,46.15831 -93.07403,46.15829 -93.06795,46.1583 -93.05528,46.15827 -93.05387,46.15812 -93.05387,46.15821 -93.05388,46.16661 -93.05404,46.18284 -93.05417,46.18951 -93.05419,46.19383 -93.05421,46.19823</coordinates></LinearRing></outerBoundaryIs></Polygon></Placemark> <Style id='Style3-polygon-2'> How do I display the names of the counties on the map ?
I think you need a context too: var styleMap = new OpenLayers.StyleMap({ "default": new OpenLayers.Style({ label: "${label}" }, { context: { label: function (feature) { return feature.attributes.name; } } }) });
CDATA not supporting to read data from XMLList
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>