0

I am new to swift but I have made an android app where a string array is selected from an xml file. This is a large xml file that contains a lot of string arrays and the app gets the relevant string array based on a user selection.

I am now trying to develop the same app for iOS using swift. I would like to use the same xml file but I can not see and easy way to get the correct array. For example, part of the xml looks like this

 <string-array name="OCR_Businessstudies_A_Topics">
    <item>1. Business objectives and strategic decisions</item>
    <item>2. External influences facing businesses</item>
    <item>3. Marketing and marketing strategies</item>
    <item>4. Operational strategy</item>
    <item>5. Human resources</item>
    <item>6. Accounting and financial considerations</item>
    <item>7. The global environment of business</item>
</string-array>

<string-array name="OCR_Businessstudies_AS_Topics">
    <item>1. Business objectives and strategic decisions</item>
    <item>2. External influences facing businesses</item>
    <item>3. Marketing and marketing strategies</item>
    <item>4. Operational strategy</item>
    <item>5. Human resources</item>
    <item>6. Accounting and financial considerations</item>
</string-array>

If I have the string "OCR_Businessstudies_A_Topics" how do i get the "OCR_Businessstudies_A_Topics" array from the xml file.

This is very straight forward in android and although I have used online tutorials for swift it seems like I have to parse the xml file but do not seem to be getting anywhere.

Is there a better approach than trying to parse the whole xml fie?

Thanks

Barry

1

1 Answer 1

0

You can write your own XML parser, conforming to NSXMLParser or use a library like HTMLReader:

let fileURL = NSBundle.mainBundle().URLForResource("data", withExtension: "xml")!
let xmlData = NSData(contentsOfURL: fileURL)!
let topic = "OCR_Businessstudies_A_Topics"

let document = HTMLDocument(data: xmlData, contentTypeHeader: "text/xml")
for item in document.nodesMatchingSelector("string-array[name='\(topic)'] item") {
    print(item.textContent)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Wrote my own xml parser in the end. Seems thats the only way to do it on swift. Got it working now and selecting the information I need from the xml file. Used the following tutorial to help (books.google.co.uk/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.