質問

I am moving some DOMNode from one document to another; here the code snap

        XercesDOMParser *parser = new XercesDOMParser;
        parser->setValidationScheme(XercesDOMParser::Val_Auto);
        parser->setDoNamespaces(false);
        parser->setDoSchema(false);
        parser->setValidationSchemaFullChecking(false);
        parser->setCreateEntityReferenceNodes(false);

        // getDOMNodeForXMLString will pares the string and return the root DOMNode
        DOMNode* vendorExtnDomNode =  getDOMNodeForXMLString(*veField,parser);

       DOMNodeList* childList = vendorExtnDomNode->getChildNodes();
        if(childList)
        {
            DOMNode* childNode = NULL;
            for(childNode = vendorExtnDomNode->getFirstChild();childNode != NULL;childNode = childNode->getNextSibling())
            {
                DOMElement* newChild = (DOMElement*) Doc->importNode(childNode,true);
                veDomNode->appendChild(newChild);
            }
        }
        parser->resetDocumentPool();
        delete parser

if the xml string is something like below then it append to the Doc

< my:root>
        < my:values>
                        < my:value1>10< /my:value1>
        < /my:values>
< /my:root>

But if the string is something like this

< my:root>
            < my:values>
                            < my:value1>10< /my:value1>
                            < my:enum>
                                            < my:value2>10< /my:value1>     
                            < /my:enum>
            < /my:values>
< /my:root

Then I only able to see the first level hierarchy string as below on xml dump why reset are missing

< my:root>
            < my:values>
                            < my:value1>10< /my:value1>
            < /my:values>
< /my:root>

NOTE: please ignore white spaces in the tags

役に立ちましたか?

解決

String which you are parsing contains white space characters '\n'(new line), '\t' (tab) and ' ' (space) between tags. Which are parsed as text node and appended to DOM tree. This cause problem on dumping xml. Do avoid the white space you can set parser property includeWhiteSpace to false

parser->setIncludeIgnorableWhitespace (false)

But whitespace is only "ignorable" if you're validating against a DTD or schema which says that the element containing them does not accept text as content. If you haven't validated, or if the element was declared as having mixed content, the whitespace is part of the document's content.

In your case you don't have a validation check. So what you can do is before append node as child, iterate through all the node and remove text node (DOMNode::TEXT_NODE) that has data only based on whitespace, tab and '/n'. This will solve your problem.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top