Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML in UNIX

Status
Not open for further replies.

DilipS

Programmer
Sep 3, 2002
5
0
0
SG
Hi All

I want to know how can we use XML in UNIX .

Regards
Dilip
 
You need to be more specific.

What do you want to use XML for ?

I have seen it used to store metadata for satellite imagery in a data warehouse project.

I think the most recent version of MasterCook stores its recipes in XML

Details, please. "Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us." Calvin (Bill Watterson)
 
I wuld like to know simple usage like reading or parsing a XML file in
2) c++ under UNIX
1) Shell Scripts ( If its possbile)

In VC++ I have done this using MSXML parser and DOM APIs.

Regards

 
A simple usage am looking for.
Specifically I want to use an XML as ini file and after parsing it, want to set the environment variables in UNIX.
 
Sorry the following is all very muddled but should get you started.

We use the "xerces" freeware XML parser from apache We are using version 5 which is quite old now as they are on at least version 7 but 5 works just fine for us.

It is not a simple piece of software to use - it takes time and patience to get it working. Its not as simple to use as the MS DOM parser in VB according to my colleagues who use VB. The following is not a program or example but some code snippits to get you started with some of the things you can look up in the documentation. We actually wrapped the DOM parser to make it a bit easier to use. I can't give you the code as it is copyright to our company but I can give you some hints:

// Strangely these sax exceptions are needed if you want to instantiate an error handler
// for the DOM parser....
#include <sax/SAXException.hpp>
#include <sax/SAXParseException.hpp>

....

mParser = new DOMParser; // Have to new/delete as must call Terminate AFTER deletion

// setValidationScheme
// Indicates what validation scheme to use. It defaults to 'auto', but
// can be set via the -v= command.
// setDoNamespaces
// Indicates whether namespace processing should be done.
// setCreateEntityReferenceNodes
// Indicates whether entity reference nodes needs to be created or not
// Defaults to false
//
// mParser->setValidationScheme(DOMParser::Val_Always);
mParser->setValidationScheme(DOMParser::Val_Auto);
mParser->setDoNamespaces(false);
mParser->setCreateEntityReferenceNodes(false);
mParser->setToCreateXMLDeclTypeNode(true);
mParser->setIncludeIgnorableWhitespace(false);
mParser->setErrorHandler(&mErrorHandler);

...

#include <framework/MemBufInputSource.hpp>
void mlXmlDOMParser::parse(const string &arXMLString)
{
parse(arXMLString.c_str());
}
void mlXmlDOMParser::parse(const char *apXMLString)
{
// Parse the XML string passed in
try
{
// This is a xerces type
MemBufInputSource inXML((const XMLByte*)apXMLString,
strlen(apXMLString), &quot;mlXmlDOMParser&quot;);
mParser->parse(inXML);

}

catch (const XMLException& e)
{
string lMsg = &quot;mlXmlDOMParser::parse : An XML error occured during XML parsing.&quot;;
lMsg += &quot; Error was in file &quot;; lMsg += e.getSrcFile();
lMsg += &quot; line &quot;; lMsg += e.getSrcLine();
lMsg += &quot; error code &quot;; lMsg += e.getCode();
lMsg += &quot; message &quot;; lMsg += *e.getMessage();
output lMsg somehow....
}
catch (const DOM_DOMException& e)
{
string lMsg = &quot;mlXmlDOMParser::parse : A DOM error occured during XML parsing with code &quot;;
lMsg += e.code;
output lMsg somehow
}
catch (...)
{
string lMsg = &quot;mlXmlDOMParser::parse : An unknown error occured during XML parsing&quot;;
output lMsg somehow
}

// Now check that the parse was ok
if (mErrorHandler.getNumFatals() > 0)
{
throw error somehow... (mErrorHandler.getNumFatals() << &quot; fatal errors encountered during parse of XML.&quot;
<< &quot; Error list is as follows:\n&quot; << mErrorHandler.getErrors());
}

// Check for any other warnings etc...
else if (mErrorHandler.getErrorCount() > 0)
{
log a warning somehow (&quot;Errors from parse are \n&quot; << mErrorHandler.getErrors());
}


// Check that the parse succeeded
if (GetTopNode().isNull())
{
throw error somehow ... (&quot;Failed to generate document&quot;);
}
else if (GetTopNode().getFirstChild().isNull())
{
throw error somehow ... (&quot;Document is blank&quot;);
}
}

...

then you can access the information using the DOM_Node type. Note that the first node is the document and you then go down into its children to get to the actual data.

Here are some other useful things that will save you headaches:

// This class is for internal use only by the DOM parser.
class mlXmlPlatformUtils
{
public:
mlXmlPlatformUtils();
~mlXmlPlatformUtils();
static bool Exists() { return msUsageCount > 0; }
private:
static int msUsageCount;
};


// *********************************************************************************
//
// PLATFORM UTILS CLASS WHICH ENSURES THAT THE PLATFORM SPECIFIC INITIALISATION FOR
// DOM HAS BEEN PERFORMED
//
// *********************************************************************************

// *********************************************************************************
//
// VITAL - This dummy instance is to ensure that XMLPlatformUtils are initialised
// before any DOM parsers and also it is Terminated after and DOM Parsers.
// ALSO ensures Terminate is only called once at the end of the program
//
// Later versions of xerces allow Initialise and Terminate to be called multiple
// times during the execution of a program. However even if this is allowed, it
// is wasteful so this dummy instance ensures it only ever gets called once.
//
// *********************************************************************************
static mlXmlPlatformUtils dummy;

// static usage count is used to determine when to really destroy and create the instance.
int mlXmlPlatformUtils::msUsageCount = 0;

//=============================================================================
//
// Method : mlXmlPlatformUtils::mlXmlPlatformUtils
//
// Description : Utility class to ensure only one instance of platform utils is created
//
//=============================================================================
mlXmlPlatformUtils::mlXmlPlatformUtils()
{
msUsageCount++;
if (msUsageCount == 1)
{
SM_LOG_MESSAGE(LOG_VERBOSE, &quot;Creating mlXmlPlatformUtils. Count = &quot; << msUsageCount);
XMLPlatformUtils::Initialize();
}
else if (msUsageCount <= 0)
{
throw error somehow (&quot;Invalid usage count error in mlXmlPlatformUtils. Count = &quot; << msUsageCount)
}
else
{
log a message (&quot;NOT Creating mlXmlPlatformUtils. Count = &quot; << msUsageCount);
}
}

//=============================================================================
//
// Method : mlXmlPlatformUtils::~mlXmlPlatformUtils
//
// Description : Utility class to ensure only one instance of platform utils is created
//
//=============================================================================
mlXmlPlatformUtils::~mlXmlPlatformUtils()
{
msUsageCount--;
if (msUsageCount == 0)
{
log the message (&quot;Destroying mlXmlPlatformUtils. Count = &quot; << msUsageCount);
XMLPlatformUtils::Terminate();
}
else
{
log the message (&quot;NOT Destroying mlXmlPlatformUtils. Count = &quot; << msUsageCount);
}
}

//=============================================================================
//
// Method : mlDOMErrorHandler::mlDOMErrorHandler
//
// Description : DOM error handler class to record errors during parsing
//
//=============================================================================
mlDOMErrorHandler::mlDOMErrorHandler() :

mNumWarnings(0), mNumErrors(0), mNumFatals(0)
{
}

//=============================================================================
//
// Method : mlDOMErrorHandler::~mlDOMErrorHandler
//
// Description : DOM Error handler class
//
//=============================================================================
mlDOMErrorHandler::~mlDOMErrorHandler()
{
}

//=============================================================================
//
// Method : mlDOMErrorHandler::CommonErrorHandler
//
// Description : Log the error in the internal buffer
//
//=============================================================================
void mlDOMErrorHandler::CommonErrorHandler(const SAXParseException& e, char *msg)
{
string lError(msg);
lError += &quot; in File: &quot;;
lError += mlXmlDOMTranscode(e.getSystemId()).asInternalCharPtr();
lError += &quot; line: &quot;;
lError += mlToString(e.getLineNumber());
lError += &quot; char: &quot;;
lError += mlToString(e.getColumnNumber());
lError += &quot; Message: &quot;;
lError += mlXmlDOMTranscode(e.getMessage()).asInternalCharPtr();

mErrors += lError;
mErrors += &quot;\n&quot;;
}

//=============================================================================
//
// Method : void mlDOMErrorHandler::warning
//
// Description : Standard error handler interface for warnings
//
//=============================================================================
void mlDOMErrorHandler::warning(const SAXParseException& e)
{
mNumWarnings++;
CommonErrorHandler(e, &quot;Warning&quot;);
}

//=============================================================================
//
// Method : mlDOMErrorHandler::error
//
// Description : Standard error handler interface for errors
//
//=============================================================================
void mlDOMErrorHandler::error(const SAXParseException& e)
{
mNumErrors++;
CommonErrorHandler(e, &quot;Non-Fatal Error&quot;);
}

//=============================================================================
//
// Method : mlDOMErrorHandler::fatalError
//
// Description : Standard error handler interface for fatal errors
//
//=============================================================================
void mlDOMErrorHandler::fatalError(const SAXParseException& e)
{
mNumFatals++;
CommonErrorHandler(e, &quot;Fatal Error&quot;);
}


//=============================================================================
//
// Method : mlDOMErrorHandler::resetErrors
//
// Description : Clear the internal error structures
//
//=============================================================================
void mlDOMErrorHandler::resetErrors()
{
mNumWarnings = 0;
mNumErrors = 0;
mNumFatals = 0;
mErrors = &quot;&quot;;
}



Hope this helps....
 
Why XML? Pardon for saying so, but it seems it might be easier to use a
Code:
void read(ifstream& fin)
{
  string x,y;
  while(getline(fin,x,'='))
  {
    getline(fin,y,'/n');
    set(x,y);
  }
}
void set(string a,string b)
{
  //make system call to set your variables
}
then all your ini files would have to be is:
Code:
varible=something
varible2=anotherthing
which is the typical look of a lot ini files... Granted it isn't as pretty as XML, but it works.
likewise, all a XML file is, is a txt file that has a certian format
Code:
<book>
  <title>The Tale of Two cities</title>
  <author>Charlie-Boy Dikens</author>
  <chaper number=&quot;1&quot;>
     It was the best of times it was the worst of times...
  </chapter>
  ...
</book>
While it isn't as elegant as using a DOM API, oh buzz words, you could still parse like this:
Code:
char ch;
string s;
fin >> x;
if(x == '<')
{
  getline(fin,s,'>'0;
  //do the rest
}
else cerr<< &quot;Not a valid XML doc.&quot;<<endl;
...
Granted it's ugly, but it works. The question is why use somthing so coplicated when the former way is much prettier? I shall never understand... The other option is to use Java to parse the XML (as it has XML APIs) and use native code (C++) to set the enviroment variables.

Just some food for thought.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top