Writing element content
Prev |  | Next

## Writing element content

Writing element content uses many of the same steps we used above — parsing the document and walking the tree. We parse the document, then traverse the tree to find the place we want to insert our element. For this example, we want to again find the "storyinfo" element and this time insert a keyword. Then we'll write the file to disk. Full code: Appendix C, Code for Add Keyword Example

The main difference in this example is in `parseStory`:

    void
    parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {

    	 xmlNewTextChild (cur, NULL, "keyword", keyword);
        return;
    }

| The `xmlNewTextChild` function adds a new child element at the current node pointer's location in the tree, specified by `cur`.

Once the node has been added, we would like to write the document to file. Is you want the element to have a namespace, you can add it here as well. In our case, the namespace is NULL.

    	xmlSaveFormatFile (docname, doc, 1);

The first parameter is the name of the file to be written. You'll notice it is the same as the file we just read. In this case, we just write over the old file. The second parameter is a pointer to the xmlDoc structure. Setting the third parameter equal to one ensures indenting on output.

Prev | Up | Next
Retrieving Element Content | Home | Writing Attribute