Classes | Typedefs | Enumerations | Functions
cpl_minixml.h File Reference

Definitions for CPL mini XML Parser/Serializer. More...

#include "cpl_port.h"

Go to the source code of this file.


struct  CPLXMLNode
 Document node structure. More...
class  CPLXMLTreeCloser
 Manage a tree of XML nodes so that all nodes are freed when the instance goes out of scope. More...


typedef struct CPLXMLNode CPLXMLNode
 Document node structure. More...


enum  CPLXMLNodeType {
  CXT_Element = 0, CXT_Text = 1, CXT_Attribute = 2, CXT_Comment = 3,
  CXT_Literal = 4
 XML node type. More...


CPLXMLNodeCPLParseXMLString (const char *)
 Parse an XML string into tree form. More...
void CPLDestroyXMLNode (CPLXMLNode *)
 Destroy a tree. More...
CPLXMLNodeCPLGetXMLNode (CPLXMLNode *poRoot, const char *pszPath)
 Find node by path. More...
CPLXMLNodeCPLSearchXMLNode (CPLXMLNode *poRoot, const char *pszTarget)
 Search for a node in document. More...
const char * CPLGetXMLValue (const CPLXMLNode *poRoot, const char *pszPath, const char *pszDefault)
 Fetch element/attribute value. More...
CPLXMLNodeCPLCreateXMLNode (CPLXMLNode *poParent, CPLXMLNodeType eType, const char *pszText)
 Create an document tree item. More...
char * CPLSerializeXMLTree (const CPLXMLNode *psNode)
 Convert tree into string document. More...
void CPLAddXMLChild (CPLXMLNode *psParent, CPLXMLNode *psChild)
 Add child node to parent. More...
int CPLRemoveXMLChild (CPLXMLNode *psParent, CPLXMLNode *psChild)
 Remove child node from parent. More...
void CPLAddXMLSibling (CPLXMLNode *psOlderSibling, CPLXMLNode *psNewSibling)
 Add new sibling. More...
CPLXMLNodeCPLCreateXMLElementAndValue (CPLXMLNode *psParent, const char *pszName, const char *pszValue)
 Create an element and text value. More...
void CPLAddXMLAttributeAndValue (CPLXMLNode *psParent, const char *pszName, const char *pszValue)
 Create an attribute and text value. More...
CPLXMLNodeCPLCloneXMLTree (const CPLXMLNode *psTree)
 Copy tree. More...
int CPLSetXMLValue (CPLXMLNode *psRoot, const char *pszPath, const char *pszValue)
 Set element value by path. More...
void CPLStripXMLNamespace (CPLXMLNode *psRoot, const char *pszNameSpace, int bRecurse)
 Strip indicated namespaces. More...
void CPLCleanXMLElementName (char *)
 Make string into safe XML token. More...
CPLXMLNodeCPLParseXMLFile (const char *pszFilename)
 Parse XML file into tree. More...
int CPLSerializeXMLTreeToFile (const CPLXMLNode *psTree, const char *pszFilename)
 Write document tree to a file. More...

Detailed Description

Definitions for CPL mini XML Parser/Serializer.

Typedef Documentation

typedef struct CPLXMLNode CPLXMLNode

Document node structure.

This C structure is used to hold a single text fragment representing a component of the document when parsed. It should be allocated with the appropriate CPL function, and freed with CPLDestroyXMLNode(). The structure contents should not normally be altered by application code, but may be freely examined by application code.

Using the psChild and psNext pointers, a hierarchical tree structure for a document can be represented as a tree of CPLXMLNode structures.

Enumeration Type Documentation

XML node type.


Node is an element


Node is a raw text value


Node is attribute


Node is an XML comment.


Node is a special literal

Function Documentation

void CPLAddXMLAttributeAndValue ( CPLXMLNode psParent,
const char *  pszName,
const char *  pszValue 

Create an attribute and text value.

This is function is a convenient short form for:

1 CPLXMLNode *psAttributeNode;
3 psAttributeNode = CPLCreateXMLNode( psParent, CXT_Attribute, pszName );
4 CPLCreateXMLNode( psAttributeNode, CXT_Text, pszValue );

It creates a CXT_Attribute node, with a CXT_Text child, and attaches the element to the passed parent.

psParentthe parent node to which the resulting node should be attached. Must not be NULL.
pszNamethe attribute name to create.
pszValuethe text to attach to the attribute. Must not be NULL.
GDAL 2.0
void CPLAddXMLChild ( CPLXMLNode psParent,
CPLXMLNode psChild 

Add child node to parent.

The passed child is added to the list of children of the indicated parent. Normally the child is added at the end of the parents child list, but attributes (CXT_Attribute) will be inserted after any other attributes but before any other element type. Ownership of the child node is effectively assumed by the parent node. If the child has siblings (its psNext is not NULL) they will be trimmed, but if the child has children they are carried with it.

psParentthe node to attach the child to. May not be NULL.
psChildthe child to add to the parent. May not be NULL. Should not be a child of any other parent.
void CPLAddXMLSibling ( CPLXMLNode psOlderSibling,
CPLXMLNode psNewSibling 

Add new sibling.

The passed psNewSibling is added to the end of siblings of the psOlderSibling node. That is, it is added to the end of the psNext chain. There is no special handling if psNewSibling is an attribute. If this is required, use CPLAddXMLChild().

psOlderSiblingthe node to attach the sibling after.
psNewSiblingthe node to add at the end of psOlderSiblings psNext chain.
void CPLCleanXMLElementName ( char *  pszTarget)

Make string into safe XML token.

Modifies a string in place to try and make it into a legal XML token that can be used as an element name. This is accomplished by changing any characters not legal in a token into an underscore.

NOTE: This function should implement the rules in section 2.3 of but it doesn't yet do that properly. We only do a rough approximation of that.

pszTargetthe string to be adjusted. It is altered in place.
CPLXMLNode* CPLCloneXMLTree ( const CPLXMLNode psTree)

Copy tree.

Creates a deep copy of a CPLXMLNode tree.

psTreethe tree to duplicate.
a copy of the whole tree.
CPLXMLNode* CPLCreateXMLElementAndValue ( CPLXMLNode psParent,
const char *  pszName,
const char *  pszValue 

Create an element and text value.

This is function is a convenient short form for:

1 CPLXMLNode *psTextNode;
2 CPLXMLNode *psElementNode;
4 psElementNode = CPLCreateXMLNode( psParent, CXT_Element, pszName );
5 psTextNode = CPLCreateXMLNode( psElementNode, CXT_Text, pszValue );
7 return psElementNode;

It creates a CXT_Element node, with a CXT_Text child, and attaches the element to the passed parent.

psParentthe parent node to which the resulting node should be attached. May be NULL to keep as freestanding.
pszNamethe element name to create.
pszValuethe text to attach to the element. Must not be NULL.
the pointer to the new element node.
CPLXMLNode* CPLCreateXMLNode ( CPLXMLNode poParent,
CPLXMLNodeType  eType,
const char *  pszText 

Create an document tree item.

Create a single CPLXMLNode object with the desired value and type, and attach it as a child of the indicated parent.

poParentthe parent to which this node should be attached as a child. May be NULL to keep as free standing.
eTypethe type of the newly created node
pszTextthe value of the newly created node
the newly created node, now owned by the caller (or parent node).
void CPLDestroyXMLNode ( CPLXMLNode psNode)

Destroy a tree.

This function frees resources associated with a CPLXMLNode and all its children nodes.

psNodethe tree to free.
CPLXMLNode* CPLGetXMLNode ( CPLXMLNode psRoot,
const char *  pszPath 

Find node by path.

Searches the document or subdocument indicated by psRoot for an element (or attribute) with the given path. The path should consist of a set of element names separated by dots, not including the name of the root element (psRoot). If the requested element is not found NULL is returned.

Attribute names may only appear as the last item in the path.

The search is done from the root nodes children, but all intermediate nodes in the path must be specified. Searching for "name" would only find a name element or attribute if it is a direct child of the root, not at any level in the subdocument.

If the pszPath is prefixed by "=" then the search will begin with the root node, and its siblings, instead of the root nodes children. This is particularly useful when searching within a whole document which is often prefixed by one or more "junk" nodes like the <?xml> declaration.

psRootthe subtree in which to search. This should be a node of type CXT_Element. NULL is safe.
pszPaththe list of element names in the path (dot separated).
the requested element node, or NULL if not found.
const char* CPLGetXMLValue ( const CPLXMLNode psRoot,
const char *  pszPath,
const char *  pszDefault 

Fetch element/attribute value.

Searches the document for the element/attribute value associated with the path. The corresponding node is internally found with CPLGetXMLNode() (see there for details on path handling). Once found, the value is considered to be the first CXT_Text child of the node.

If the attribute/element search fails, or if the found node has no value then the passed default value is returned.

The returned value points to memory within the document tree, and should not be altered or freed.

psRootthe subtree in which to search. This should be a node of type CXT_Element. NULL is safe.
pszPaththe list of element names in the path (dot separated). An empty path means get the value of the psRoot node.
pszDefaultthe value to return if a corresponding value is not found, may be NULL.
the requested value or pszDefault if not found.
CPLXMLNode* CPLParseXMLFile ( const char *  pszFilename)

Parse XML file into tree.

The named file is opened, loaded into memory as a big string, and parsed with CPLParseXMLString(). Errors in reading the file or parsing the XML will be reported by CPLError().

The "large file" API is used, so XML files can come from virtualized files.

pszFilenamethe file to open.
NULL on failure, or the document tree on success.
CPLXMLNode* CPLParseXMLString ( const char *  pszString)

Parse an XML string into tree form.

The passed document is parsed into a CPLXMLNode tree representation. If the document is not well formed XML then NULL is returned, and errors are reported via CPLError(). No validation beyond wellformedness is done. The CPLParseXMLFile() convenience function can be used to parse from a file.

The returned document tree is owned by the caller and should be freed with CPLDestroyXMLNode() when no longer needed.

If the document has more than one "root level" element then those after the first will be attached to the first as siblings (via the psNext pointers) even though there is no common parent. A document with no XML structure (no angle brackets for instance) would be considered well formed, and returned as a single CXT_Text node.

pszStringthe document to parse.
parsed tree or NULL on error.
int CPLRemoveXMLChild ( CPLXMLNode psParent,
CPLXMLNode psChild 

Remove child node from parent.

The passed child is removed from the child list of the passed parent, but the child is not destroyed. The child retains ownership of its own children, but is cleanly removed from the child list of the parent.

psParentthe node to the child is attached to.
psChildthe child to remove.
TRUE on success or FALSE if the child was not found.
CPLXMLNode* CPLSearchXMLNode ( CPLXMLNode psRoot,
const char *  pszElement 

Search for a node in document.

Searches the children (and potentially siblings) of the documented passed in for the named element or attribute. To search following siblings as well as children, prefix the pszElement name with an equal sign. This function does an in-order traversal of the document tree. So it will first match against the current node, then its first child, that child's first child, and so on.

Use CPLGetXMLNode() to find a specific child, or along a specific node path.

psRootthe subtree to search. This should be a node of type CXT_Element. NULL is safe.
pszElementthe name of the element or attribute to search for.
The matching node or NULL on failure.
char* CPLSerializeXMLTree ( const CPLXMLNode psNode)

Convert tree into string document.

This function converts a CPLXMLNode tree representation of a document into a flat string representation. White space indentation is used visually preserve the tree structure of the document. The returned document becomes owned by the caller and should be freed with CPLFree() when no longer needed.

psNodethe node to serialize.
the document on success or NULL on failure.
int CPLSerializeXMLTreeToFile ( const CPLXMLNode psTree,
const char *  pszFilename 

Write document tree to a file.

The passed document tree is converted into one big string (with CPLSerializeXMLTree()) and then written to the named file. Errors writing the file will be reported by CPLError(). The source document tree is not altered. If the output file already exists it will be overwritten.

psTreethe document tree to write.
pszFilenamethe name of the file to write to.
TRUE on success, FALSE otherwise.
int CPLSetXMLValue ( CPLXMLNode psRoot,
const char *  pszPath,
const char *  pszValue 

Set element value by path.

Find (or create) the target element or attribute specified in the path, and assign it the indicated value.

Any path elements that do not already exist will be created. The target nodes value (the first CXT_Text child) will be replaced with the provided value.

If the target node is an attribute instead of an element, the name should be prefixed with a #.

Example: CPLSetXMLValue( "Citation.Id.Description", "DOQ dataset" ); CPLSetXMLValue( "Citation.Id.Description.#name", "doq" );

psRootthe subdocument to be updated.
pszPaththe dot separated path to the target element/attribute.
pszValuethe text value to assign.
TRUE on success.
void CPLStripXMLNamespace ( CPLXMLNode psRoot,
const char *  pszNamespace,
int  bRecurse 

Strip indicated namespaces.

The subdocument (psRoot) is recursively examined, and any elements with the indicated namespace prefix will have the namespace prefix stripped from the element names. If the passed namespace is NULL, then all namespace prefixes will be stripped.

Nodes other than elements should remain unaffected. The changes are made "in place", and should not alter any node locations, only the pszValue field of affected nodes.

psRootthe document to operate on.
pszNamespacethe name space prefix (not including colon), or NULL.
bRecurseTRUE to recurse over whole document, or FALSE to only operate on the passed node.

Generated for GDAL by doxygen 1.8.8.