libyang  1.0.253
YANG data modeling language library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
XML Parser
Collaboration diagram for XML Parser:

Modules

 XML parser options
 
 XML printer options
 

Data Structures

struct  lyxml_ns
 Namespace definition. More...
 
struct  lyxml_attr
 Element's attribute definition. More...
 
struct  lyxml_elem
 Structure describing an element in an XML tree. More...
 

Typedefs

typedef enum lyxml_attr_type LYXML_ATTR_TYPE
 enumeration of attribute types More...
 

Enumerations

enum  lyxml_attr_type { LYXML_ATTR_STD = 1, LYXML_ATTR_NS = 2, LYXML_ATTR_STD_UNRES = 3 }
 enumeration of attribute types More...
 

Functions

struct lyxml_elemlyxml_parse_mem (struct ly_ctx *ctx, const char *data, int options)
 Parse XML from in-memory string. More...
 
struct lyxml_elemlyxml_parse_path (struct ly_ctx *ctx, const char *filename, int options)
 Parse XML from filesystem. More...
 
int lyxml_print_file (FILE *stream, const struct lyxml_elem *elem, int options)
 Dump XML tree to a IO stream. More...
 
int lyxml_print_fd (int fd, const struct lyxml_elem *elem, int options)
 Dump XML tree to a IO stream. More...
 
int lyxml_print_mem (char **strp, const struct lyxml_elem *elem, int options)
 Dump XML tree to a IO stream. More...
 
int lyxml_print_clb (ssize_t(*writeclb)(void *arg, const void *buf, size_t count), void *arg, const struct lyxml_elem *elem, int options)
 Dump XML tree to a IO stream. More...
 
struct lyxml_elemlyxml_dup (struct ly_ctx *ctx, struct lyxml_elem *root)
 Duplicate the XML tree into the different content. More...
 
void lyxml_free (struct ly_ctx *ctx, struct lyxml_elem *elem)
 Free (and unlink from the XML tree) the specified element with all its attributes and namespace definitions. More...
 
void lyxml_free_withsiblings (struct ly_ctx *ctx, struct lyxml_elem *elem)
 Free (and unlink from the XML tree) the specified (sub)tree with all its attributes and namespace definitions. In contrast to lyxml_free(), free also all the element's siblings (preceding as well as following). More...
 
void lyxml_unlink (struct ly_ctx *ctx, struct lyxml_elem *elem)
 Unlink the element from its parent. In contrast to lyxml_free(), after return the caller can still manipulate with the elem. Any namespaces are corrected and copied, if needed. More...
 
const char * lyxml_get_attr (const struct lyxml_elem *elem, const char *name, const char *ns)
 Get value of the attribute in the specified element. More...
 
struct lyxml_nslyxml_get_ns (const struct lyxml_elem *elem, const char *prefix)
 Get namespace definition of the given prefix in context of the specified element. More...
 

Detailed Description

Simplified libyang XML parser for XML data modeled by YANG.


Data Structure Documentation

struct lyxml_ns

Namespace definition.

The structure is actually casted lyxml_attr structure which covers all attributes defined in an element. The namespace definition is in this case also covered by lyxml_attr structure.

Definition at line 58 of file xml.h.

Data Fields
LYXML_ATTR_TYPE type

type of the attribute = LYXML_ATTR_NS

struct lyxml_ns * next

next sibling attribute

struct lyxml_elem * parent

parent node of the attribute

const char * prefix

the namespace prefix if defined, NULL for default namespace

const char * value

the namespace value

struct lyxml_attr

Element's attribute definition.

The structure actually covers both the attributes as well as namespace definitions.

Attributes are being connected only into a singly linked list (compare it with the elements).

Definition at line 75 of file xml.h.

Data Fields
LYXML_ATTR_TYPE type

type of the attribute

struct lyxml_attr * next

next sibling attribute

struct lyxml_ns * ns

pointer to the namespace of the attribute if any

const char * name

name of the attribute (the LocalPart of the qualified name)

const char * value

data stored in the attribute

struct lyxml_elem

Structure describing an element in an XML tree.

If the name item is NULL, then the content is part of the mixed content.

Children elements are connected in a half ring doubly linked list:

  • first's prev pointer points to the last children
  • last's next pointer is NULL

Definition at line 92 of file xml.h.

Data Fields
char flags

special flags

struct lyxml_elem * parent

parent node

struct lyxml_attr * attr

first attribute declared in the element

struct lyxml_elem * child

first children element

struct lyxml_elem * next

next sibling node

struct lyxml_elem * prev

previous sibling node

const char * name

name of the element

struct lyxml_ns * ns

namespace of the element

const char * content

text content of the node if any

Typedef Documentation

enumeration of attribute types

Enumeration Type Documentation

enumeration of attribute types

Enumerator
LYXML_ATTR_STD 

standard XML attribute

LYXML_ATTR_NS 

XML namespace definition

LYXML_ATTR_STD_UNRES 

standard XML attribute with unresolved namespace, its ns attribute is actually prefix

Definition at line 45 of file xml.h.

Function Documentation

struct lyxml_elem* lyxml_parse_mem ( struct ly_ctx *  ctx,
const char *  data,
int  options 
)

Parse XML from in-memory string.

Parameters
[in]ctxlibyang context to use
[in]dataPointer to a NULL-terminated string containing XML data to parse.
[in]optionsParser options, see XML parser options.
Returns
Pointer to the root of the parsed XML document tree or NULL in case of empty data. To free the returned data, use lyxml_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, #ly_errno contains appropriate error code (see #LY_ERR).
struct lyxml_elem* lyxml_parse_path ( struct ly_ctx *  ctx,
const char *  filename,
int  options 
)

Parse XML from filesystem.

Parameters
[in]ctxlibyang context to use
[in]filenamePath to the file where read data to parse
[in]optionsParser options, see XML parser options.
Returns
Pointer to the root of the parsed XML document tree or NULL in case of empty file. To free the returned data, use lyxml_free(). In these cases, the function sets #ly_errno to LY_SUCCESS. In case of error, #ly_errno contains appropriate error code (see #LY_ERR).
int lyxml_print_file ( FILE *  stream,
const struct lyxml_elem elem,
int  options 
)

Dump XML tree to a IO stream.

To write data into a file descriptor instead of file stream, use lyxml_print_fd().

Parameters
[in]streamIO stream to print out the tree.
[in]elemRoot element of the XML tree to print
[in]optionsDump options, see XML printer options.
Returns
number of printed characters.
int lyxml_print_fd ( int  fd,
const struct lyxml_elem elem,
int  options 
)

Dump XML tree to a IO stream.

Same as lyxml_dump(), but it writes data into the given file descriptor.

Parameters
[in]fdFile descriptor to print out the tree.
[in]elemRoot element of the XML tree to print
[in]optionsDump options, see XML printer options.
Returns
number of printed characters.
int lyxml_print_mem ( char **  strp,
const struct lyxml_elem elem,
int  options 
)

Dump XML tree to a IO stream.

Same as lyxml_dump(), but it allocates memory and store the data into it. It is up to caller to free the returned string by free().

Parameters
[out]strpPointer to store the resulting dump.
[in]elemRoot element of the XML tree to print
[in]optionsDump options, see XML printer options.
Returns
number of printed characters.
int lyxml_print_clb ( ssize_t(*)(void *arg, const void *buf, size_t count)  writeclb,
void *  arg,
const struct lyxml_elem elem,
int  options 
)

Dump XML tree to a IO stream.

Same as lyxml_dump(), but it writes data via the provided callback.

Parameters
[in]writeclbCallback function to write the data (see write(1)).
[in]argOptional caller-specific argument to be passed to the writeclb callback.
[in]elemRoot element of the XML tree to print
[in]optionsDump options, see XML printer options.
Returns
number of printed characters.
struct lyxml_elem* lyxml_dup ( struct ly_ctx *  ctx,
struct lyxml_elem root 
)

Duplicate the XML tree into the different content.

Date parser requires to have the input XML tree in the same context as the resulting data tree. Therefore, if you need to parse a single XML tree into a different contexts, you have to duplicate the source XML tree into the required context first.

Parameters
[in]ctxTarget context for the result.
[in]rootRoot node of the XML tree to duplicate. If an internal node is provided, the parents are not duplicated and only the specified subtree is duplicated.
Returns
Pointer to the duplicated tree or NULL on error.
void lyxml_free ( struct ly_ctx *  ctx,
struct lyxml_elem elem 
)

Free (and unlink from the XML tree) the specified element with all its attributes and namespace definitions.

Parameters
[in]ctxlibyang context to use
[in]elemPointer to the element to free.
void lyxml_free_withsiblings ( struct ly_ctx *  ctx,
struct lyxml_elem elem 
)

Free (and unlink from the XML tree) the specified (sub)tree with all its attributes and namespace definitions. In contrast to lyxml_free(), free also all the element's siblings (preceding as well as following).

Parameters
[in]ctxlibyang context to use
[in]elemPointer to the element to free.
void lyxml_unlink ( struct ly_ctx *  ctx,
struct lyxml_elem elem 
)

Unlink the element from its parent. In contrast to lyxml_free(), after return the caller can still manipulate with the elem. Any namespaces are corrected and copied, if needed.

Parameters
[in]ctxlibyang context to use.
[in]elemElement to unlink from its parent (if any).
const char* lyxml_get_attr ( const struct lyxml_elem elem,
const char *  name,
const char *  ns 
)

Get value of the attribute in the specified element.

struct lyxml_ns* lyxml_get_ns ( const struct lyxml_elem elem,
const char *  prefix 
)

Get namespace definition of the given prefix in context of the specified element.

Parameters
[in]elemElement where start namespace searching
[in]prefixPrefix of the namespace to search for
Returns
Namespace definition or NULL if no such namespace exists