Parsing XML issue - ios

I have been trying to parse an XML file and all is going well except for one thing.
this is what my XML looks like:
<portfolio>
<item>
<image url="http://www.google.com" />
<title>my first title here.</title>
<desc>my first description here...</desc>
<date>15/07/2010</date>
<skills>skills 1, skills 2, skills 3</skills>
</item>
</portfolio>
I have been parsing: title, desc, date, and skills perfectly. The only issue I am having is parsing the image url. I am using this simple parser: https://github.com/robertmryan/Simple-XML-Parser
Anyway this is how I am setting up the element names to parse:
parser.elementNames = #[#"image", #"title", #"desc", #"date", #"skills"];
Anyway what do I feed into the element name for the image url based upon the XML snippet I gave above?
Thanks!
Edit:
I logged the dictionary it returns after trying the following 3 bits of code:
parser.attributeNames = #[#"image url"];
parser.attributeNames = #[#"image"];
parser.attributeNames = #[#"url"];
Each one of those (after being parsed), returns a dictionary which I logged as this:
dict keys: (
title,
skills,
desc,
date
)
So something is not working right.

The image element has a url attribute so you need to specify that you want the attribute to be parsed out too. Do this by setting the value of the attributeNames property on your parser.
This parser is really basic though so it has some limitations. Most important for you is that attributeNames is only used on the 'main' element (specified with rowElementName) so to do what you want to do you will need to edit the parser class to change that.

Related

Xquery to map the value of a specific name attribute

I am trying to create an xquery in jdeveloper . I am stuck at a small portion of it. It would be great if I get some suggestions.
Below is the part I am stuck at
The request is:
`<variables>
<variable name="StartTime" value="01:00:00"/>
<variable name= "EndTime" value="05:00:00"/>
The response I want to map is a single element with two values looks like below:
<ns2:time ns2:startTime="01:00:00" ns2:endTime="05:00:00"/>
Below is the xquery I tried. But I get only the start time at both places. I want some way by which I can correctly assign the values looking at the name value in the request.
if (fn:data($Prefereces/ns1:variables/ns1:variable/#name="StartTime")or fn:data($Prefereces/ns1:variables/ns1:variable/#name="EndTime")) then
( <ns2:time ns2:startTime="{fn:data($Prefereces/ns1:variables/ns1:variable/#value)}" ns2:endTime="{fn:data($Prefereces/ns1:variables/ns1:variable/#value)}">
</ns2:time>)
else
()
Thanks in advance.
You can use this :
<ns2:time ns2:startTime="{fn:data($Prefereces/variables/variable[#name = 'StartTime']/#value)}" ns2:endTime="{fn:data($Prefereces/variables/variable[#name = 'EndTime']/#value)}"/>
Note that the ns2 prefix has to be defined beforehand.
XQuery's predicates are specified using brackets ([condition]), which were missing from your tries.

Core Data imports attributes (from type NSString) with spaces and new line

I am importing some data using NSXMLParser from XML into Core Data.
xml looks like:
<Translation>
<LanguageCode>EN</LanguageCode>
<SurahName>Al Anfal (The Spoils of War)</SurahName>
<TranslatedText>Believers areā€¦</TranslatedText>
</Translation>
XML is ok i mean there aren't existing spaces there.
Then i want to display saved data on the App.
I concatenate different attributes into one string but it is not displayed in one line. (Integer values coming from other entity)
After debug, i realised that the attributes from type NSString added wrong into the core data. Namely they are containing spaces and line break.
I am using following code to concatenate string with integer values:
NSString *result = [NSString stringWithFormat:#"%# - :%i / %i",currentTranslation.surahName,surahNr,verseNr];
Result should be a string without line breaks, but surahName pushes following integers to the next line.
Result:
Al Anfal (The Spoils of War)
- :8 / 2
As you see this part "- :8 / 2" printed in new line which pushed by surahName.
I searched this problem but i didn't find something. I don't know what i am doing wrong.
I hope the description above was clear.
Thank you in advance
The problem should be in the way you parse your XML.
In the exemple you give, there is a new line caracter after the ending of each element.
Here is the delegate's methods called to parse your surahname line:
didStartElement (<SurahName>)
foundCharacters (Al Anfal (The Spoils of War))
didEndElement (</SurahName>)
foundCharacters (new line caracter)
In foundCaracters method you have to check if those caracters are in an opened element, and if this element is supposed to contain caracters.
If you determine caracters as usefull, add it to your current content, else don't use it.

Getting an XML value from an NSArray

I'm currently using SMXMLDocument as my parser and so far it does a fantastic job parsing some XML files. The only problem that I have encountered is that it cannot seem to handle children with the same name, well at least in my case. But this parser can return the parsed XML as an NSArray.
The NSArray would look like this:
(
"<id>https://spreadsheets.goog\U2026</id>",
"<updated>2013-12-23T17:54:04.814Z</updated>",
"<category term=\"http://schemas.google.com/spreadsheets/2006#cell\" scheme=\"http://schemas.google.com/spreadsheets/2006\"/>",
"<title type=\"text\">A1</title>",
"<content type=\"text\">What?</content>",
"<link rel=\"self\" type=\"application/atom+xml\" href=\"https://spreadsheets.google.com/feeds/cells/some key/od6/private/full/R1C1\"/>",
"<link rel=\"edit\" type=\"application/atom+xml\" href=\"https://spreadsheets.google.com/feeds/cells/some key/18o84x\"/>",
"<cell row=\"1\" col=\"1\" inputValue=\"What?\">What?</cell>",
"<id>A1</id>",
"<status code=\"200\" reason=\"Success\"/>",
"<operation type=\"update\"/>")
So my question is, how would I get the values (and attributes) from the XML? If there is a way to tokenize this (ie going through the array as an NSString with a for-in loop or something) without having to use a big fancy library that would be great. Thanks in advance.
Update:
Here is the NSLog of what happens if I try to get id with SMXMLDocument:
Code:
SMXMLElement* testEntry = [feed childNamed:#"entry"];
NSLog(#"id: %#", [testEntry valueWithPath:#"id"]);
Output:
id: https://spreadsheets.google.com/feeds/cells/some key/od6/private/full/R1C1
After hours of battling with the code, I ended up using another parser (as a secondary) called SHXMLParser because of it's neat syntax. It is capable returning multiple values from nodes with the same name as an NSArray. From there I just compared the contents in the array and picked the one I wanted.

iOS Parse XML iTunes:image tag

I am trying to parse a podcast to build a table view of its contents in my app. The picture for the podcast is in a tag listed as:
<itunes:image href="http://www.site.com/picture.jpg" />
In my app, I try to get the contents of the string using:
NSString *articleImage = [item valueForChild:#"itunes:image"];
When I run a log for this string, nothing shows up. So, how would I go about getting the URL of the jpg from this particular tag?
The url is in an attribute called href, valueForChild gets the text value of the element, which is quite correctly empty.
You want to use [item attributeForName:#"href"] where item is your itunes:image element.

How to use locale entity in js-code

is it possible to get the value of an entity
<!ENTITY gatwayError "Gateway error">
using javascript? For now I reference them in my xul file using
&gatewayError;
UPDATE: In my ff-sidebar.xul within the <page> I have
<stringbundleset id="stringbundleset">
<stringbundle id="strings"
src="chrome://myaddon/locale/de/sidebar.properties"/>
</stringbundleset>
In my ff-sidebar.js I do on click:
var strbundle = document.getElementById("strings");
var localizedString = strbundle.getString("test");
This gives me following error
Should it not be
var strbundle = document.getElementById("stringbundleset");
This gives me no error but no result too.
Basically what Neil posted there is what you need to do (minus first paragraph rant :P )
Here's an example (basically digest from Neil's links):
Your XUL file:
<stringbundleset id="strbundles">
<stringbundle id="strings" src="chrome://yourextension/locale/something.properties"/>
</stringbundleset>
Your something.properties (there you define your localized strings key=value). Of course you can have as many files as you want/need:
something=Some text for localization
something2=Some more text
Your js file:
var strbundle = document.getElementById("strings");
var localizedString = strbundle.getString("something");
Hope this helps.
This works for small numbers of entities. For instance, menuitems sometimes have two entities with slightly different text depending on what the menuitem will be used for, and the correct entity is then copied to the label. The worst abuse of this was for the Delete menuitem in Thunderbird and SeaMonkey's mail windows, which had labels for unsubscribing from newsgroups, deleting folders, cancelling news posts, deleting single or multiple messages, or undeleting single or multiple messages from folders using the IMAP mark as delete model. Phew!
If you have lots of locale data then the best thing is to put it in its own .properties file and read it using a <stringbundle>. If your script doesn't have access to a <stringbundle> element it is also possible to manually retrieve an nsIStringBundle from the nsIStringBundleService.

Resources