how read plist levels with cocos2d-x v3.2 - cocos2d-x

I have the this plist file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<key>levels</key>
<array>
<dict>
<key>x</key>
<int>80</int>
<key>y</key>
<int>266</int>
</dict>
<dict>
<key>x</key>
<int>170</int>
<key>y</key>
<int>266</int>
</dict>
</array>
</plist>
And I'm trying to read this file using cocos2dx v3.2 that way:
ValueMap data;
std::string path = FileUtils::getInstance()->fullPathForFilename("my.plist");
data = FileUtils::getInstance()->getValueMapFromFile(path);
auto arrLevels = data.at("levels").asValueVector();
for(int i = 0; i < arrLevels.capacity(); i++){
//I don't know what I have to do here to get the x value and y value of the current item.
}
Someone can help me? I'm searching in all internet and the all examples I found for this are deficient.

Use this;
ValueMap data;
std::string path = FileUtils::getInstance()->fullPathForFilename("my.plist");
data = FileUtils::getInstance()->getValueMapFromFile(path);
auto arrLevels = data.at("levels").asValueVector();
for (int i = 0; i<arrLevels.size(); i++) {
ValueMap sdata = (arrLevels[i]).asValueMap();
int x = sData["x"].asInt();
int y = sData["y"].asInt();
}

Related

I cant get my webpage to pull data from an xml file inside the same folder

I'm not well versed in javascript nor xml but i'm trying to create a webpage where i can search through products on an xml file by filtering them by an ID Code. I've tried many things but i don't get errors, just nothing happens when i click search. Any help on where i'm going wrong would be appreciated as honestly I have barely any idea what i'm doing.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vendor</title>
</head>
<body>
<input type="number" id="Code">
<button type="button" name="button" onclick="loadData()">Get Product</button>
<div id="results">
</div>
<script type="text/javascript">
async function loadData(){
var data = await fetch("product.xml")
var parsedData = await data.text()
var parser = new DOMParser();
var ProductDoc = parser.parseFromString(parsedData,"text/xml");
var results = ""
var CodeInput = parseInt(document.getElementById("Code").value)
var product = ProductDoc.getElementsByTagName("product")
for(var i = 0; i < product.length; i++){
var Category = Category[i].getElementsByTagName("Category")[0].childNodes[0].nodeValue
var Code = Code[i].getElementsByTagName("Code")[0].childNodes[0].nodeValue
var Name = Name[i].getElementsByTagName("Name")[0].childNodes[0].nodeValue
var Description = Description[i].getElementsByTagName("Description")[0].childNodes[0].nodeValue
var Quantity = Quantity[i].getElementsByTagName("Quantity")[0].childNodes[0].nodeValue
var UnitPrice = UnitPrice[i].getElementsByTagName("Category")[0].childNodes[0].nodeValue
if(isNaN(CodeInput) || CodeInput == parseInt(Code)) {
results += "<div>"
+ "Category: " + Category
+ ", Code: " + Code
+ ", Name: " + Name
+ ", Description: " + Description
+ ", Quantity: " + Quantity
+ ", UnitPrice: " + UnitPrice
+ "</div><br/>"
}
}
document.getElementById("results").innerHTML = results
}
</script>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<main xmlns:product="index.html">
<product>
<product:Category>example</product:Category>
<product:Code>111-11</product:Code>
<product:Name>example</product:Name>
<product:Description>example</product:Description>
<product:Quantity>example</product:Quantity>
<product:UnitPrice>example</product:UnitPrice>
</product>
<product>
<product:Category>example</product:Category>
<product:Code>122-22</product:Code>
<product:Name>example</product:Name>
<product:Description>example</product:Description>
<product:Quantity>example</product:Quantity>
<product:UnitPrice>example</product:UnitPrice>
</product>
</main>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="index.html" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Category" type="xs:string" />
<xs:element name="Code" type="xs:string" />
<xs:element name="Name" type="xs:string" />
<xs:element name="Description" type="xs:string" />
<xs:element name="Quantity" type="xs:string" />
<xs:element name="UnitPrice" type="xs:string" />
</xs:schema>
Since you are dealing with an xml document which contains namespaces, it safer to parse it using an xml parser with a namespace resolver. The following should get you close enough to what you are looking for:
let parser = new DOMParser(),
ProductDoc = parser.parseFromString(parsedData, "application/xml"),
evaluator = new XPathEvaluator(),
resolver = evaluator.createNSResolver(ProductDoc),
results = "",
products = ProductDoc.evaluate('//product', ProductDoc, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for (let i = 0; i < products.snapshotLength; i++) {
let tags = ProductDoc.evaluate('.//product:*', products.snapshotItem(i), resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
new_div = "<div>"
for (let i = 0; i < tags.snapshotLength; i++) {
new_div += `${tags.snapshotItem(i).localName}: ${tags.snapshotItem(i).textContent} <br/>`
}
results += `${new_div}</div><br/>`
}
document.getElementById("results").innerHTML = results

How can I parse my xml in Flex?

I have the following XML structure:
<Order>
<Mats>
<mat>
<item>
<imgsrc>img_0</imgsrc>
<cid>cid_0</cid>
</item>
</mat>
<mat>
<item>
<imgsrc>img_1</imgsrc>
<cid>cid_1</cid>
</item>
</mat>
</Mats>
</Order>
In Flex, I am trying to get the first imgsrc and first cid. This is what I have so far:
public function globals_get_default_matte():void
{
var defaults_matte_loader:XMLLoader = new XMLLoader();
defaults_matte_loader.url = Globals.defaultMatteXMLURL;
defaults_matte_loader.loadXML();
defaults_matte_loader.addEventListener("dataWritten",globals_get_default_matte_result_handler);
}
public function globals_get_default_matte_result_handler(e:Event):void
{
var _xml:XML = new XML(e.currentTarget.data);
if (_xml)
{
var mattes_list:XMLList = _xml.elements("Mats");
var matte0:XML = mattes_list[0][0][0] as XML;
trace(mattes_list);
trace(matte0);
trace(matte0.childNodes);
}
}
In the Expressions tab, matte0.childNodes gives:
errors during evaluation
This is not doing what you want:
var mattes_list:XMLList = _xml.elements("Mats");
var matte0:XML = mattes_list[0][0][0] as XML;
mattes_list lists the <Mats> node, then matte0 does nothing more than select the first (and only) <Mats> node. Meanwhile, childNodes is not a property of XML, but the legacy XMLNode object, which you aren't using.
You should familiarize yourself with e4x in AS3. It's more like a query selector syntax.
To list the <mat> nodes and retrieve the first <cid> node, you can do this:
var mattes_list:XMLList = _xml.Mats.mat;
var matte0_cid:XML = mattes_list[0].item.cid[0];

ashx Handler and google maps not showing points

I have a problem using google maps and ashx handler generating points on the map.
It's really strange cause I've been using them a lot before but there is no problem.
The handler works fine, but points are not displayed on the map.
So the file generated is :
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>Bla</name>
<description><div class="ExternalClassEA84F87DED7745D594D3D4236376E796">dfdfg</div></description>
<Point>
<coordinates>19.8106635766601,41.3390789318785</coordinates>
</Point>
</Placemark>
</Document>
</kml>
NOTE: If I upload this kml file in google sites and use that kml url it works FINE :/
The code I use to create the generic handler is:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
context.Response.ContentType = "application/vnd.google-earth.kml+xml";
context.Response.AddHeader("Content-Disposition", "attachment; filename=Pikat.kml");
context.Response.Expires = -1;
XmlTextWriter kml = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
kml.Formatting = Formatting.Indented;
kml.Indentation = 3;
kml.WriteStartDocument();
kml.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
kml.WriteStartElement("Document");
String place = String.Empty;
place = context.Request.QueryString["place"];
if (!String.IsNullOrEmpty(place))
{
#region Places Of Interests
SPListItemCollection places = this.GetPlacesOfInterest(context);
kml = GeneratePlacemarkElementForPlaceOfInterests(kml, places, context);
#endregion
}
private XmlTextWriter GeneratePlacemarkElementForPlaceOfInterests(XmlTextWriter kml, SPListItemCollection places, HttpContext context)
{
int i = 0;
if (places != null)
{
foreach (SPListItem item in places)
{
kml.WriteStartElement("Placemark");
kml.WriteAttributeString("id", String.Format("{0}", i + 1));
// kml.WriteElementString("id", String.Format("{0}", Cases.IndexOf(item) + 1));
kml.WriteElementString("name", item["Title"].ToString());
string HtmlDesc = String.Format(#"<Table width='300px'>
<tr><td><b>Titulli: </b></td><td>{0}</td></tr>
<tr><td><b>Kategoria: </b></td><td>{1}</td></tr>
<tr><td><b>Adresa:</b> </td><td>{2}</td></tr>
<tr><td><b>Pershkrimi:</b> </td><td>{3}</td></tr>
<tr><td><b>Orari:</b> </td><td>{4}</td></tr>
<tr><td><b>Me shume:</b> </td><td>{5}</td></tr>
</table><br/>",
item["Title"] == null ? "--" : item["Title"].ToString(),
item["Category"] == null ? "--" : item["Category"].ToString(),
item["Address"] == null ? "--" : item["Address"].ToString(),
item["Description"] == null ? "--" : item["Description"].ToString(),
item["HoursOfOperation"] == null ? "--" : item["HoursOfOperation"].ToString(),
item["SiteUrl"] == null ? "--" : item["SiteUrl"].ToString());
kml.WriteElementString("description", HtmlDesc);
// show the Geometry
kml.WriteStartElement("Point");
string x = item["Longitude"].ToString(); string y = item["Latitude"].ToString();
kml.WriteElementString("coordinates", String.Format("{0},{1}", x, y));
kml.WriteEndElement(); // </Point>
kml.WriteEndElement(); // </Placemark>
i++;
}
}
return kml;
Ok it was a stupid firewall problem.
Everything else was ok

Dynamic XML with flex

I'm trying to build a type of query GUI, which returns queries from a php script in xml format via an httpservice.
Each query returns different results
eg.
rep with most and leat quotes
store with highest $ value
I'm really stuck on how to display the queries and access the node names and values.
Here's an example of an xml:
<node>
<action>query</action>
<quotes name="Most Quotes">
<first>John</first>
<last>Smith</last>
<quote_num>71</quote_num>
</quotes>
<quotes name="Least Quotes">
<first>Dave</first>
<last>Cook</last>
<quote_num>6</quote_num>
</quotes>
</node>
Id like to present the data in a readable way.
Thanks
Here the example code for read your XML (it works perfectly):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" horizontalAlign="center">
<mx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
import mx.utils.ObjectUtil;
private var tempXML:XML;
public function init():void{
tempXML = myXML;
txtA.text = myXML.toString();
readXml();
}
public function readXml():void{
var str:String = "";
var myXML:XMLList = new XMLList(myXML);
for each(var node:XML in myXML){
str = str + "action:" + node["action"] + "\n";
for each(var obj2:XML in node.quotes){
str = str + " name:" + obj2.attributes().toXMLString() + "\n";
str = str + " first:" + obj2["first"] + "\n";
str = str + " first:" + obj2["last"] + "\n";
str = str + " quote_num:" + obj2["quote_num"] + "\n";
}
txtB.text = str;
}
}
]]>
</mx:Script>
<mx:XML id="myXML">
<node>
<action>query</action>
<quotes name="Most Quotes">
<first>John</first>
<last>Smith</last>
<quote_num>71</quote_num>
</quotes>
<quotes name="Least Quotes">
<first>Dave</first>
<last>Cook</last>
<quote_num>6</quote_num>
</quotes>
</node>
</mx:XML>
<mx:HBox width="100%">
<mx:TextArea id="txtA" width="400" height="400" />
<mx:TextArea id="txtB" width="400" height="400" />
</mx:HBox>
Note: This code was created in actionscript 3, but should also work in your version. Please try it and tell me if will be useful, otherwise I'll have to post a code for your version. Remember that there are many ways to do this, i posted this way because maybe is more simple.
You can try this HERE.
Please visit this link for more explanation Working with XML
Second Version
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" horizontalAlign="center">
<mx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
import mx.utils.ObjectUtil;
private var tempXML:XML;
public function init():void{
tempXML = myXML;
txtA.text = myXML.toString();
readXml();
}
public function readXml():void{
var str:String = "";
var quotes:XMLList = myXML.quotes;
str = str + "action:" + myXML["action"] + "\n";
for (var i:int = 0; i < quotes.length(); i++){
str = str + "----quote name:" + XMLList(quotes[i]).attributes().toXMLString() + "\n";
var quotes_child:XMLList = quotes[i].children();
for (var j:int = 0; j < quotes_child.length(); j++){
str = str + "--------" + XML(quotes_child[j]).name() + ":" + quotes_child[j] + "\n";
}
}
txtB.text = str;
}
]]>
</mx:Script>
<mx:XML id="myXML">
<node>
<action>query</action>
<quotes name="Most Quotes">
<first>John</first>
<last>Smith</last>
<quote_num>71</quote_num>
</quotes>
<quotes name="Least Quotes">
<first>Dave</first>
<last>Cook</last>
<quote_num>6</quote_num>
</quotes>
<quotes name="other">
<first>other_first</first>
<last>other_last</last>
<quote_num>other_num</quote_num>
<other_property>other_prop</other_property>
</quotes>
</node>
</mx:XML>
<mx:HBox width="100%">
<mx:TextArea id="txtA" width="400" height="400" />
<mx:TextArea id="txtB" width="400" height="400" />
</mx:HBox>
You can try this HERE.
Check that in this new version, I iterate over each child using a "for" statement with increment variable.
Here's a way to do it without knowing what the node names or attribute names will be
for each(var item : XML in yourXML.children()) {
trace(item.name());//this will get the name of the node
for each(var attribute : XML in item.attributes()) {
trace(attribute.name() + " = " + attribute.toXMLString()); // prints out the attribute names and values
}
}

Get a href from xml file into a flash button

I have a flash file where I'm trying to change the value of the same button depending on the image that is clicked but I can't make it work with different values (I can only make it work with a static value).
AS:
var weblinkXML:XML = new XML();
weblinkXML.ignoreWhite = true;
weblinkXML.load("xml/main.xml");
weblinkXML.onLoad = function(success) {
trace("success = "+success);
for (var i:Number = 0; i< weblinkXML.childNodes.length; i++) {
PPTBUTTON.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage_1);
function fl_ClickToGoToWebPage_1(event:MouseEvent):void
{
navigateToURL(new URLRequest("test/test.html"), "_blank");
}
}
}
my XML:
<?xml version="1.0" encoding="iso-8859-1"?>
<content>
<!-- general vars -->
<settings>
<item name="copyright"><![CDATA[<font letterspacing="0.5">© 2012 | PRIVACY POLICY</font>]]></item>
<item name="companyName"><![CDATA[<font letterspacing="-2"><b>TANITA</b></font>]]></item>
<item name="companySlogan"><![CDATA[<font letterspacing="1">PHOTO PORTFOLIO</font>]]></item>
<!--mp3Url srcUrl="music.mp3"/-->
<imagesPage>
<image imageUrl="images/tfile_splash_pic_main.jpg" />
</imagesPage>
</settings>
<!-- menu -->
<menu>
<button><![CDATA[PORTFOLIO]]></button>
<button><![CDATA[ABOUT]]></button>
<button><![CDATA[NEWS]]></button>
<button><![CDATA[CONTACTS]]></button>
</menu>
<gallery gallName="Crystal Cabin Awards 2012">
<image imageUrl="gallery/tfile_gall_small_01.jpg" imagesBig="gallery/tfile_gall_big_01.jpg" buttonName="PPTBUTTON" targ="_self" href="http://www.google.com"/>
<image imageUrl="gallery/tfile_gall_small_02.jpg" imagesBig="gallery/tfile_gall_big_02.jpg" buttonName="PPTBUTTON" targ="_self" href="http://www.youtube.com"/>
<image imageUrl="gallery/tfile_gall_small_03.jpg" imagesBig="gallery/tfile_gall_big_03.jpg" buttonName="PPTBUTTON" targ="_self" href="http://www.yahoo.com"/>
<image imageUrl="gallery/tfile_gall_small_04.jpg" imagesBig="gallery/tfile_gall_big_04.jpg"/>
<image imageUrl="gallery/tfile_gall_small_05.jpg" imagesBig="gallery/tfile_gall_big_05.jpg"/>
<image imageUrl="gallery/tfile_gall_small_06.jpg" imagesBig="gallery/tfile_gall_big_06.jpg"/>
<image imageUrl="gallery/tfile_gall_small_07.jpg" imagesBig="gallery/tfile_gall_big_07.jpg"/>
<image imageUrl="gallery/tfile_gall_small_08.jpg" imagesBig="gallery/tfile_gall_big_08.jpg"/>
</gallery>
I realize my xml is complex but it's being used through the entire flash. Maybe I could reduce it to a stand alone xml just like:
<image imageUrl="gallery/tfile_gall_small_02.jpg" imagesBig="gallery/tfile_gall_big_02.jpg" buttonName="PPTBUTTON" targ="_self" href="http://www.youtube.com"/>
my issue is that I've tried to get the href into a variable and use it instead of "test/test.html" but it never works.
I also realize I have a method inside a for but at this point I've iterated so much over this code that I'm unsure how to proceed.
Here's how i would retrieve the list of href attributes:
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("xml/main.xml");
loader.addEventListener(IOErrorEvent.IO_ERROR, function errorHandler(event:IOErrorEvent):void {
trace("Error loading XML" + event.type);
});
loader.addEventListener(Event.COMPLETE, function(event:Event):void {
trace("success = " + event);
var xml:XML = new XML(loader.data);
var xmllist:XMLList = xml.gallery.children();
for (var i:Number = 0; i < xmllist.length(); i++) {
trace(XML(xmllist[i]).attribute("href"));
}
});
loader.load(request);
I had the following output:
success = [Event type="complete" bubbles=false cancelable=false eventPhase=2]
http://www.google.com
http://www.youtube.com
http://www.yahoo.com
I hope that helps.