Libxml Tutorials (text) _c language

Source: Internet
Author: User
Tags win32

Origin
My cloudbox needs a cross-platform solution to the XML file read problem on iOS and Android
Because the game always needs to store a set of values, or a clearance file or something.
But it's not easy to use the built-in functions provided by iOS or Java built in, so it's going to be a hassle.
and thinking about adding new platforms later, there will be no small trouble, the final decision on the use of Libxml
Libxml can also be used in Windows environments, so you can easily use Visual Studio to learn how the API uses
Introduction to the Environment
operating system:
Windows XP
IDE Tools: Visual Studio 2008
Project Type: Visual C + + Win32 Console application
Download link
Lib and. h file Download link
http://xmlsoft.org/sources/win32/
DLL download link
Http://www.dll-files.com/dllindex/dll-files.shtml?iconv
Http://www.dll-files.com/dllindex/dll-files.shtml?zlib1
Http://www.dll-files.com/dllindex/dll-files.shtml?libxml2
Setting Instructions diagram

As shown, I created two folders in the Engineering directory and put Iconv and libxml in the libxml. h Documents
Also created Lib file, put all lib in

Then Project project to set Libaray file path with include file paths
Code description
1. Reading XML documents

Copy Code code as follows:

Xmldocptr Doc;
Xmlnodeptr Root;
Load an exist XML file.
doc = Xmlparsefile ("Test.xml");
if (doc = = NULL)
{
fprintf (stderr, "Document not parsed successfully. \ n");
return 0;
}
Get root
root = Xmldocgetrootelement (doc);
if (root = NULL)
{
fprintf (stderr, "empty document\n");
Xmlfreedoc (DOC);
return 0;
}

XmlDoc is a struct that holds information about XML, such as file names, document types, child nodes, and so on; Xmldocptr equals xmldoc*, and it makes this look like it's always a smart pointer, not actually, to be deleted manually.
The Xmlparsefile function reads a document in a UTF-8 format by default and returns the document pointer.
The Xmlreadfile function reads an XML document with some encoding and returns the document pointer; see the LIBXML2 Reference manual for details.
Xmlfreedoc release the document pointer.
The Xmldocgetrootelement function gets the root node Curnode
2. Create an XML document
Copy Code code as follows:

void Createnewxmldemo ()
{
Create XML document
Xmldocptr doc = Xmlnewdoc (bad_cast "1.0");
Xmlnodeptr root = xmlNewNode (null,bad_cast "root");

Set root
Xmldocsetrootelement (Doc,root);

Add node
Xmlnewtextchild (Root, NULL, bad_cast "NewNode1", bad_cast "newNode1 content");
Xmlnewtextchild (Root, NULL, bad_cast "NewNode2", bad_cast "NewNode2 content");
Xmlnewtextchild (Root, NULL, bad_cast "NewNode3", bad_cast "newNode3 content");

Create node and add content

Xmlnodeptr node = xmlNewNode (null,bad_cast "Node2");
Xmlnodeptr content = Xmlnewtext (bad_cast "NODE content");
Xmladdchild (Root,node);
Xmladdchild (node,content);

Add attribute
Xmlnewprop (Node,bad_cast "attribute", Bad_cast "yes");

Create son and grandson

node = xmlNewNode (NULL, bad_cast "Son");
Xmladdchild (Root,node);
Xmlnewtextchild (node, NULL, Bad_cast "grandson", Bad_cast "grandson Content");

Xmlnodeptr grandson = xmlNewNode (NULL, Bad_cast "Grandson2");
Xmladdchild (Node,grandson);
Xmladdchild (grandson, Xmlnewtext (Bad_cast "This is a Grandson2 node"));

Save XML

int nRel = Xmlsavefile ("Test3.xml", Doc);

if (NRel!=-1)
{
cout<< "Create a xml:" <<nRel<< "bytes" <<endl;
}

Release

Xmlfreedoc (DOC);
}



Xmlchar is the character type in LIBXML2, and all characters and strings in the library are based on this data type. In fact, it is defined as: xmlstring.h
The Xmlnewdoc function creates a new document pointer.
xmlNewNode can create a new node
Xmldocsetrootelement can set this node as the root node
Xmlnewtextchild Add a text child node directly
The second way to create a node is to create a new node and then add the new node to the upper node with Xmladdchild.
Xmlnewprop can create a node's properties
Xmlsavefile can archive XML
Because there is always a type conversion between xmlchar* and char*, a macro bad_cast is defined, which is defined as follows: Xmlstring.h
#define BAD_CAST (Xmlchar *)
3. Traverse nodes, modify and delete
Copy Code code as follows:

Xmlnodeptr head = root->children->next;
while (head!= NULL)
{
if (Head->type = = Xml_element_node)
{
cout<< "Name:" <cout<< "Content:" <<xmlnodegetcontent (Head->children) <<endl;
Update test05
if ((!XMLSTRCMP (Head->name, (const XMLCHAR *) "test05"))
{
Xmlnodesetcontent (Head->children, (const XMLCHAR *) "orz05");
}

Remove node
if (!xmlstrcmp (Head->name, Bad_cast "test07"))
{
Xmlnodeptr Tempnode;
Tempnode = head->next;
Xmlunlinknode (head);
Xmlfreenode (head);
head = Tempnode;
Continue
}
}
Head = head->next;
}

In Libxml, you only need to search for Xml_element_node-type nodes
xmlnodegetcontentCan get the inner text of the node
xmlnodesetcontentYou can set the text of a node
You can compare the xmlstrcmp to the node you are looking for, the Xmlnodeptr Name property is the node, and children is the text node
Xmlunlinknode can delete nodes


Click to download the complete project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.