C++ sax2 parser problem
Вопрос
I want to parse an XML file. My XML looks like this:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>tracker</name>
<value>localhost:58303</value>
<description>The host and port that the MapReduce job tracker runs
at. If "local", then jobs are run in-process as a single map
and reduce task.
</description>
</property>
</configuration>
I use the sxx 2 parser to parse this file. I want to chain the value of an element<value>
from localhost to 192.168.0.5. I wrote some C++ code which looks like this:
#include <SAX2XMLReader.hpp>
#include <XMLReaderFactory.hpp>
#include <DefaultHandler.hpp>
#include <XMLString.hpp>"
#include <iostream>
using namespace std;
using namespace xercesc;
int main (int argc, char* args[]) {
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Error during initialization! :\n";
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return 1;
}
char* xmlFile = "/home/project/conf/mapred.xml";
SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true); // optional
DefaultHandler* defaultHandler = new DefaultHandler();
parser->setContentHandler(defaultHandler);
parser->setErrorHandler(defaultHandler);
try {
parser->parse(xmlFile);
}
catch (const XMLException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (const SAXParseException& toCatch) {
char* message = XMLString::transcode(toCatch.getMessage());
cout << "Exception message is: \n"
<< message << "\n";
XMLString::release(&message);
return -1;
}
catch (...) {
cout << "Unexpected Exception \n" ;
return -1;
}
delete parser;
delete defaultHandler;
return 0;
}
The code compiles. What I want to know is how do I change the value in the XML file? How do I go about writing a handler for this and use it in my code? Can anybody explain what I need to do to successfully change a value in the XML file?
Нет правильного решения
Не связан с StackOverflow