Converting this XMLList to XML - actionscript-3

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" )

Related

using cheeriojs to output a specific tag as-is

I am using cheeriojs to parse some xml
const $ = cheerio.load(xml, {
normalizeWhitespace: true,
xmlMode: true
})
This is probably something very basic that eludes me, but I can't figure out how to output the selected element verbatim. For example, I have
<foo id="3B3D3CAD1268FFDFD3E36F2DFD30DBD0" a="3392779302" b="Chiapas" c="Mexico">
<emphasis bold="true" pageId="3" pageNumber="120">Type locality.</emphasis>
</foo>
<emphasis bold="true" pageId="3" pageNumber="120">Other localities.</emphasis>
<foo id="3B3D3CAD1268FFDFD3E36F2D68FFDFD" a="3392779378" b="Baja California" c="Mexico">
<emphasis bold="true" pageId="3" pageNumber="120">Type locality.</emphasis>
</foo>
and I have selected the above like so
const elements = $('foo')
for (let i = 0; i < elements.length; i++) {
const e = elements[i]
console.log($(e).html())
}
I get
<emphasis bold="true" pageId="3" pageNumber="120">Type locality.</emphasis>
<emphasis bold="true" pageId="3" pageNumber="120">Type locality.</emphasis>
But I want
<foo id="3B3D3CAD1268FFDFD3E36F2DFD30DBD0" a="3392779302" b="Chiapas" c="Mexico"></foo>
<foo id="3B3D3CAD1268FFDFD3E36F2D68FFDFD7" a="3392779378" b="Baja California" c="Mexico"></foo>
console.log($(e).prop('outerHTML')) does the trick

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

How to append multiple child root nodes in an XML file while fetching data from MySQL Database

Am trying to retrieve objects from MySQL database and store it in an XML file. How ever the below code is not writing the XML data in the format i needed.
private static Document buildCustomerXML(ResultSet EmployeeRS) throws Exception
{
Document xmlDoc = new DocumentImpl();
/* Creating the root element */
Element rootElement = xmlDoc.createElement("Schedule");
xmlDoc.appendChild(rootElement);
while(EmployeeRS.next())
{
Element employee1 = xmlDoc.createElement("Employees");
Element employee = xmlDoc.createElement("Employee");
/* Build the CustomerId as a Attribute*/
employee.setAttribute("id", EmployeeRS.getString("id"));
/* Creating elements within customer DOM*/
Element contractid = xmlDoc.createElement("ContractID");
Element lastName = xmlDoc.createElement("Name");
Element skills = xmlDoc.createElement("Skills");
Element skill = xmlDoc.createElement("Skill");
/* Populating Customer DOM with Data*/
contractid.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Contractid")));
lastName.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("last")));
skill.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Skill")));
/* Adding the firstname and lastname elements to the Customer Element*/
employee.appendChild(contractid);
employee.appendChild(lastName);
employee.appendChild(skills);
skills.appendChild(skill);
employee1.appendChild(employee);
rootElement.appendChild(employee1);
/* Appending Customer to the Root Class*/
/* Build the CustomerId as a Attribute*/
Element constraints1 = xmlDoc.createElement("Constraints");
Element constraints = xmlDoc.createElement("Constraint");
constraints.setAttribute("id", EmployeeRS.getString("id"));
Element startdat = xmlDoc.createElement("startdate");
startdat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("startdate")));
constraints.appendChild(startdat);
constraints1.appendChild(constraints);
rootElement.appendChild(constraints1);
}
return xmlDoc;
}
Below is the output im getting for the above code
<?xml version="1.0" encoding="UTF-8"?>
<Schedule>
<Employees>
<Employee id="11">
<ContractID>1</ContractID>
<Name>kumbhar</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="11">
<startdate>11/08/2014</startdate>
</Constraint>
</Constraints>
<Employees>
<Employee id="14">
<ContractID>1</ContractID>
<Name>Raje</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="14">
<startdate>2014-11-12</startdate>
</Constraint>
</Constraints>
</Schedule>
However i need output to be in the below form
<?xml version="1.0" encoding="UTF-8"?>
<Schedule>
<Employees>
<Employee id="11">
<ContractID>1</ContractID>
<Name>kumbhar</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
<Employee id="14">
<ContractID>1</ContractID>
<Name>Raje</Name>
<Skills>
<Skill>Employee</Skill>
</Skills>
</Employee>
</Employees>
<Constraints>
<Constraint id="14">
<startdate>2014-11-12</startdate>
</Constraint>
<Constraint id="11">
<startdate>11/08/2014</startdate>
</Constraint>
</Constraints>
</Schedule>
Can any one provide me suggestions on how to achieve output in the above format
I solved it
while(EmployeeRS.next())
{
Element employee = xmlDoc.createElement("Employee");
/* Build the CustomerId as a Attribute*/
employee.setAttribute("id", EmployeeRS.getString("id"));
/* Creating elements within customer DOM*/
Element contractid = xmlDoc.createElement("ContractID");
Element lastName = xmlDoc.createElement("Name");
Element skills = xmlDoc.createElement("Skills");
Element skill = xmlDoc.createElement("Skill");
/* Populating Customer DOM with Data*/
contractid.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Contractid")));
lastName.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("last")));
skill.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Skill")));
/* Adding the firstname and lastname elements to the Customer Element*/
employee.appendChild(contractid);
employee.appendChild(lastName);
employee.appendChild(skills);
skills.appendChild(skill);
employee1.appendChild(employee);
/* Appending Customer to the Root Class*/
/* Build the CustomerId as a Attribute*/
Element constraints = xmlDoc.createElement("Constraint");
constraints.setAttribute("id", EmployeeRS.getString("id"));
Element startdat = xmlDoc.createElement("startdate");
Element enddat = xmlDoc.createElement("enddate");
startdat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("startdate")));
enddat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("enddate")));
constraints.appendChild(startdat);
constraints.appendChild(enddat);
constraints1.appendChild(constraints);
} rootElement.appendChild(employee1);
rootElement.appendChild(constraints1);
return xmlDoc;
}

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;
}
}

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>