Create XML file in Xcode 8 Swift 3 - ios

I need to create code in Swift for iOS device, to create and save a local XML file. It's actually a KML file with waypoints for exporting to Google earth.
I found the API reference here: API Reference - Class - XMLDocument
But it lacks an example of how to use these functions. Other examples I find online are all in objective C, so I'm lost how to write the code.
If someone can show simple example of how to create the file and add some keys, childs etc, that would be awesome.
An example of what this KML output file will look like:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>Waypoint Name.kml</name>
<StyleMap id="msn_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#sn_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#sh_ylw-pushpin</styleUrl>
</Pair>
</StyleMap>
<Style id="sn_ylw-pushpin">
<IconStyle>
<scale>1.1</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<ListStyle>
</ListStyle>
</Style>
<Style id="sh_ylw-pushpin">
<IconStyle>
<scale>1.3</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
<ListStyle>
</ListStyle>
</Style>
<Placemark>
<name>Waypoint Name</name>
<description>Description</description>
<LookAt>
<longitude>-114.4379986989632</longitude>
<latitude>62.47084160066127</latitude>
<altitude>0</altitude>
<heading>9.194188445440662e-10</heading>
<tilt>0</tilt>
<range>1000.000042077875</range>
<gx:altitudeMode>relativeToSeaFloor</gx:altitudeMode>
</LookAt>
<styleUrl>#msn_ylw-pushpin</styleUrl>
<Point>
<gx:drawOrder>1</gx:drawOrder>
<coordinates>-114.4435573708376,62.46869255035281,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>

Related

the display result of tiled map is wrong (using cocos2d-x 3.6)

this is what I created in my TiledMap Editor:
But when I try to display it in simulator, it becomes this:
You can see some of tile images are missed, some are wrong(the place where dirt.png should be become wall.png instead). I don't know why this happened. I follow the tutorial and add the following code in the HelloWorld project.
// create a TMX map
auto map = TMXTiledMap::create("tile/test.tmx");
addChild(map);
the content of test.tmx:
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="36" tileheight="36" nextobjectid="5">
<tileset firstgid="1" name="test" tilewidth="36" tileheight="36">
<tile id="0">
<image width="36" height="36" source="dirt.png"/>
</tile>
<tile id="1">
<image width="36" height="36" source="floor.png"/>
</tile>
<tile id="2">
<image width="36" height="36" source="wall.png"/>
</tile>
</tileset>
<layer name="Tile Layer 1" width="10" height="10">
<data encoding="base64" compression="gzip">
H4sIAAAAAAAAA2NkYGBgRMMwgM7GhRnQaEJmMaCJ4zILXS8+dbjsxOY+XO4g5FZ0M0GAiQBGV49PDwgAAKCoyOyQAQAA
</data>
</layer>
<objectgroup name="Object Layer 1">
<object id="1" name="SpawnPoint" x="125" y="133" width="67" height="62"/>
</objectgroup>
</map>
Could any one give me a hand?
okay... I have found why....
There are limitations which cocos2d-x support Tied Map Editor.
in this page, http://www.cocos2d-x.org/wiki/TileMap, it says:
Tiles:
- Embedded tiles are NOT supported (i.e., tilesets with embedded images).
- Only embedded tilesets are supported (i.e., the tileset is embedded, but not its images).
- supports at most 1 tileset per layer.
So.... each of tilesets MUST only contain one image.
And... each of layer MUST only contain one tilesets.
If you don't follow the rule while editing map in the Tied Map Editor. The result will become chaos like mine.

Parsing XML documents iOS

I have to parse this document and I plan on using NSXMLparser but the only problem is I do not know how to get the information I want. I don't know how to navigate through each tag, I have seen simple examples but when I try to use their method I get random tags and information I don't want. Can someone explain how to get certain information from a complex XML structure such as this one?
Thanks!
<kml xmlns:ns3="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:ns2="http://www.w3.org/2005/Atom"
xmlns="http://www.opengis.net/kml/2.2">
<Document>
<description>Available CAR2GO Vehicles</description>
<Style id="car2go">
<IconStyle>
<Icon>
<href>http://www.car2go.com/common/images/openapi/marker_car2go.png
</href>
</Icon>
<hotSpot y="0.0" x="0.0" />
</IconStyle>
</Style>
<Style id="car2go_ed">
<IconStyle>
<Icon>
<href>http://www.car2go.com/common/images/openapi/marker_car2go_ed.png
</href>
</Icon>
<hotSpot y="0.0" x="0.0" />
</IconStyle>
</Style>
<Placemark>
<name>UL-C5887</name>
<description>Sudetenweg, 89075 Ulm<br/>Fuel
24<br/>Interior GOOD<br/>Exterior GOOD
</description>
<styleUrl>#car2go_ed</styleUrl>
<ExtendedData>
<Data name="fuel">
<value>24</value>
</Data>
<Data name="interior">
<value>GOOD</value>
</Data>
<Data name="exterior">
<value>GOOD</value>
</Data>
<Data name="vin">
<value>WME4513001K154655</value>
</Data>
<Data name="engineType">
<value>ED</value>
</Data>
<Data name="charging">
<value>true</value>
</Data>
</ExtendedData>
<Point>
<coordinates>10.029,48.4362,0</coordinates>
</Point>
</Placemark>
<Placemark>
<name>UL-C5889</name>
<description>Heilmeyersteige, 89075 Ulm<br/>Fuel
58<br/>Interior GOOD<br/>Exterior GOOD
</description>
<styleUrl>#car2go</styleUrl>
<ExtendedData>
<Data name="fuel">
<value>58</value>
</Data>
<Data name="interior">
<value>GOOD</value>
</Data>
<Data name="exterior">
<value>GOOD</value>
</Data>
<Data name="vin">
<value>WME4513001K154655</value>
</Data>
<Data name="engineType">
<value>CE</value>
</Data>
</ExtendedData>
<Point>
<coordinates>9.947235,48.410497,0</coordinates>
</Point>
</Placemark>
</Document>
I get random tags and information I don't want
With NSXMLParser you don't navigate through the XML. Instead, you create a delegate for the parser that gets messages each time the parser encounters something -- a tag, a value, etc. Your delegate needs to watch for the tags and values that it's interested in and do whatever makes sense for your app when those arrive. So, the tags you're seeing aren't random, they're just the tags from the file you're parsing in the order in which they appear in the file.
Can someone explain how to get certain information from a complex XML structure such as this one?
Generally, your parser delegate will be a little like a state machine that keeps track of which tag it's parsing, what data it sees, etc. That may be more than you're up for, in which case you might want to look into other XML parsing options than NSXMLParser. TouchXML is one library that's popular because it parses the file for you and then lets you look through the data with Xquery.
I would highly suggest you do not reinvent the wheel and use a high level 3rd party library which will make your life a ton easier. #mattt of AFNetworking fame amongst others has an excellent XML parsing library called Ono.
I suggest you examine the strategy demonstrated by Apple's own example code here:
https://developer.apple.com/library/ios/samplecode/SeismicXML/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007323
The actual parsing code is here:
https://developer.apple.com/library/ios/samplecode/SeismicXML/Listings/SeismicXML_APLParseOperation_m.html#//apple_ref/doc/uid/DTS40007323-SeismicXML_APLParseOperation_m-DontLinkElementID_11
All the work is done by the parser's delegate (which is this object, self). Notice in particular how didStartElement:... makes choices depending on what kind of element has just been encountered.
I should also mention that there is another strategy, where instead of the delegate making these choices, you keep repointing the parser at a different delegate. Here, I develop a general version of that strategy and show how to adapt it to a particular parsing situation: http://www.apeth.com/iOSBook/ch36.html#_xml

Import custom Atom feed in PowerPivot

Question: I have created a sample xml document containing data conforming to the atom 1.0 schema. When I import the contents of this file (for testing purposes) in PowerPivot, it creates columns for each atom element in each entry, instead of creating a column per content element. Why is this?
Background: A customer wants to import data from a web service which provides a feed that uses a custom XML schema that is not supported by PowerPivot. The service provides the ability for the caller to supply an XSLT template that will be applied to the feed. I am hoping to be able to transform this feed into a valid atom feed thereby allowing the customer to import data into PowerPivot.
Sample atom xml:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<title type="text">My Data Feed</title>
<id>http://temp/feed</id>
<updated>2012-12-13T00:00:00Z</updated>
<entry>
<id>http://temp/feed/1</id>
<title type="text">Title</title>
<author>
<name>Author</name>
</author>
<updated>2012-12-13T00:00:00Z</updated>
<content type="application/xml">
<d:Name>John Smith</d:Name>
<d:Address>Address</d:Address>
<d:Zip>1234</d:Zip>
</content>
</entry>
</feed>
When imported into PowerPivot (selecting "From Data Feeds", clicking "Browse" and pointing out the xml file), it looks like this:
I was excpecting three columns: Name, Address and Zip. If I change "Include Atom Elements" from Auto to False in the connection configuration, no columns are imported.
It seems I was just missing the m:properties element. Final result - also includes examples of null attributes and data types:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns="http://www.w3.org/2005/Atom"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<title type="text">My Data Feed</title>
<id>http://temp/feed</id>
<updated>2012-12-13T00:00:00Z</updated>
<entry>
<id>http://temp/feed/1</id>
<title type="text">Title</title>
<author>
<name>Author</name>
</author>
<updated>2012-12-13T00:00:00Z</updated>
<content type="application/xml">
<!-- attributes placed under the properties element -->
<m:properties>
<d:Name>John Smith</d:Name>
<d:Address>Address</d:Address>
<d:Zip m:type="Edm.Int32">1234</d:Zip>
<d:Comment m:null="true" />
</m:properties>
</content>
</entry>
</feed>

How to convert my xml file to kml file

In my iOS application,I have one xml file with thousands of locations data. I need to convert it to kml format to show heat maps on my map view. How can I convert it? Please give me guidelines for this conversion...
<?xml version="1.0"?>
<row id="1">
<id>101</id>
<name>Sharon Appartments</name>
<longitude>115.23412</longitude>
<latitude>34.734121</latitude>
<city>Dallas</city>
<state>California</state>
<country>USA</country>
</row>
<row id="2">
.
.
.
.
<row id="20000">
.
.
</row>
</xml>
Since you asked for guidelines, an example kml document looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>New York City</name>
<description>New York City</description>
<Point>
<coordinates>-74.006393,40.714172,0</coordinates>
</Point>
</Placemark>
</Document>
</kml>
It seems you can keep the <name> field the same, you can combine the items in xml fields <city,<state>,<country> to klm field <address>city,state,country</address> or use it as a kml <description> tag (User-supplied content that appears in the description balloon ) as kml will use long and lat to determine a point if it is provided instead of using an address. Transform the xml <longitute>,<latitudite> need to be turned into a kml <point><cordinates>longitute,latatude</cordinates></point>
So you need to transform your klm document into something like this:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>Sharon Appartments</name>
<description>Dallas, California, USA</description>
<Point>
<coordinates>115.23412,34.734121</coordinates>
</Point>
</Placemark>
<Placement>
Put item 2 details here
</Placement>
.
.
.
<Placement>
Put item 20000 details here
</Placement>
</Document>
</kml>
I got this from the https://developers.google.com/kml/documentation/kmlreference ,
Now for the heat map part, if you can transform your file into a list of lat and longs, you can put it into the python program on this webpage: http://jjguy.com/heatmap/ and it will produce a kml overlay for google maps based on that data, you need to combine that overlay with your kml file for your city data to get a complete map.

How to add one more action to unknownContentType dialog?

In case user downloads particular type of file (let's say .doc), I should show one more item (i.e. possible action) in addition to Save and Open at unknownContentType Firefox dialog.
How can I do it?
Upd. Looking at FlashGot addon sources, I've extracted the following:
chrome.manifest
overlay chrome://mozapps/content/downloads/unknownContentType.xul chrome://flashgot/content/DMOverlayFx.xul
overlay chrome://global/content/nsHelperAppDlg.xul chrome://flashgot/content/DMOverlayMoz.xul
Why do they have two overlays? What second does? And, how to understand that user chosen my option?
DMOverlayFx.xul
<?xml version="1.0" encoding="UTF-8"?>
<overlay id="DMOverlayFF"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript" src="Common.js"/>
<script type="application/x-javascript" src="DMOverlay.js"/>
<radiogroup id="mode" >
<vbox insertbefore="save" id="synoextcontainer" flex="1">
<hbox flex="1">
<radio id="synoext-dmradio" label="Download with Synology NAS" />
</hbox>
</vbox>
</radiogroup>
</overlay>
DMOverlayMoz.xul
<?xml version="1.0" encoding="UTF-8"?>
<overlay id="DMOverlayMoz"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/x-javascript" src="Common.js"/>
<script type="application/x-javascript" src="DMOverlay.js"/>
<radiogroup id="mode" >
<hbox position="3" >
<radio id="synoext-dmradio" label="Download with Synology NAS"/>
</hbox>
</radiogroup>
</overlay>
The important file to overlay is unknownContentType.xul. You can see an example here: http://code.google.com/p/firedownload/source/browse/chrome/content/unknownContentType-overlay.xul. This adds a new checkbox to the dialog, but you can use the same approach to add a new radio button. You'll want to include a new JS file in the overlay (like unknownContentType-overlay.js in this example) where you can override the standard processing of the radio buttons. Process your new option and hand off the other options to the existing implementation (look at helperApps.js to see how the standard processing works).

Resources