I am creating an NSXMLDocument, from which I get a file defined in a constant USER_PRINTXML_URL, as follows:
NSXMLDocument *ads_user_printxml = [[NSXMLDocument alloc] initWithContentsOfURL: [NSURL URLWithString:USER_PRINTXML_URL] options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA) error:&ads_err];
Then I use XPath to get to the desired location in the XML file, as follows:
NSArray* ipp_folder = [[ads_user_printxml nodesForXPath:@".//root/printing/IPP"error:&ads_err] objectAtIndex:0];
Now, if I want to add NSXMLElements after the element in the XML file, how do I do it?
I have tried to do the following:
NSXMLElement *s= [ipp_folder objectAtIndex:0];
But this is generating a run time error. I also tried using an NSXMLNode instead of putting the data in an NSArray but again to no avail. I believe the solution is quite simple, but I can't for the life of me find an answer in the docs.
Thanks for any help.
-nodesForXPath:error:
returns an NSArray*
, but your second code snippet has already applied -objectAtIndex:
to that. So, ipp_folder
doesn't hold an array, it holds a pointer to an NSXMLNode
. Fix its declared type.
Then, you can identify its parent using the -parent
method and its index within the parent's children using the -index
method. Then, assuming the parent is an NSXMLElement
, you can do:
[[ipp_folder parent] insertChild:someNewNode atIndex:[ipp_folder index] + 1];