The first thing your program will need to do is call XmlDOMUtil::Initialize(). That'll handle all the initialization (including initialization of the XercesC platform library underneath everything). You need to do this once before using any other XmlDOMUtil routines, and in a multithreaded program I strongly recommend you call Initialize() before you start spawning threads. It's not critical because the routine uses a flag within the library that essentially makes the call a no-op if it's already been called, but note that it doesn't lock that flag in any way so you can get a race condition if you call it from multiple threads simultaneously. In my opinion you probably don't want to go there anyway, and if you are it's a sign you've done something very wrong somewhere.
When you're done using XmlDOMUtil, you need to call XmlDOMUtil::Terminate(). That shuts down everything and cleans up and frees memory controlled by the library and XercesC. Don't try to use any XmlDOMUtil routines after you've called Terminate(), and don't try to use any pointers to memory you got from the library. It's got an internal flag to protect against multiple calls or being called without Initialize() having been called, but the same caveats against multithreaded programs apply. After you've called Terminate(), it's safe to call Initialize() again to restart the library.
As with XmlUtil::Exception, you probably won't use XmlDOMUtil::XmlDOMException directly much. You'll mostly catch exceptions thrown by the library, and if you do need to create your own you'll likely only need to use the single-string-argument constructors.
If you wanted to parse a document and use information from it, the code would look something like this:
You might vary it, for instance by calling adoptDocument() on the document you got from parseDocument()/parseDocumentFile(), and then doing
If you're creating a document, the code (modulo the initialization stuff) would look something like this:
That code creates a document, get it's root element, then takes a list of strings and adds each one as a STRING element under the root with an "index" attribute whose value is the position 1-n of the string in the original list. Note that when creating an element you have to specify the document it's in. That's so XercesC can link it to the document and to document-level metadata like namespace declarations and encoding.
Normally you won't have to worry about these, they'll be used within the library to create the exception objects. You can of course use them if you're doing things directly with the XercesC routines and want to take exceptions they throw and convert them into a common type of exception to simplify things, but that's about the only time you'll need to construct exception objects.
Additional type strings supported by this class are "DOM", "DOM range", "SAX", "SAX parse", coming from the 4 exception types supported by the constructors. For "DOM" and "DOM range" the code field comes from the code field of the underlying exception object. "SAX" and "SAX parse" don't have an associated code, so the code will always be zero. The message for "SAX parse" exceptions includes the line and column numbers where the exception occurred followed by the actual message text.
getparser() returns a new parser object for later use. destroyParser() takes a parser object and frees it and all it's associated memory (including that of any documents you parsed using it and didn't adopt). Note that you can use a single parser to parse multiple documents. releaseDocuments() provides a way for you to release the memory used to hold parsed documents without destroying the parser. If you needed to process several large documents one at a time, you'd simply call releaseDocuments() after each one to free up the memory.
To make life a bit easier when dealing with exceptions, there's an XmlDOMParser class defined. This class doesn't do anything itself. Objects of this class can't be copied or assigned. The default constructor gets a parser using getParser(), the destructor calls destroyParser() on the saved parser (if any), and the no-arguments function-call operator returns a pointer to the parser just like you'd get from getParser(). It's only purpose is to make it easier to keep parsers cleaned up by giving you a way to allocate them in a normal stack-based variable that'll be automatically cleaned up upon exit from the block it's defined in, even if the exit is because of a thrown exception or some other abnormal exit path.
parseDocument() and parseDocumentFile() are the basic parsing functions. One parses the document from a standard C++ string, the other parses from a named file on disk. The enc_string argument to each is used if the character encoding isn't specified in the XML itself and you need to specify it. This string matches the encoding attribute on the ?xml declaration, and would be something like "UTF-8" or "ISO-8859-15". Often you won't need to specify it since either the XML document will have the encoding attribute present or it'll be in the XML default UTF-8 encoding already.
getRoot() returns the root element node of the document (the immediate child of the document node itself). There's only one root element allowed.
adoptDocument() adopts the document from the parser. Normally the DOM objects that make up the document tree are owned by the parser after a document is parsed, and they're freed when the parser is destroyed or releaseDocuments() is called on the parser. By calling adoptDocument() you're taking ownership of the DOM tree's memory yourself, and the parser will release control over the memory to you. Note that the memory's still controlled by the XercesC memory manager, so while you can destroy the parser without releasing an adopted document it's memory will still be freed when you call Terminate() and shut down the XercesC library.
cloneDocument() is similar to adoptDocument() in that it returns a document that's controlled by you. But instead of returning the original document it returns a deep copy of the document you passed in, a copy you can alter without changing the original. Just remember that, as with adoptDocument(), you're responsible for releasing the document once you're done with it by calling the release() method on it.
These translate between namespace URIs and prefixes. You need to supply a node because namespace prefixes are specific to a particular document. The same namespace URI can map to prefix "abc" in one document and "xyz" in another, so without a node (and it's containing document) to refer to it makes no sense to talk about prefixes.
Note that the library doesn't have routines yet to truly handle namespace prefixes and URIs in functions like searching for elements or creating elements and attributes. I plan to add them, it's just that my current needs don't involve disambiguating names based on namespaces.
This group of functions help in locating elements within a document tree. findElement() takes a single element name and returns a list of pointers to all occurrences of that element name, while findElements() accepts a list of element names and locates multiple element names in one search. They come in two variants. The first takes a pointer to an element node as the root of the search, and locates elements beneath that root. The second takes a pointer to a document node, searches starting at the root element of the document and includes the root element in the search. The recurse argument controls whether the search is recursive down the entire depth of the tree or limited only to immediate children of the root. You can use non-recursive searches to search based on the relative positions of elements in the tree (eg. search for all CUSTOMER elements, and then search for ADDRESS elements beneath them).
Note that in all cases the search doesn't continue in the sub-tree below an element that matches the search. I found I rarely wanted to locate such children, I more commonly wanted to exclude them. I do plan on adding functionality to continue the search below matched elements at some point.
I also plan on adding functions to search based on sequences of names, so you could directly search for ADDRESS elements underneath CUSTOMER elements. The code's more complex, though, and I didn't want to hack together something that wouldn't work well or had an inconvenient way of specifying sequences in combination with the list-of-names form. I'd rather take time to think about it first.
getName(), getNamespacePrefix() and getNamespaceURI() return the obvious information about a node's name. Commonly the node will be an element or attribute. Other types of nodes, eg. text content, don't have names and these functions will return the empty string if called on them.
getValue() returns the node's contents, which varies depending on the type of node. For elements it's the contained text content. For attributes it's the attribute value. For text and CDATA nodes it's the text data of the node. Note that for elements it does not include child nodes or their text content, only text content that's directly within the element itself.
getChildren() returns a list of the child elements of an element. It only returns elements, it skips non-element children (eg. text content).
getAttributes() returns the list of attribute nodes for an element. hasAttribute() returns a flag indicating whether the attribute is present. getAttribute() returns a single attribute, or the NULL pointer if the attribute isn't present. getAttributeValue() returns the value of an attribute, or the empty string if the attribute wasn't present. Note that if you use getAttributeValue() and need to distinguish between "attribute not present" and "attribute present but with no value" you'll need to also use hasAttribute(). Most of the time attributes are either present/not-present (no value that you care about) or has-value/no-value (with not-present being functionally equivalent to no-value), so you shouldn't need the extra check often.
The basic functions to create an empty document and, if needed, a DTD. Note that you don't actually populate a DTD directly, it's contents are loaded as you create it based on the public and system ID strings. Usually that means the DTD's loaded from a URL or copy of the DTD alread in the system DTD library. Read the XercesC library documentation about DTDs if you need to work with them. A lot of the time all you'll need is the empty document and won't need to associate a DTD with it.
Create either a standard text-content node (parsed character data) or a CDATA node (unparsed data). createText() is likely to be the most common one, suitable for ordinary plain-text content. createCDATA() would be used when you need to include content that can't safely be parsed as XML. One common use is for holding encoded binary data, eg. base64-encoded data, hence the flag to let you automatically base64-encode the text in the process of creating the node. Note that the base64_split flag, which nominally controls whether base64-encoded content is split into lines or left as a single very long string, is ignored at the moment (the XercesC base64-encoding functions don't allow unsplit output and I haven't gotten around to writing code to undo the line-splitting). createEntity() creates an XML entity reference from the entity name, for when you need to create text content that includes embedded entities.
These are the functions you'll more commonly use to create elements and set attributes and content on them. The first form of createElement() creates an empty element with no content, which you'd use setContent() or setCDATAContent() on to attach text or CDATA content you'd created with createText() or createCDATA(). The second form of createElement() lets you pass in a string to set as the content of the element. Normally it'll be ordinary text, but you can flag it as CDATA content. I need to add flags for base64-encoding of CDATA content.
setAttributeValue() lets you set an attribute on an element. There first form sets a value for the attribute, the second allows you to set an attribute without a value (the attribute's just a flag).
addSibling() and addSiblings() add sibling nodes to a given node, either before or after the location node in question.
removeChild() and removeChildren() remove child elements from a node. The removed elements are returned, and it's the caller's responsibility to deallocate them when necessary. Note that removeChildren() has an issue with exceptions: if some children are successfully removed and then an exception occurs, the list of successfully-removed children can't be returned but we can't safely undo the removals either. The children that were removed have to be released, and even though removeChildren() didn't complete successfully those child nodes will have been removed from the parent. This isn't ideal, but it's the only way that's safe from memory leaks. If you need absolute control, use removeChild() to remove one child node at a time and handle errors as they occur.