1

I am wanting to know how to add text from a variable into an xml writer.

This is what I am trying to do below

NSString *testString = [[NSString alloc] initWithString:@"TEST TEST TEST"];
    //allocate serializer
        id <XMLStreamWriter> xmlWriter = [[XMLWriter alloc] init];
        //add root element
        [xmlWriter writeStartElement:@"Rows"];
            [xmlWriter writeStartElement:@"Row"];
                [xmlWriter writeCharacters:@"request: %@ can go in here", testString]; //this line here
            [xmlWriter writeEndElement];
        //close rows element
        [xmlWriter writeEndElement];

so pretty much how you do an nslog.. but i would like to know how i can do this with xmlwriter.. or if its even possible?

otherwise i guess the other option would be to create the whole string outside of the xmlwriter? what do you think?

C.Johns
  • 10,185
  • 20
  • 102
  • 156

1 Answers1

2

Try this:

[xmlWriter writeCharacters:[NSString stringWithFormat:@"request: %@ can go in here", testString]]; //this line here
sosborn
  • 14,676
  • 2
  • 42
  • 46
  • perfect.. that worked :) I'll mark your answer once the time is up :) – C.Johns Mar 08 '12 at 00:46
  • another question.. if I wanted to add something like this to my xml packet **** How do you think I could go about doing that? if you don't know thats all good I am reading through documentation but its a little unclear with the xswi examples – C.Johns Mar 08 '12 at 00:48
  • Sorry, but I have never used XMLStreamWriter so I do not know. – sosborn Mar 08 '12 at 01:22
  • @C.Johns to write that Result tag, you need to call writeStartElement and then writeAttribute twice (for Format and Compression attributes) and then writeEndElement. Automatic end element should be enabled by default. ;-) – ThomasRS Jun 29 '12 at 11:00
  • @sosborn Can you please tell me how can I change my code so that I can write different language http://stackoverflow.com/questions/43539307/how-to-post-string-with-special-character-and-thai-language-using-xml-parsing-in – Muju Apr 22 '17 at 05:23