Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to edit an XML file in ios5.1 ?

Hi,

I have an large xml file . I need to split it into 3 small ones .For reading the XML i am using NSXMLParser . How to write xml file ? If there is need of edit some tag , how to do it ?Please guide me ....


Thanks

Amiya

iPad 2, iOS 5.1

Posted on May 20, 2013 12:30 AM

Reply
Question marked as Best reply

Posted on May 20, 2013 9:26 PM

Is there any way like to convert the xml into plist and viceversa regardless there should not be any format (node) change of the previous xml ?


Thanks

6 replies

May 20, 2013 10:08 PM in response to amiya.sahu

How large is your source XML? If it easily fits in available RAM on the device, consider using an XML DOM. I use the XML DOM in Google's GData Objective-C library because Cocoa doesn't include an XML DOM (NSXMLDocument) for iOS. If you don't need other GData APIs, you only need to include GDataXMLNode.h and .m in your project.


http://code.google.com/p/gdata-objectivec-client/


You could either: (1) Continue to use SAX (NSXMLParser) to read your source file, then create the three parts using GDataXMLDocument, or (2) Read your source file into GDataXMLDocument, then separate the nodes. The latter would be easier.


This article walks through an example of reading and writing with GDataXMLDocument.

http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdatax ml

May 29, 2013 4:02 AM in response to Llessur999

Thanks for the reply . I went through the example but not able to parse key & value attributes . Please guide me to parse the below xml using GDataXMLDocument ....



<?xml version="1.0" encoding="ISO-8859-1"?>

<root>

<presetinfo>


<modality type="CT">

<body PART="Abdomen" WW="400" WL="40"></body>

<body PART="Bone" WW="2000" WL="300"></body>

<body PART="Brain" WW="80" WL="40"></body>

<body PART="Chest" WW="350" WL="40"></body>

<body PART="Liver" WW="150" WL="90"></body>

<body PART="Lung" WW="1600" WL="-600"></body>

<body PART="Pelvis" WW="350" WL="40"></body>

<body PART="Soft" WW="350" WL="50"></body>

</modality>

<modality type="MR">

<body PART="Brain T1"WW="500"
WL="250"></body>

<body PART="Brain T2"WW="350"WL="150"></body>

<body PART="Head/Neck"WW="500"
WL="250"></body>

<body PART="Spine" WW="500" WL="250"></body>

</modality>

</presetinfo>

</root>




Thanks

May 29, 2013 9:06 PM in response to amiya.sahu

I don't know what your model is but if I assume a straighforward tree-structured model PresetInfo > Modality > Body, this code is the design pattern I usually use. If you don't actually need to load a model, but instead just extract a specific data element, look at the XPath query in PresetInfo.parseXmlElement, and how I get attribute and element values in the other parseXmlElements.


The model interface. I used arrays for the lists, if you need keyed access to list elements, you could use dictionaries. The top-level class PresetInfo contains methods to read the XML from a file.

@interface ModelBase : NSObject

- (id)initWithXmlElement:(GDataXMLElement *)element;

- (void)parseXmlElement:(GDataXMLElement *)element;

@end


@interface PresetInfo : ModelBase

@property NSMutableArray *modalities;

- (id)initWithPath:(NSString *)path;

- (id)initWithXmlString:(NSString *)xmlString;

@end


@interface Modality : ModelBase

@propertyNSString *type;

@propertyNSMutableArray *bodies;

@end


@interface Body : ModelBase

@propertyNSString *part;

@propertyNSInteger ww;

@propertyNSInteger wl;

@end


The model implementation.

@implementation ModelBase


- (id)initWithXmlElement:(GDataXMLElement *)element

{

if (self = [self init]) {

[self parseXmlElement:element];

}

returnself;

}


- (void)parseXmlElement:(GDataXMLElement *)element {

[NSExceptionraise:NSInternalInconsistencyExceptionformat:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)];
// Objective-C has no abstract methods

}


@end


@implementation PresetInfo


- (id)init

{

if (self = [super init]) {

self.modalities = [[NSMutableArray alloc] init];

}

returnself;

}


- (id)initWithPath:(NSString *)path

{

NSError *error = nil;

NSStringEncoding *usedEncoding = nil;

NSString *xmlString = [NSString stringWithContentsOfFile:path usedEncoding:usedEncoding error:&error];

if (!xmlString)

NSLog(@"Error reading XML file: %@", [error localizedDescription]);

return [self initWithXmlString:xmlString];

}


- (id)initWithXmlString:(NSString *)xmlString

{

NSError *error = nil;

GDataXMLDocument *xmlDoc = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:&error];

if (!xmlDoc)

NSLog(@"Error creating XML document: %@", [error localizedDescription]);

return [selfinitWithXmlElement:xmlDoc.rootElement];

}


- (void)parseXmlElement:(GDataXMLElement *)element

{

NSError *error = nil;

NSArray *nodes = [element nodesForXPath:@"presetinfo/modality" error:&error];

if (!nodes) {

NSLog(@"Response not found: %@", [error localizedDescription]);

return;

}

for (GDataXMLElement *element in nodes)

[self.modalities addObject:[[Modality alloc] initWithXmlElement:element]];

}


@end


@implementation Modality


- (id)init

{

if (self = [super init]) {

self.bodies = [[NSMutableArray alloc] init];

}

returnself;

}


- (void)parseXmlElement:(GDataXMLElement *)element {

GDataXMLNode *node = [element attributeForName:@"type"];

if (node) self.type = node.stringValue;

NSArray *nodes = [element elementsForName:@"body"];

for (GDataXMLElement *element in nodes)

[self.bodies addObject:[[Body alloc] initWithXmlElement:element]];

}


@end


@implementation Body


- (void)parseXmlElement:(GDataXMLElement *)element {

GDataXMLNode *node = [element attributeForName:@"PART"];

if (node) self.part = node.stringValue;

node = [element attributeForName:@"WW"];

if (node) self.ww = [node.stringValue integerValue];

node = [element attributeForName:@"WL"];

if (node) self.wl = [node.stringValue integerValue];

}


@end


Example of loading your XML into the model and showing the data. This assumes the containing class (for example a view controller) has a property PresetInfo *presetInfo and the XML you posted is in a file presetinfo.xml.

// load the XML into the model

NSString *path = [[NSBundlemainBundle] pathForResource:@"presetinfo"ofType:@"xml"];

self.presetInfo = [[PresetInfoalloc] initWithPath:path];


// verify the model loaded correctly

for (Modality *modality inself.presetInfo.modalities) {

for (Body *body in modality.bodies) {

NSLog(@"Modality Type = %@; Body Part = %@; WW = %d; WL = %d", modality.type, body.part, body.ww, body.wl);

}

}

How to edit an XML file in ios5.1 ?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.