Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I now have a hack where I perform the following:</p> <ul> <li>Read the XML as a string.</li> <li>Parse using a regular expression to find all processing instructions.</li> <li>Add all the processing instructions into a string array.</li> <li>Use the regular expression to remove the processing instructions</li> </ul> <p>This then allows me to execute the XPath queries without issue. However if I then need to write the XML back I need to:</p> <ul> <li>Create a new mutable string to hold the output XML document/</li> <li>Loop through the processing instructions and append them to the new output XML document string.</li> <li>Get the string from my real XML document and append to the new output XML string.</li> </ul> <p>However this is far from optimal and I don't understand why the processing instructions break the XPath queries in KissXML. Here is an example of my code:</p> <pre><code> // Load the XML file from the application bundle NSString *xmlFilename = @"demo1-5.xml"; NSLog(@"**** About to parse %@ ****\n\n", xmlFilename); NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:xmlFilename]; NSString *xmlStringData = [NSString stringWithContentsOfFile:XMLPath encoding:NSUTF8StringEncoding error:nil]; // Now that we have the XML as a string, use regex to: // 1) Find all processing instructions NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&lt;\\?.*?\\?&gt;" options:NSRegularExpressionCaseInsensitive error:nil]; processingInstructions = [[NSMutableArray alloc] init]; [regex enumerateMatchesInString:xmlStringData options:0 range:NSMakeRange(0, [xmlStringData length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){ // 2) Put all the matches in an array NSString *toStore = [xmlStringData substringWithRange:[match range]]; [processingInstructions addObject:toStore]; }]; // 3) Remove all processing instructions from the XML string NSString *newString = [regex stringByReplacingMatchesInString:xmlStringData options:0 range:NSMakeRange(0, xmlStringData.length) withTemplate:@""]; // Having removed the processing instructions, we can now parse the XML NSError *err=nil; NSURL *furl = [NSURL fileURLWithPath:XMLPath]; if (!furl) { NSLog(@"Can't create an URL from file: %@.", XMLPath); return; } xmlViewDoc = [[DDXMLDocument alloc] initWithXMLString:newString options:0 error:&amp;err]; // Now execute an XPath query using the namespace NSString *xPathQuery = @"/tst:TstForm/tst:TstDetails"; NSArray *nodes = [xmlViewDoc.rootElement nodesForXPath:xPathQuery error:nil]; for (DDXMLElement *node in nodes) { NSLog(@"Title Value: %@", node.stringValue); } // Finally we'll generate a new XML string which is a concatenation of // the processing instructions and the NSXMLDocument. NSMutableString *xmlOutputString = [[NSMutableString alloc] init]; for (NSString *piString in processingInstructions) { [xmlOutputString appendFormat:@"%@\n", piString]; } [xmlOutputString appendString:[xmlViewDoc XMLStringWithOptions:DDXMLNodePrettyPrint]]; NSLog(@"XML:\n%@", xmlOutputString); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload