From 88f00ae133cf47694736b24fc7a2afd7c52bd247 Mon Sep 17 00:00:00 2001
From: Daniel Veillard
Latest versions can be found on The latest versions of libxml can be found on rpmfind.net or on the Gnome FTP server either as a source archive or RPMs -packages (NOTE that you need both the . (NOTE that you need both the libxml and libxml-devel -packages installed to compile applications using libxml).
+packages installed to compile applications using libxml.)Here is an example (erroneous w.r.t. the XML spec since there should be -only one ELEMENT under the root):
+Here is an example (erroneous with respect to the XML spec since there +should be only one ELEMENT under the root):
In the source package there is a small program (not installed by default) called tester which parses XML files given as argument and -prints them back as parsed. This is useful to detect errors both in XML code +prints them back as parsed. This is useful for detecting errors both in XML code and in the XML parser itself. It has an option --debug which prints the actual in-memory structure of the document, here is the result with the example given before:
@@ -383,27 +383,28 @@ standalone=true TEXT content=... -This should be useful to learn the internal representation model.
+This should be useful for learning the internal representation model.
Sometimes the DOM tree output is just too large to fit reasonably into -memory. In that case and if you don't expect to save back the XML document -loaded using libxml, it's better to use the SAX interface of libxml. SAX is a +memory. In that case (and if you don't expect to save back the XML document +loaded using libxml), it's better to use the SAX interface of libxml. SAX is a callback-based interface to the parser. Before parsing, the application layer registers a customized set of callbacks which are called by the library as it progresses through the XML input.
-To get a more detailed step-by-step guidance on using the SAX interface of -libxml, James Henstridge has written some nice -documentation.
+To get more detailed step-by-step guidance on using the SAX interface of +libxml, see the +href="http://www.daa.com.au/~james/gnome/xml-sax/xml-sax.html">nice +documentation. written by James +Henstridge.
You can debug the SAX behaviour by using the testSAX program located in the gnome-xml module (it's usually not shipped in the -binary packages of libxml, but you can also find it in the tar source +binary packages of libxml, but you can find it in the tar source distribution). Here is the sequence of callbacks that would be reported by -testSAX when parsing the example given before:
+testSAX when parsing the example XML document shown earlier:SAX.setDocumentLocator() SAX.startDocument() SAX.getEntity(amp) @@ -448,40 +449,40 @@ specific interface.The XML library interfaces
This section is directly intended to help programmers getting bootstrapped -using the XML library from the C language. It doesn't intent to be extensive, -I hope the automatically generated docs will provide the completeness -required, but as a separated set of documents. The interfaces of the XML -library are by principle low level, there is nearly zero abstration. Those +using the XML library from the C language. It is not intended to be extensive. +I hope the automatically generated documents will provide the completeness +required, but as a separate set of documents. The interfaces of the XML +library are by principle low level, there is nearly zero abstraction. Those interested in a higher level API should look at DOM.
The parser interfaces for XML are -separated from the HTML parser ones, -let's have a look at how it can be called:
+separated from the HTML parser +interfaces. Let's have a look at how the XML parser can be called: -Invoking the parser : the pull way
+Invoking the parser : the pull method
-Usually, the first thing to do is to read an XML input, the parser accepts -to parse both memory mapped documents or direct files. The functions are +
Usually, the first thing to do is to read an XML input. The parser accepts +documents either from in-memory strings or from files. The functions are defined in "parser.h":
xmlDocPtr xmlParseMemory(char *buffer, int size);
parse a zero terminated string containing the document
+Parse a null-terminated string containing the document.
xmlDocPtr xmlParseFile(const char *filename);
parse an XML document contained in a file (possibly compressed)
+Parse an XML document contained in a (possibly compressed) file.
This returns a pointer to the document structure (or NULL in case of +
The parser returns a pointer to the document structure (or NULL in case of failure).
-In order for the application to keep the control when the document is been -fetched (common for GUI based programs) the libxml, as of version 1.8.3 -provides a push interface too, here are the interfaces:
+fetched (which is common for GUI based programs) libxml provides a push +interface, too, as of version 1.8.3. Here are the interface functions:xmlParserCtxtPtr xmlCreatePushParserCtxt(xmlSAXHandlerPtr sax, void *user_data, const char *chunk, @@ -492,7 +493,7 @@ int xmlParseChunk (xmlParserCtxtPtr ctxt, int size, int terminate);-
and here is a simple use example:
+and here is a simple example showing how to use the interface:
FILE *f; f = fopen(filename, "r"); @@ -514,8 +515,8 @@ int xmlParseChunk (xmlParserCtxtPtr ctxt, } }-
Also note that the HTML parser embedded into libxml also have a push -interface they are just prefixed by "html" instead of "xml"
+Also note that the HTML parser embedded into libxml also has a push +interface; the functions are just prefixed by "html" rather than "xml"
xmlCreatePushParserCtxt()
.
The other way to get an XML tree in memory is by building it. Basically -there is a set of functions dedicated to building new elements, those are also -described in "tree.h", here is for example the piece of code producing the -example used before:
+there is a set of functions dedicated to building new elements. (These are also +described in "tree.h".) For example, here is a piece of code that produces the +XML document used in the previous examples:xmlDocPtr doc; xmlNodePtr tree, subtree; @@ -554,64 +555,66 @@ example used before:Traversing the tree
Basically by including "tree.h" your code -has access to the internal structure of all the element of the tree. The names +has access to the internal structure of all the elements of the tree. The names should be somewhat simple like parent, childs, next, prev, -properties, etc... For example still with the previous +properties, etc... For example, still with the previous example:
doc->root->childs->childs
points to the title element,
doc->root->childs->next->child->child-points to the text node containing the chapter titlle "The Linux adventure" -and
+points to the text node containing the chapter title "The Linux adventure". +
NOTE: XML allows PIs and comments to be -present before the document root, so doc->root may point to an element which -is not the document Root Element, a function +present before the document root, so
doc->root
may point to an +element which is not the document Root Element, a functionxmlDocGetRootElement()
was added for this purpose.Modifying the tree
-functions are provided to read and write the document content, here is an -excerpt from the tree API:
+Functions are provided for reading and writing the document content. Here +is an excerpt from the tree API:
xmlAttrPtr xmlSetProp(xmlNodePtr node, const xmlChar *name, const
xmlChar *value);
This set (or change) an attribute carried by an ELEMENT node the - value can be NULL
+This sets (or changes) an attribute carried by an ELEMENT node. The + value can be NULL.
const xmlChar *xmlGetProp(xmlNodePtr node, const xmlChar
*name);
This function returns a pointer to the property content, note that - no extra copy is made
+This function returns a pointer to the property content. Note that + no extra copy is made.
Two functions must be used to read an write the text associated to +
Two functions are provided for reading and writing the text associated with elements:
xmlNodePtr xmlStringGetNodeList(xmlDocPtr doc, const xmlChar
*value);
This function takes an "external" string and convert it to one text node or possibly to a list of entity and text nodes. All non-predefined - entity references like &Gnome; will be stored internally as an - entity node, hence the result of the function may not be a single + entity references like &Gnome; will be stored internally as + entity nodes, hence the result of the function may not be a single node.
xmlChar *xmlNodeListGetString(xmlDocPtr doc, xmlNodePtr list, int
inLine);
this is the dual function, which generate a new string containing +
This function is the inverse of xmlStringGetNodeList()
.
+ It generates a new string containing
the content of the text and entity nodes. Note the extra argument
- inLine, if set to 1 instead of returning the &Gnome; XML encoding in
- the string it will substitute it with it's value say "GNU Network Object
- Model Environment". Set it if you want to use the string for non XML
- usage like User Interface.
void xmlDocDumpMemory(xmlDocPtr cur, xmlChar**mem, int
*size);
returns a buffer where the document has been saved
+Returns a buffer into which the document has been saved.
extern void xmlDocDump(FILE *f, xmlDocPtr doc);
dumps a buffer to an open file descriptor
+Dumps a document to an open file descriptor.
int xmlSaveFile(const char *filename, xmlDocPtr cur);
save the document ot a file. In that case the compression interface - is triggered if turned on
+Saves the document to a file. In this case, the compression interface + is triggered if it has been turned on.
The library handle transparently compression when doing file based -accesses, the level of compression on saves can be tuned either globally or +
The library transparently handles compression when doing file-based +accesses. The level of compression on saves can be turned on either globally or individually for one file:
int xmlGetDocCompressMode (xmlDocPtr doc);
Get the document compression ratio (0-9)
+Gets the document compression ratio (0-9).
void xmlSetDocCompressMode (xmlDocPtr doc, int mode);
Set the document compression ratio
+Sets the document compression ratio.
int xmlGetCompressMode(void);
Get the default compression ratio
+Gets the default compression ratio.
void xmlSetCompressMode(int mode);
set the default compression ratio
+Sets the default compression ratio.
Entities principle is similar to simple C macros. They define an -abbreviation for a given string that you can reuse many time through the -content of your document. They are especially useful when frequent occurrences -of a given string may occur within a document or to confine the change needed +
Entities in principle are similar to simple C macros. An entity defines an +abbreviation for a given string that you can reuse many times throughout the +content of your document. Entities are especially useful when a given string +may occur frequently within a document, or to confine the change needed to a document to a restricted area in the internal subset of the document (at the beginning). Example:
1 <?xml version="1.0"?> @@ -680,23 +683,24 @@ the beginning). Example:-Line 3 declares the xml entity. Line 6 uses the xml entity, by prefixing it's name with '&' and following it by ';' without any spaces added. There -are 5 predefined entities in libxml allowing to escape charaters with +are 5 predefined entities in libxml allowing you to escape charaters with predefined meaning in some parts of the xml document content: -< for the letter '<', > for -the letter '>', ' for the letter ''', -" for the letter '"', and & -for the letter '&'.
+< for the character '<', > +for the character '>', ' for the character ''', +" for the character '"', and +& for the character '&'.One of the problems related to entities is that you may want the parser to -substitute entities content to see the replacement text in your application, -or you may prefer keeping entities references as such in the content to be -able to save the document back without loosing this usually precious -information (if the user went through the pain of explicitley defining +substitute an entity's content so that you can see the replacement text in your +application. +Or you may prefer to keep entity references as such in the content to be +able to save the document back without losing this usually precious +information (if the user went through the pain of explicitly defining entities, he may have a a rather negative attitude if you blindly susbtitute -them as saving time). The function xmlSubstituteEntitiesDefault() -allows to check and change the behaviour, which is to not substitute entities -by default.
+function allows you to check and change the behaviour, which is to not +substitute entities by default.Here is the DOM tree built by libxml for the previous document in the default case:
@@ -720,46 +724,46 @@ version=1.0 TEXT content= Extensible Markup Language
So entities or no entities ? Basically it depends on your use case, I -suggest to keep the non-substituting default behaviour and avoid using +
So, entities or no entities? Basically, it depends on your use case. I +suggest that you keep the non-substituting default behaviour and avoid using entities in your XML document or data if you are not willing to handle the entity references elements in the DOM tree.
Note that at save time libxml enforce the conversion of the predefined entities where necessary to prevent well-formedness problems, and will also -transparently replace those with chars (i.e. will not generate entity -reference elements in the DOM tree nor call the reference() SAX callback when +transparently replace those with chars (i.e., it will not generate entity +reference elements in the DOM tree or call the reference() SAX callback when finding them in the input).
The libxml library implement namespace @@ support by recognizing namespace +
The libxml library implements namespace @@ support by recognizing namespace contructs in the input, and does namespace lookup automatically when building the DOM tree. A namespace declaration is associated with an in-memory structure and all elements or attributes within that namespace point to it. Hence testing the namespace is a simple and fast equality operation at the user level.
-I suggest it that people using libxml use a namespace, and declare it on -the root element of their document as the default namespace. Then they dont -need to happend the prefix in the content but we will have a basis for future +
I suggest that people using libxml use a namespace, and declare it in +the root element of their document as the default namespace. Then they don't +need to use the prefix in the content but we will have a basis for future semantic refinement and merging of data from different sources. This doesn't augment significantly the size of the XML output, but significantly increase -it's value in the long-term.
+its value in the long-term. -Concerning the namespace value, this has to be an URL, but this doesn't -have to point to any existing resource on the Web. I suggest using an URL -within a domain you control, which makes sense and if possible holding some -kind of versionning informations. For example +
Concerning the namespace value, this has to be an URL, but the URL doesn't
+have to point to any existing resource on the Web. I suggest that it makes
+sense to use an URL within a domain you control, and that the URL
+should contain some kind of version information if possible. For example,
"http://www.gnome.org/gnumeric/1.0"
is a good namespace scheme.
Then when you load a file, make sure that a namespace carrying the
-version-independant prefix is installed on the root element of your document,
+version-independent prefix is installed on the root element of your document,
and if the version information don't match something you know, warn the user
and be liberal in what you accept as the input. Also do *not* try to base
-namespace checking on the prefix value <foo:text> may be exactly the same
-as <bar:text> in another document, what really matter is the URI
-associated with the element or the attribute, not the prefix string which is
-just a shortcut for the full URI.
@@Interfaces@@
@@ -767,11 +771,11 @@ just a shortcut for the full URI.Usually people object using namespace in the case of validation, I object
this and will make sure that using namespaces won't break validity checking,
-so even is you plan or are using validation I strongly suggest to add
-namespaces to your document. A default namespace scheme
+so even is you plan to use or currently are using validation I strongly suggest
+adding namespaces to your document. A default namespace scheme
xmlns="http://...."
should not break validity even on less
-flexible parsers. Now using namespace to mix and differenciate content coming
-from mutliple Dtd will certainly break current validation schemes, I will try
+flexible parsers. Now using namespace to mix and differentiate content coming
+from multiple DTDs will certainly break current validation schemes. I will try
to provide ways to do this, but this may not be portable or standardized.
The suggested principle is to be tolerant with respect to the input -structure. For example the ordering of the attributes is not significant, Cthe +structure. For example, the ordering of the attributes is not significant, Cthe XML specification is clear about it. It's also usually a good idea to not be -dependant of the orders of the childs of a given node, unless it really makes +dependent of the orders of the childs of a given node, unless it really makes things harder. Here is some code to parse the informations for a person:
/* * A person record @@ -1043,6 +1047,6 @@ base under gnome-xml/example -$Id: xml.html,v 1.25 2000/02/29 22:57:47 veillard Exp $
+$Id: xml.html,v 1.26 2000/03/01 00:40:41 veillard Exp $