Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4172380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:33:22+00:00 2026-05-21T00:33:22+00:00

I want to parse an XML file. My XML looks like this: <?xml version=1.0?>

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T00:33:23+00:00Added an answer on May 21, 2026 at 12:33 am

    You can’t change the value with the SAX handler AFAIK. Generally I define both a sax handler to convert from xml to some c++ object, as well as a document builder to convert from object to xml.

    A general approach is as follows: use the sax handler with an xml parser to get an object, modify the object, and then use the document builder to save the xml file. Have a look at the DomWriter class. There was also an example that shows you how to build an XML document with xerces.

    Maybe you could also just search for the tag you want to chain to the value, and do a string replace if you want to avoid the whole XML conversion process (dependent on your document structure, etc).

    Edit:
    There are two aspects to XML: parsing and building. When it comes to parsing, you have two options: using sax or dom. Sax parsing involves you writing a handler. The XML doc is scanned and the handler gets called as an element in your XML doc is encountered.

    E.G.
    “Saw the Foo opening tag”

    And then later
    “Saw the Foo closing tag”

    When your handler is called, you get the opportunity to act. Typically you would have some object as a member of your handler, and you would call setters with the value obtained from the xml document. Once the parsing is complete, you could then e.g. call a getter on the handler to retrieve the object that now has set values. Using sax, you cannot modify the document with the sax handler.

    A DOM handler works with the whole xml document in memory. I haven’t used a xerces DOM parser before, but I’m sure there must be an example for that. Since DOM has the whole document in memory, you might even be able to change the document on the fly without going through the sax parsing and rebuilding I outlined above. I would definitely investigate the DOM parser examples before using SAX.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi i have xml file whitch i want to parse, it looks something like
I have to parse an XML document that looks like this: <?xml version=1.0 encoding=UTF-8
My XML file looks like the following: <?xml version=1.0?> <ItemSearchResponse xmlns=http://webservices.amazon.com/AWSECommerceService/2008-08-19> <Items> <Item> <ItemAttributes>
So I have an XML file that looks a little like this <xml> <post>
I want to parse a config file sorta thing, like so: [KEY:Value] [SUBKEY:SubValue] Now
I have a xml file. That I want to parse with NXXMLParser. I have
I am having one xml file and I want to parse it to get
I'm using XML::Simple to parse an XML file which I then want to use
can i parse remote(not local) file using JS/Jquery? i want to display remote xml
In my C++ program I want to parse a small piece of XML, insert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.