开发者

iPhone SDK: XML mystery, after adding child nodeforXPath returns nothing (found a hacky solution)

开发者 https://www.devze.com 2023-03-27 03:56 出处:网络
I have a big mystery here, I have a Gdataxml document property: GDataXMLDocument *doc; I\'m adding a new element to doc, interestingly, this method below looks perfect for other elements but not f

I have a big mystery here,

I have a Gdataxml document property:

GDataXMLDocument *doc;

I'm adding a new element to doc, interestingly, this method below looks perfect for other elements but not for the element I just added:

GDataXMLElement *newValueDefElement = [GDataXMLNode elementWithName:@"valuedefinition"];
[variableElement addChild:newValueDefElement];

and now when I query:

NSString *path = [NSString stringWithFormat:@"//inferenceresponse/state/variable[pageId=%d]/valuedefinition",pageID];       
NSArray *valueElement = [self.doc nodesForXPath:path error:nil];

Now array comes with zero objects! new added element NOT found! but I can see it in debug as xml string, how on earth it can not find something which I can see it is there on the log? it is a cache problem or a namespace problem or a bug in GDataXML? again..Problem is adding a new child and it is somehow not updated in the doc, but I can get the other elements under same root when use the same Xpath query standard

in NSlog I can see that the new element is added to doc.

NSData *xmlData2 = self.doc.XMLData;
NSString *s= [[[NSString alloc] initWi开发者_运维百科thBytes:[xmlData2 bytes] length:[xmlData2 length] encoding:NSUTF8StringEncoding] autorelease];
NSLog(s);

Also How can self.doc.XMLData give something different than [self.doc nodesForXPath]? so it fools me to thing my doc is ok but maybe I corrupted the doc or a wrong namespace while adding removing some elements in a previous method?

my xml starts like this:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<inferenceresponse xmlns="">
<state goalreached="false">
..
..

Update

I just found a (hacky) solution; when I convert "doc" to NSData with "doc.XMLData" and then again convert back to doc, then it works! but this should not be real solution, that's lame to do that conversion back and forth to get a correct document object. What is the problem here? I guess it can not fix the namespaces for new child.


Your problem is here:

<inferenceresponse xmlns="">

The empty namespace attribute is obviously confusing the libxml XPath evaluation. If you step through GDataXMLNode's nodesForXPath:namespaces:error:, xmlXPathEval indeed returns an empty nodes set.

If you have control over the XML generation, I've got correct XPath results removing the empty attribute.

<inferenceresponse>

If modifying the server response is too hard, you can edit GDataXMLNode.m: Find the method fixQualifiedNamesForNode:graftingToTreeNode: in GDataXMLNode implementation and replace the line

if (foundNS != NULL) {
    // we found a namespace, so fix the ns pointer and the local name

with

if (foundNS != NULL && foundNS->href != NULL && strlen((char *)foundNS->href) != 0) {
    // we found a namespace, so fix the ns pointer and the local name
0

精彩评论

暂无评论...
验证码 换一张
取 消