SDTS_AL
Classes | Typedefs | Enumerations | Functions
cpl_minixml.h File Reference
#include "cpl_port.h"

Go to the source code of this file.

Classes

struct  CPLXMLNode
 

Typedefs

typedef struct CPLXMLNode CPLXMLNode
 

Enumerations

enum  CPLXMLNodeType {
  CXT_Element = 0, CXT_Text = 1, CXT_Attribute = 2, CXT_Comment = 3,
  CXT_Literal = 4
}
 

Functions

CPLXMLNode CPL_DLL * CPLParseXMLString (const char *)
 Parse an XML string into tree form. More...
 
void CPL_DLL CPLDestroyXMLNode (CPLXMLNode *)
 Destroy a tree. More...
 
CPLXMLNode CPL_DLL * CPLGetXMLNode (CPLXMLNode *poRoot, const char *pszPath)
 Find node by path. More...
 
CPLXMLNode CPL_DLL * CPLSearchXMLNode (CPLXMLNode *poRoot, const char *pszTarget)
 Search for a node in document. More...
 
const char CPL_DLL * CPLGetXMLValue (CPLXMLNode *poRoot, const char *pszPath, const char *pszDefault)
 Fetch element/attribute value. More...
 
CPLXMLNode CPL_DLL * CPLCreateXMLNode (CPLXMLNode *poParent, CPLXMLNodeType eType, const char *pszText)
 Create an document tree item. More...
 
char CPL_DLL * CPLSerializeXMLTree (const CPLXMLNode *psNode)
 Convert tree into string document. More...
 
void CPL_DLL CPLAddXMLChild (CPLXMLNode *psParent, CPLXMLNode *psChild)
 Add child node to parent. More...
 
int CPL_DLL CPLRemoveXMLChild (CPLXMLNode *psParent, CPLXMLNode *psChild)
 Remove child node from parent. More...
 
void CPL_DLL CPLAddXMLSibling (CPLXMLNode *psOlderSibling, CPLXMLNode *psNewSibling)
 Add new sibling. More...
 
CPLXMLNode CPL_DLL * CPLCreateXMLElementAndValue (CPLXMLNode *psParent, const char *pszName, const char *pszValue)
 Create an element and text value. More...
 
void CPL_DLL CPLAddXMLAttributeAndValue (CPLXMLNode *psParent, const char *pszName, const char *pszValue)
 Create an attribute and text value. More...
 
CPLXMLNode CPL_DLL * CPLCloneXMLTree (CPLXMLNode *psTree)
 Copy tree. More...
 
int CPL_DLL CPLSetXMLValue (CPLXMLNode *psRoot, const char *pszPath, const char *pszValue)
 Set element value by path. More...
 
void CPL_DLL CPLStripXMLNamespace (CPLXMLNode *psRoot, const char *pszNameSpace, int bRecurse)
 Strip indicated namespaces. More...
 
void CPL_DLL CPLCleanXMLElementName (char *)
 Make string into safe XML token. More...
 
CPLXMLNode CPL_DLL * CPLParseXMLFile (const char *pszFilename)
 Parse XML file into tree. More...
 
int CPL_DLL 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

◆ CPLXMLNode

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

◆ CPLXMLNodeType

XML node type

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

◆ CPLAddXMLAttributeAndValue()

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

Create an attribute and text value.

This is function is a convenient short form for:

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

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

Parameters
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.
Since
GDAL 2.0

◆ CPLAddXMLChild()

void CPL_DLL 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.

Parameters
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.

◆ CPLAddXMLSibling()

void CPL_DLL 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
psOlderSiblingthe node to attach the sibling after.
psNewSiblingthe node to add at the end of psOlderSiblings psNext chain.

◆ CPLCleanXMLElementName()

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

Parameters
pszTargetthe string to be adjusted. It is altered in place.

◆ CPLCloneXMLTree()

CPLXMLNode CPL_DLL* CPLCloneXMLTree ( CPLXMLNode psTree)

Copy tree.

Creates a deep copy of a CPLXMLNode tree.

Parameters
psTreethe tree to duplicate.
Returns
a copy of the whole tree.

◆ CPLCreateXMLElementAndValue()

CPLXMLNode CPL_DLL* 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
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.
Returns
the pointer to the new element node.

◆ CPLCreateXMLNode()

CPLXMLNode CPL_DLL* 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
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
Returns
the newly created node, now owned by the caller (or parent node).

◆ CPLDestroyXMLNode()

void CPL_DLL CPLDestroyXMLNode ( CPLXMLNode psNode)

Destroy a tree.

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

Parameters
psNodethe tree to free.

◆ CPLGetXMLNode()

CPLXMLNode CPL_DLL* 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.

Parameters
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).
Returns
the requested element node, or NULL if not found.

◆ CPLGetXMLValue()

const char CPL_DLL* 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 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.

Parameters
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.
Returns
the requested value or pszDefault if not found.

◆ CPLParseXMLFile()

CPLXMLNode CPL_DLL* 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
pszFilenamethe file to open.
Returns
NULL on failure, or the document tree on success.

◆ CPLParseXMLString()

CPLXMLNode CPL_DLL* 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.

Parameters
pszStringthe document to parse.
Returns
parsed tree or NULL on error.

◆ CPLRemoveXMLChild()

int CPL_DLL 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.

Parameters
psParentthe node to the child is attached to.
psChildthe child to remove.
Returns
TRUE on success or FALSE if the child was not found.

◆ CPLSearchXMLNode()

CPLXMLNode CPL_DLL* 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.

Parameters
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.
Returns
The matching node or NULL on failure.

◆ CPLSerializeXMLTree()

char CPL_DLL* 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
psNodethe node to serialize.
Returns
the document on success or NULL on failure.

◆ CPLSerializeXMLTreeToFile()

int CPL_DLL 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
psTreethe document tree to write.
pszFilenamethe name of the file to write to.
Returns
TRUE on success, FALSE otherwise.

◆ CPLSetXMLValue()

int CPL_DLL 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
psRootthe subdocument to be updated.
pszPaththe dot separated path to the target element/attribute.
pszValuethe text value to assign.
Returns
TRUE on success.

◆ CPLStripXMLNamespace()

void CPL_DLL 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
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.