我正在尝试解析位于字符串中的XMl文件并更新xml中的某些属性。
我同时使用xerces c ++解析器,并使用MemBufInputSource
和parse
。
cout << "Got the string object" << endl;
cout << "The XML is: " << str.str() << endl;
string registrationRequest=str.str();
XMLPlatformUtils::Initialize();
XercesDOMParser *parser = new XercesDOMParser;
parser->setValidationScheme(XercesDOMParser::Val_Never);
parser->setDoNamespaces(false);
parser->setDoSchema(false);
MemBufInputSource registrationRequest_buf((const XMLByte*)registrationRequest.c_str(),
registrationRequest.size(), "dummy", false);
parser->parse(registrationRequest_buf);
DOMDocument *doc = parser->getDocument();
DOMElement* elementRoot = doc->getDocumentElement();
char *RootName = XMLString::transcode(elementRoot->getTagName());
XMLCh *ns = XMLString::transcode("xmlns");
XMLCh *val = XMLString::transcode("Some Value that I want to update");
elementRoot->removeAttribute(ns);
elementRoot->setAttribute(ns, val);
DOMNodeList* children = elementRoot->getChildNodes();
const XMLSize_t nodeCount = children->getLength();
val = XMLString::transcode("");
for( XMLSize_t xx = 0; xx < nodeCount; ++xx )
{
DOMNode* currentNode = children->item(xx);
if( currentNode->getNodeType() && // true is not NULL
currentNode->getNodeType() == DOMNode::ELEMENT_NODE ) // is element
{
DOMElement* currentElement
= dynamic_cast< xercesc::DOMElement* >( currentNode );
currentElement->setAttribute(ns, val);
}
}
有人可以帮助我如何将更新后的xml写回到字符串中。