更改文件上的标记值 XML Xerces C++ 解析器
Change tag value on file XML Xerces C++ Parser
我创建了一个 XML 文件,但我不知道如何更改此文件上的标签文本值。
我也可以获得节点文本值并可以更改它,但是我如何将此修改写入文件?
xercesc_3_2::XMLPlatformUtils::Initialize();
parser = new xercesc_3_2::XercesDOMParser();
parser->parse(xmlFile);
xercesc_3_2::DOMDocument* doc = parser->getDocument();
xercesc_3_2::DOMNodeList* child = doc-getElementsByTagName(xercesc_3_2::XMLString::transcode("A"));
xercesc_3_2::DOMNode* currentNode = child->item(i);
if (currentNode->getNodeType() && currentNode->getNodeType() == xercesc_3_2::DOMNode::ELEMENT_NODE) {
currentNode->setTextContent(xercesc_3_2::XMLString::transcode("3000"));
void changeXMLNodevalue(std::string fileName, std::string node, std::string value) {
xercesc_3_2::XMLPlatformUtils::Initialize();
// create the DOM parser
xercesc_3_2::XercesDOMParser* parser = new xercesc_3_2::XercesDOMParser;
parser->setValidationScheme(xercesc_3_2::XercesDOMParser::Val_Never);
parser->parse(fileName.c_str());
// get the DOM representation
xercesc_3_2::DOMDocument* doc = parser->getDocument();
// get the root element
xercesc_3_2::DOMElement* root = doc->getDocumentElement();
// evaluate the xpath
xercesc_3_2::DOMXPathResult* result = doc->evaluate(
xercesc_3_2::XMLString::transcode(node.c_str()),//"/dgml/document/head/zoom/maximum"),
root,
NULL,
xercesc_3_2::DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
if (result->getNodeValue() == NULL)
{
cout << "There is no result for the provided XPath " << endl;
}
else
{
result->getNodeValue()->getFirstChild()->setNodeValue(xercesc_3_2::XMLString::transcode(value.c_str()));// "3000"));
const XMLCh* a = result->getNodeValue()->getFirstChild()->getNodeValue();
char* tttt = (char*)a;
cout << "Node value: " << tttt << endl;
}
xercesc_3_2::XMLPlatformUtils::Terminate();
return;
}
我创建了一个 XML 文件,但我不知道如何更改此文件上的标签文本值。 我也可以获得节点文本值并可以更改它,但是我如何将此修改写入文件?
xercesc_3_2::XMLPlatformUtils::Initialize();
parser = new xercesc_3_2::XercesDOMParser();
parser->parse(xmlFile);
xercesc_3_2::DOMDocument* doc = parser->getDocument();
xercesc_3_2::DOMNodeList* child = doc-getElementsByTagName(xercesc_3_2::XMLString::transcode("A"));
xercesc_3_2::DOMNode* currentNode = child->item(i);
if (currentNode->getNodeType() && currentNode->getNodeType() == xercesc_3_2::DOMNode::ELEMENT_NODE) {
currentNode->setTextContent(xercesc_3_2::XMLString::transcode("3000"));
void changeXMLNodevalue(std::string fileName, std::string node, std::string value) {
xercesc_3_2::XMLPlatformUtils::Initialize();
// create the DOM parser
xercesc_3_2::XercesDOMParser* parser = new xercesc_3_2::XercesDOMParser;
parser->setValidationScheme(xercesc_3_2::XercesDOMParser::Val_Never);
parser->parse(fileName.c_str());
// get the DOM representation
xercesc_3_2::DOMDocument* doc = parser->getDocument();
// get the root element
xercesc_3_2::DOMElement* root = doc->getDocumentElement();
// evaluate the xpath
xercesc_3_2::DOMXPathResult* result = doc->evaluate(
xercesc_3_2::XMLString::transcode(node.c_str()),//"/dgml/document/head/zoom/maximum"),
root,
NULL,
xercesc_3_2::DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
NULL);
if (result->getNodeValue() == NULL)
{
cout << "There is no result for the provided XPath " << endl;
}
else
{
result->getNodeValue()->getFirstChild()->setNodeValue(xercesc_3_2::XMLString::transcode(value.c_str()));// "3000"));
const XMLCh* a = result->getNodeValue()->getFirstChild()->getNodeValue();
char* tttt = (char*)a;
cout << "Node value: " << tttt << endl;
}
xercesc_3_2::XMLPlatformUtils::Terminate();
return;
}