<libroxml  version="3.0.2" />
contact: tristan.lelong@libroxml.net
Functions
roxml_edit.c File Reference

XML document edition module. More...

#include <string.h>
#include <stdlib.h>
#include "roxml_core.h"
#include "roxml_mem.h"

Go to the source code of this file.

Functions

ROXML_STATIC ROXML_INT node_troxml_get_real_prev_sibling (node_t *n)
 get real sibling
 
ROXML_STATIC ROXML_INT void roxml_reset_ns (node_t *n, node_t *ns)
 internal function
 
ROXML_STATIC ROXML_INT void roxml_del_std_node (node_t *n)
 node deletion function
 
ROXML_STATIC ROXML_INT void roxml_del_txt_node (node_t *n)
 text node deletion function
 
ROXML_STATIC ROXML_INT void roxml_del_arg_node (node_t *n)
 attribute node deletion function
 
ROXML_STATIC ROXML_INT node_troxml_prepend_node (node_t *parent, node_t *n)
 
ROXML_STATIC ROXML_INT node_troxml_parent_node (node_t *parent, node_t *n, int position)
 node relocate function
 
ROXML_API void roxml_del_node (node_t *n)
 node deletion function
 
ROXML_STATIC ROXML_INT void roxml_generate_cmt_node (node_t *n, char *content)
 
ROXML_STATIC ROXML_INT void roxml_generate_txt_node (node_t *n, char *content)
 
ROXML_STATIC ROXML_INT void roxml_generate_elm_node (node_t *n, char *name, char *content)
 
ROXML_STATIC ROXML_INT void roxml_generate_pi_node (node_t *n, char *name, char *content)
 
ROXML_STATIC ROXML_INT void roxml_generate_attr_node (node_t *n, int type, char *name, char *content)
 
ROXML_INT int roxml_add_node_check (node_t *parent, int type, char *name, char *content)
 
ROXML_API node_troxml_add_node (node_t *parent, int position, int type, char *name, char *content)
 add a node to the tree
 
ROXML_API node_troxml_set_ns (node_t *n, node_t *ns)
 namespace setter function
 

Detailed Description

XML document edition module.

(C) Copyright 2014 Tristan Lelong trist.nosp@m.an.l.nosp@m.elong.nosp@m.@lib.nosp@m.roxml.nosp@m..net

SPDX-Licence-Identifier: LGPL-2.1+ The author added a static linking exception, see License.txt.

Definition in file roxml_edit.c.

Function Documentation

◆ roxml_add_node()

roxml_add_node ( node_t parent,
int  position,
int  type,
char *  name,
char *  value 
)

add a node to the tree

this function add a new node to the tree. This will not update de buffer or file, only the RAM loaded tree. One should call roxml_commit_changes to save modifications. If the parent node is an ROXML_ELM_NODE, then, new node will be added as a child. Else the node will be added as a sibling of the parent node. In the later case, position parameter describes the position in the sibling list, instead of position in the children list.

Parameters
parentthe parent node
positionthe position as a child of parent 1 is the first child, 0 for auto position at the end of children list...
typethe type of node between ROXML_ATTR_NODE, ROXML_ELM_NODE, ROXML_TXT_NODE, ROXML_CDATA_NODE, ROXML_PI_NODE, ROXML_CMT_NODE, ROXML_NSDEF_NODE, ROXML_NS_NODE.
namethe name of the node (mandatory for ROXML_ATTR_NODE and ROXML_ELM_NODE and ROXML_PI_NODE and ROXML_NSDEF_NODE and ROXML_NS_NODE only)
valuethe text content (mandatory for ROXML_TXT_NODE, ROXML_CDATA_NODE, ROXML_CMT_NODE, ROXML_ATTR_NODE and ROXML_NSDEF_NODE optional for ROXML_ELM_NODE, ROXML_PI_NODE). The text content for an attribute is the attribute value
Returns
the newly created node
See also
roxml_commit_changes
roxml_commit_buffer
roxml_commit_file
roxml_del_node
roxml_close

Escaping the as per XML specifications can be done by adding the ROXML_ESCAPED_MOD modifier to node type as showed below:

roxml_add_node(NULL, 0, ROXML_ESCAPE_MODE | ROXML_ELM_NODE, "node1", "content <that> needs escaping");
#define ROXML_ELM_NODE
Definition roxml.h:70
ROXML_API node_t * roxml_add_node(node_t *parent, int position, int type, char *name, char *content)
add a node to the tree
Definition roxml_edit.c:353

This will create a node that exports as:

 * <node1>content &lt;that&gt; needs escaping</node1>
 * 

paramaters name and value depending on node type:

  • ROXML_ELM_NODE take at least a node name. the parameter value is optional and represents the text content.
  • ROXML_TXT_NODE ignore the node name. the parameter value represents the text content.
  • ROXML_CDATA_NODE ignore the node name. the parameter value represents the text content that will be encapsulated in CDATA tags.
  • ROXML_CMT_NODE ignore the node name. the parameter value represents the comment.
  • ROXML_PI_NODE take the node name as process-instruction target. the parameter value represents the content of processing-instruction.
  • ROXML_ATTR_NODE take an attribute name. and the attribute value as given by parameter value.
  • ROXML_NSDEF_NODE take an attribute name (empty string for default namespace). and the namespace value as given by parameter value.
  • ROXML_NS_NODE take an attribute name (empty string for default namespace).

some examples to obtain this xml result file

<root>
 <!-- sample XML file -->
 <item id="42">
  <price>
   24
  </price>
 </item>
</root>
#include <roxml.h>
int main(void)
{
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", "24");
roxml_commit_changes(root, "/tmp/test.xml", NULL, 1);
roxml_close(root);
return 0;
}
header for libroxml.so
#define ROXML_CMT_NODE
Definition roxml.h:86
#define ROXML_ATTR_NODE
Definition roxml.h:51
ROXML_API int roxml_commit_changes(node_t *n, char *dest, char **buffer, int human)
sync function
ROXML_API void roxml_close(node_t *n)
unload function
Definition roxml_core.c:25
node_t structure

Or also:

#include <roxml.h>
int main(void)
{
node_t *root = roxml_add_node(NULL, 0, ROXML_ELM_NODE, "xml", NULL);
node_t *tmp = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample XML file");
tmp = roxml_add_node(root, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(tmp, 0, ROXML_ATTR_NODE, "id", "42");
tmp = roxml_add_node(tmp, 0, ROXML_ELM_NODE, "price", NULL);
tmp = roxml_add_node(tmp, 0, ROXML_TXT_NODE, NULL, "24");
roxml_commit_changes(root, "/tmp/test.xml", NULL, 1);
roxml_close(root);
return 0;
}
#define ROXML_TXT_NODE
Definition roxml.h:78

If you need a valid XML doc, just start by adding a corresponding process-instruction. Example, to obtain this xml file:

<?xml version="1.0" encoding="UTF-8"?>
<!--sample file-->
<doc>
 basic content
 <item/>
 <item/>
 <item/>
</doc>
#include <roxml.h>
int main(void)
{
node_t *root = roxml_add_node(NULL, 0, ROXML_PI_NODE, "xml", "version=\"1.0\" encoding=\"UTF-8\"");
node_t *node = roxml_add_node(root, 0, ROXML_CMT_NODE, NULL, "sample file");
node = roxml_add_node(root, 0, ROXML_ELM_NODE, "doc", "basic content");
roxml_add_node(node, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(node, 0, ROXML_ELM_NODE, "item", NULL);
roxml_add_node(node, 0, ROXML_ELM_NODE, "item", NULL);
roxml_commit_changes(root, "/tmp/test.xml", NULL, 1);
roxml_close(root);
return 0;
}
#define ROXML_PI_NODE
Definition roxml.h:94

Definition at line 353 of file roxml_edit.c.

◆ roxml_add_node_check()

ROXML_INT int roxml_add_node_check ( node_t parent,
int  type,
char *  name,
char *  content 
)

Definition at line 313 of file roxml_edit.c.

◆ roxml_del_arg_node()

roxml_del_arg_node ( node_t n)

attribute node deletion function

this function delete an attribute node

Parameters
nthe node to delete
Returns

Definition at line 127 of file roxml_edit.c.

◆ roxml_del_node()

roxml_del_node ( node_t n)

node deletion function

this function delete a node from the tree. The node is not really deleted from the file or buffer until the roxml_commit_changes is called, but it won't be visible anymore in the XML tree.

Parameters
nthe node to delete
Returns
See also
roxml_add_node
roxml_commit_changes
roxml_commit_buffer
roxml_commit_file
Warning
when removing a nsdef node, all node using this namespace will be updated and inherit their parent namespace

Definition at line 204 of file roxml_edit.c.

◆ roxml_del_std_node()

roxml_del_std_node ( node_t n)

node deletion function

this function delete a standard node

Parameters
nthe node to delete
Returns

Definition at line 85 of file roxml_edit.c.

◆ roxml_del_txt_node()

roxml_del_txt_node ( node_t n)

text node deletion function

this function delete a text node

Parameters
nthe node to delete
Returns

Definition at line 110 of file roxml_edit.c.

◆ roxml_generate_attr_node()

ROXML_STATIC ROXML_INT void roxml_generate_attr_node ( node_t n,
int  type,
char *  name,
char *  content 
)

Definition at line 281 of file roxml_edit.c.

◆ roxml_generate_cmt_node()

ROXML_STATIC ROXML_INT void roxml_generate_cmt_node ( node_t n,
char *  content 
)

Definition at line 220 of file roxml_edit.c.

◆ roxml_generate_elm_node()

ROXML_STATIC ROXML_INT void roxml_generate_elm_node ( node_t n,
char *  name,
char *  content 
)

Definition at line 238 of file roxml_edit.c.

◆ roxml_generate_pi_node()

ROXML_STATIC ROXML_INT void roxml_generate_pi_node ( node_t n,
char *  name,
char *  content 
)

Definition at line 262 of file roxml_edit.c.

◆ roxml_generate_txt_node()

ROXML_STATIC ROXML_INT void roxml_generate_txt_node ( node_t n,
char *  content 
)

Definition at line 229 of file roxml_edit.c.

◆ roxml_get_real_prev_sibling()

node_t * roxml_get_real_prev_sibling ( node_t n)

get real sibling

This function returns the real previous sibling of a node no matter what its type is.

Warning
do not call this function if n->prnt == NULL
Parameters
nis one node of the tree
Returns
the previous sibling

Definition at line 26 of file roxml_edit.c.

◆ roxml_parent_node()

roxml_parent_node ( node_t parent,
node_t n,
int  position 
)

node relocate function

this function change the position of a node in its parent list

Parameters
parentthe parent node
nthe node to parent
positionthe position, 0 means or > nb children means at the end
Returns

Definition at line 165 of file roxml_edit.c.

◆ roxml_prepend_node()

ROXML_STATIC ROXML_INT node_t * roxml_prepend_node ( node_t parent,
node_t n 
)

Definition at line 144 of file roxml_edit.c.

◆ roxml_reset_ns()

void ROXML_STATIC ROXML_INT roxml_reset_ns ( node_t n,
node_t ns 
)

internal function

This function remove the namespace of a node if it is the one specified as argument, and replace it with its parent one

Parameters
nis one node of the tree
nsis one nsdef of the tree
Returns
void

Definition at line 48 of file roxml_edit.c.

◆ roxml_set_ns()

node_t * roxml_set_ns ( node_t n,
node_t ns 
)

namespace setter function

This function set the namespace of a node to the given namespace definition. The namespace must be previously defined in the xml tree in an ancestor of node n.

Parameters
nis one node of the tree
nsis one nsdef node of the tree
Returns
the node or ROXML_INVALID_DOC (NULL) if ns cannot be set
See also
roxml_add_node
roxml_get_ns
roxml_get_nodes
Warning
: Setting a namespace to a node is recursif:
  • it will update all element and attribute that are descendant from current node
  • namespace will be applied to all new node added as descendant as current node

Definition at line 395 of file roxml_edit.c.