thebiglebowski11
thebiglebowski11

Reputation: 1461

Writing XML back to the webserver on iPhone

I am writing an iPhone application that will be used to find the line information at local bars/social spots. In the application, I parse an XML file from a web server. Here is the structure of the XML:

<?xml version="1.0" encoding="UTF-8"?>
<Bars>
    <Bar>Kam's</Bar>
    <Line>10</Line>
    <Bar>Cly's</Bar>
    <Line>10</Line>
    <Bar>Joe's</Bar>
    <Line>10</Line>
    <Bar>The Red Lion</Bar>
</Bars>

This data is then displayed in a tableview with the Bar and the Line shown. If a user selects an element in the table, a uiactionview appears and requests for the user to update the line information for that particular bar. I know how to take the users input.

The problem I am having, and I really don't know where to begin, is figuring out how to rewrite the XML and put it back in the same location on the web server. Does anyone know of a tutorial or could possibly get me started with this?

Thanks so much.

Upvotes: 1

Views: 179

Answers (1)

DougW
DougW

Reputation: 30065

So I'll answer your direct question, but I have to mention that what you've posted is not proper XML. The line numbers should be attributes, or sub-nodes of the Bar nodes. Either of the following would be considered correct:

<?xml version="1.0" encoding="UTF-8"?>
<Bars>
    <Bar line="10">Kam's</Bar>
    <Bar line="10">Cly's</Bar>
    <Bar line="10">Joe's</Bar>
    <Bar line="10">The Red Lion</Bar>
</Bars>

or

<?xml version="1.0" encoding="UTF-8"?>
<Bars>
    <Bar>
        <name>Kam's</name>
        <line>10</line>
    </Bar>
    <Bar>
        <name>Cly's</name>
        <line>10</line>
    </Bar>
    <Bar>
        <name>Joe's</name>
        <line>10</line>
    </Bar>
    <Bar>
        <name>The Red Lion</name>
        <line>10</line>
    </Bar>
</Bars>

There are even more ways to represent it, but the XML spec does not honor node ordering. A parser will generally order them first to last, but there's no guarantee, so your Line nodes may get associated with the wrong Bar.

Anyway, that said, you need a few different tools, and they are far too many details to write out in an answer here so I'll point you to the documentation.

1) To communicate with the server (either get the XML, or retrieve it) you'll probably want to use NSURLConnection. Here's Apple's tutorial on it.

2) For parsing the XML, Apple only supplies a SAX parser. It's going to be a steep learning curve for you, and it also cannot write XML, which you need to do. I'm going to direct you to this other StackOverflow post, since it goes into more detail about finding a good 3rd party XML parser.

3) If you don't know how to set up a server that can serve an XML document, and accept POSTed XML content, that's a whoooole other bag of worms. I would suggest you start with a simple "LAMP" stack, and read up on PHP.

Cheers!

Upvotes: 2

Related Questions