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.

## Classes
struct | CPLXMLNode
| Document node structure. More...
## Typedefs
typedef struct CPLXMLNode | CPLXMLNode
| Document node structure.
## Enumerations
enum | CPLXMLNodeType { CXT_Element = 0, CXT_Text = 1, CXT_Attribute = 2, CXT_Comment = 3, CXT_Literal = 4 }
## Functions
CPLXMLNode * | CPLParseXMLString (const char *)
| Parse an XML string into tree form.
void | CPLDestroyXMLNode (CPLXMLNode *)
| Destroy a tree.
CPLXMLNode * | CPLGetXMLNode (CPLXMLNode *poRoot, const char *pszPath)
| Find node by path.
CPLXMLNode * | CPLSearchXMLNode (CPLXMLNode *poRoot, const char *pszTarget)
| Search for a node in document.
const char * | CPLGetXMLValue (CPLXMLNode *poRoot, const char *pszPath, const char *pszDefault)
| Fetch element/attribute value.
CPLXMLNode * | CPLCreateXMLNode (CPLXMLNode *poParent, CPLXMLNodeType eType, const char *pszText)
| Create an document tree item.
char * | CPLSerializeXMLTree (const CPLXMLNode *psNode)
| Convert tree into string document.
void | CPLAddXMLChild (CPLXMLNode *psParent, CPLXMLNode *psChild)
| Add child node to parent.
int | CPLRemoveXMLChild (CPLXMLNode *psParent, CPLXMLNode *psChild)
| Remove child node from parent.
void | CPLAddXMLSibling (CPLXMLNode *psOlderSibling, CPLXMLNode *psNewSibling)
| Add new sibling.
CPLXMLNode * | CPLCreateXMLElementAndValue (CPLXMLNode *psParent, const char *pszName, const char *pszValue)
| Create an element and text value.
CPLXMLNode * | CPLCloneXMLTree (CPLXMLNode *psTree)
| Copy tree.
int | CPLSetXMLValue (CPLXMLNode *psRoot, const char *pszPath, const char *pszValue)
| Set element value by path.
void | CPLStripXMLNamespace (CPLXMLNode *psRoot, const char *pszNameSpace, int bRecurse)
| Strip indicated namespaces.
void | CPLCleanXMLElementName (char *)
| Make string into safe XML token.
CPLXMLNode * | CPLParseXMLFile (const char *pszFilename)
| Parse XML file into tree.
int | CPLSerializeXMLTreeToFile (const CPLXMLNode *psTree, const char *pszFilename)
| Write document tree to a file.

## 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 heirarchical tree structure for a document can be represented as a tree of CPLXMLNode structures.

## Enumeration Type Documentation

enum CPLXMLNodeType

Enumerator:

  CXT_Element | Node is an element
  CXT_Text | Node is a raw text value
  CXT_Attribute | Node is attribute
  CXT_Comment | Node is an XML comment.
  CXT_Literal | Node is a special literal

## Function Documentation

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 (it's psNext is not NULL) they will be trimmed, but if the child has children they are carried with it.

Parameters:

  | psParent | the node to attach the child to. May not be NULL.
  | psChild | the 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().

Parameters:

  | psOlderSibling | the node to attach the sibling after.
  | psNewSibling | the node to add at the end of psOlderSiblings psNext chain.

void CPLCleanXMLElementName | ( | char * | pszTarget | ) |

Make string into safe XML token.

Modififies 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 http://www.w3.org/TR/xml11/ but it doesn't yet do that properly. We only do a rough approximation of that.

Parameters:

  | pszTarget | the string to be adjusted. It is altered in place.

CPLXMLNode* CPLCloneXMLTree | ( | CPLXMLNode * | psTree | ) |

Copy tree.

Creates a deep copy of a CPLXMLNode tree.

Parameters:

  | psTree | the tree to duplicate.

Returns:
  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:

         CPLXMLNode *psTextNode;
         CPLXMLNode *psElementNode;

         psElementNode = CPLCreateXMLNode( psParent, CXT_Element, pszName );
         psTextNode = CPLCreateXMLNode( psElementNode, CXT_Text, pszValue );

         return psElementNode;

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

Parameters:

  | psParent | the parent node to which the resulting node should be attached. May be NULL to keep as freestanding.
  | pszName | the element name to create.
  | pszValue | the text to attach to the element. Must not be NULL.

Returns:
  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.

Parameters:

  | poParent | the parent to which this node should be attached as a child. May be NULL to keep as free standing.
  | eType | the type of the newly created node
  | pszText | the value of the newly created node

Returns:
  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.

Parameters:

  | psNode | the 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. Seaching 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 it's 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.

Parameters:

  | psRoot | the subtree in which to search. This should be a node of type CXT_Element. NULL is safe.
  | pszPath | the list of element names in the path (dot separated).

Returns:
  the requested element node, or NULL if not found.

const char* CPLGetXMLValue | ( | 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 not 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.

Parameters:

  | psRoot | the subtree in which to search. This should be a node of type CXT_Element. NULL is safe.
  | pszPath | the list of element names in the path (dot separated). An empty path means get the value of the psRoot node.
  | pszDefault | the value to return if a corresponding value is not found, may be NULL.

Returns:
  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.

Parameters:

  | pszFilename | the file to open.

Returns:
  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 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.

Parameters:

  | pszString | the document to parse.

Returns:
  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 it's own children, but is cleanly removed from the child list of the parent.

Parameters:

  | psParent | the node to the child is attached to.
  | psChild | the child to remove.

Returns:
  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 it's first child, that childs first child, and so on.

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

Parameters:

  | psRoot | the subtree to search. This should be a node of type CXT_Element. NULL is safe.
  | pszElement | the name of the element or attribute to search for.

Returns:
  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.

Parameters:

  | psNode | the node to serialize.

Returns:
  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.

Parameters:

  | psTree | the document tree to write.
  | pszFilename | the name of the file to write to.

Returns:
  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" );

Parameters:

  | psRoot | the subdocument to be updated.
  | pszPath | the dot seperated path to the target element/attribute.
  | pszValue | the text value to assign.

Returns:
  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.

Parameters:

  | psRoot | the document to operate on.
  | pszNamespace | the name space prefix (not including colon), or NULL.
  | bRecurse | TRUE to recurse over whole document, or FALSE to only operate on the passed node.

Generated for GDAL by 1.7.1.