simple XML for C++ application data-file I/O

Stewart Rubenstein sdr at camsoft.com
Mon Dec 6 02:31:48 GMT 1999


I was easily able to use XML for exactly this sort of thing.  For
reading, I used James Clark's expat parser with Andy Dent's expatpp
wrapper for C++, and it dropped in quite easily.  You can get them both
from <http://www.highway1.com.au/adsoftware/expatpp.htm>.

Writing XML is almost too easy to bother getting help for.  You do have
to take some care if you're going to be dealing with text beyond
US-ASCII.  Fortunately, my OS's - MacOS and Windows - both have fairly
decent Unicode support now.

My application already has an object tree, so I just wrote the following
in the base class, and implemented the obvious virtual functions in the
subclasses that can exist in the tree:

void CDXObject::XMLWrite(std::ostream &sink) const
{
	// First write the opening tag and the attributes.
	sink << "<" << XMLObjectName() << std::endl;
	
	// The id is the only totally generic tag
	if (m_objectID != 0)
		sink << " " << kCDXML_id << "=\"" << m_objectID << "\"" << std::endl;

	// This is overridden by subclasses to write any object-specific attributes
	XMLWriteAttributes(sink);
	
	// If there's any 
	if (!XMLNeedToWriteContent() && m_contents.empty())
		sink << "/>";
	else
	{
		sink << ">";
		XMLWriteContent(sink);
           for (CDXObjectContentMap::const_iterator i = m_contents.begin();
			i != m_contents.end();  ++i)
			GetObject(i)->XMLWrite(sink);	// write each of the contained objects
		sink << "</" << XMLObjectName() << ">";
	}
}

Paul Miller wrote:
> 
> > :: I've seen a lot of discussion about DOM, SAX, RDF, etc. but none of the
> > :: solutions I've seen are very simple or straightforward for generic
> > :: application data I/O (ie. non web, e-commerce, Java-type stuff). In
> > :: other words, I'm about to roll my own, and would like to gauge interest
> > :: in a small callback-based API for simple XML I/O.
> 
> > Not sure what you mean. Are you talking about IPC, RPC?
> > Have you looked at XML-RPC and SOAP?
> 
> I should have been more clear. I just want to use XML for simple
> non-web-bound application data files (document files). I need a
> non-validating parser that I can use to efficiently parse my application
> data, without all the complexity (and overhead) of something like DOM,
> but not as general-purpose as expat.
> 
> > <Heh. Nine acronyms embedded in one brief msg.>
> 
> Yeah, XML has definitely helped spawn plenty of new TLAs.
> 
> --
> Paul Miller - stele at fxtech.com
> 
> xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
> Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
> To unsubscribe, mailto:majordomo at ic.ac.uk the following message;
> unsubscribe xml-dev
> To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
> subscribe xml-dev-digest
> List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at ic.ac.uk
Archived as: http://www.lists.ic.ac.uk/hypermail/xml-dev/ and on CD-ROM/ISBN 981-02-3594-1
To unsubscribe, mailto:majordomo at ic.ac.uk the following message;
unsubscribe xml-dev
To subscribe to the digests, mailto:majordomo at ic.ac.uk the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at ic.ac.uk)





More information about the Xml-dev mailing list