TheCodeThe name is add_node.c.
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <libxml/xmlmemory. h>
# Include <libxml/parser. h>
# Include "null. H"
Void parsestory (xmldocptr doc, xmlnodeptr cur, char * keyword)
{
Xmlnewtextchild (cur, null, "keyword", keyword );
Cur = cur-> xmlchildrennode;
While (cur! = NULL)
{
Printf ("cur-> name: % s \ n", cur-> name );
Cur = cur-> next;
}
Return;
}
Xmldocptr parsedoc (char * docname, char * keyword)
{
Xmldocptr Doc;
Xmlnodeptr cur;
Printf ("before xmlparsefile-> docname: % s \ n", docname );
Doc = xmlparsefile (docname );
Printf ("after xmlparsefile-> docname: % s \ n", docname );
If (Doc = NULL)
{
Fprintf (stderr, "document not parsed successfully. \ n ");
Return (null );
}
Cur = xmldocgetrootelement (DOC );
If (cur = NULL)
{
Fprintf (stderr, "empty document \ n ");
Xmlfreedoc (DOC );
Return (null );
}
If (xmlstrcmp (cur-> name, (const xmlchar *) "story "))
{
Fprintf (stderr, "Document of the wrong type, root node! = Story \ n ");
Xmlfreedoc (DOC );
Return (null );
}
Cur = cur-> xmlchildrennode;
While (cur! = NULL)
{
If ((! Xmlstrcmp (cur-> name, (const xmlchar *) "storyinfo ")))
{
Parsestory (Doc, cur, keyword );
}
Cur = cur-> next;
}
Return (DOC );
}
Int main (INT argc, char ** argv)
{
Char * docname;
Char * keyword;
Xmldocptr Doc;
If (argc <= 2)
{
Printf ("Usage: % s docname, keyword \ n", argv [0]);
Return (0 );
}
Printf ("argv [0]: % s \ n", argv [0]);
Docname = argv [1];
Printf ("argv [1]: % s \ n", argv [1]);
Keyword = argv [2];
Printf ("argv [2]: % s \ n", argv [2]);
Doc = parsedoc (docname, keyword );
If (Doc! = NULL)
{
Xmlsaveformatfile (docname, Doc, 0 );
Xmlfreedoc (DOC );
}
Return (1 );
}
To guide readers to compile more files using makefile, write a null. h file, as shown below:
# Include <time. h>
/* AAAA */
In fact, it is not a civil servant.
The content of makefile is as follows:
Libxml_inc = $ (shell PKG-config -- cflags libxml-2.0)
Cflags + = $ (libxml_inc)
Libxml_libs = $ (shell PKG-config -- libs libxml-2.0)
Libs + = $ (libxml_libs)
AAA: add_node.c null. h
$ (CC)-o aaa $ (cflags) $ (libs) $ <
You only need to pay a little attention to the introduction of PKG-config on the Internet. Here we use it to help youProgramIt is versatile and cannot only run in one acre or three minutes. After making is used, it outputs AAA. If you input:./AAA 1.xml suibianxieshenme, a node is added to 1.xml.
1. the XML format is as follows. I have run it several times, so it looks strange:
<? XML version = "1.0"?>
<Story>
<Storyinfo>
<Author> JOHN fleck </author>
<Datewritten> June 2, 2002 </datewritten>
<Keyword> example keyword </keyword>
<Keyword> menj </keyword> <keyword> mengjun </keyword> </storyinfo>
<Body>
<Headline> This is the headline <Para> This is the body text. </para>
</Body>
<Reference uri = "mengj" address = "mengj" Institute = "mengj"/> </story>
In addition, if you want to compile add_node.c by using the GCC command containing PKG-congfig at the beginning, you can use the following command:
Gcc-G add_node.c-O add_node 'pkg-config "libxml-2.0" -- cflags -- libs'