Home Database Mysql Tutorial libxml2实现xml文档的节点的修改

libxml2实现xml文档的节点的修改

Jun 07, 2016 pm 03:00 PM
Sky xml Revise accomplish document node

其实在网上很容易能找到使用libxml2来对xml文档进行创建、解析、修改等。我着这里主要是就自己学习的libxml2修改xml文档的节点进行一个简单的总结,方便自己以后回顾。 下面给出我写的一个例子: /*********************************************************

 其实在网上很容易能找到使用libxml2来对xml文档进行创建、解析、修改等。我着这里主要是就自己学习的libxml2修改xml文档的节点进行一个简单的总结,方便自己以后回顾。

 

下面给出我写的一个例子:

/********************************************************************** 

                Copyright, 2011, **** Tech. Co., Ltd. 

                            All Rights Reserved 

----------------------------------------------------------------------- 

Project Code   : wlan 

File name      : modify_node.cpp 

Author         : Sky_qing 

Description    : 使用libxml2修改xml文档的节点

Function List: 

----------------------------------------------------------------------- 

History:  

Date            Author                 Modification 

2011-12-27     Sky_qing                created file

**********************************************************************/
#include <stdio.h>
#include "libxml/parser.h"
#include "libxml/tree.h"

int main(int argc, char* argv[])
{
	xmlDocPtr doc;				//定义解析文档指针
	xmlNodePtr curNode;			//定义节点指针(在各个节点之间移动)
	char* szDocName = argv[1];	//保存xml文档名,该文档名在运行程序到时候输入。
	//例如:编译格式为g++ modify_node.cpp -o modify_node -I /usr/local/include/libxml2/  -L /usr/local/lib -lxml2,生成可执行文件modify_node,运行时:./modify_node log4crc(此处log4crc为要修改的xml文档)

	printf("........start........\n");
    doc = xmlReadFile(szDocName, "utf-8", XML_PARSE_RECOVER); 	//解析文档
	if (NULL == doc)
	{
		fprintf(stderr, "Document not parsed successfully.\n");
		
		return -1;
	}

	curNode = xmlDocGetRootElement(doc);		//确定文档根元素
	if (NULL == curNode)
	{
		fprintf(stderr, "Empty Document.\n");
		xmlFreeDoc(doc);		//释放文件

		return -1;
	}

	if (xmlStrcmp(curNode->name, (const xmlChar*)"log4c"))	//确认根元素是否为“log4c”
	{
		fprintf(stderr, "Document of wrong type. root node != log4c");
		xmlFreeDoc(doc);

		return -1;
	}

	curNode = curNode->xmlChildrenNode;
	xmlNodePtr propNode = curNode;
	while (NULL != curNode)		//遍历所有节点
	{
		//获取名称为category的节点
		if (!xmlStrcmp(curNode->name, (const xmlChar*)"category"))
		{
			//查找带有属性name的节点
      		if (xmlHasProp(curNode, BAD_CAST "name"))
       		{
                propNode = curNode;
     		}

			//查找属性name为WLAN_Console的节点
    		xmlAttrPtr attrPtr = propNode->properties;
			while (NULL != attrPtr)		//遍历所有名称为category的节点
    		{
		   		if (!xmlStrcmp(attrPtr->name, (const xmlChar*)"name"))	//找到有name属性到节点
		   		{
					//查找属性为name的值的节点
					xmlChar* szPropity = xmlGetProp(propNode, (const xmlChar*)"name");
					if (!xmlStrcmp((const xmlChar*)szPropity, (const xmlChar*)"WLAN_Console"))
					{
						xmlAttrPtr setAttrPtr = propNode->properties;
						while (NULL != setAttrPtr)
						{
							//设置属性priority的值
							xmlSetProp(propNode, (const xmlChar*)"priority", (const xmlChar*)"debug");
			
							setAttrPtr = setAttrPtr->next;
						}
					}
		  		}
		   		attrPtr = attrPtr->next;
	   		}	
		}
		curNode = curNode->next;
	}
	
	//保存文档到原文档中
	xmlSaveFile("log4crc", doc);

	printf("...........OK............\n");

	return 0;
}</stdio.h>
Copy after login


编译和运行格式在代码中有。

 

参考文章:

http://blog.sina.com.cn/s/blog_669e9f6a0100sbql.html

http://wenku.baidu.com/view/02d0fdea172ded630b1cb61c.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk Mar 29, 2024 pm 08:41 PM

1. First open DingTalk. 2. Open the group chat and click the three dots in the upper right corner. 3. Find my nickname in this group. 4. Click to enter to modify and save.

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

Can Douyin Blue V change its name? What are the steps to change the name of corporate Douyin Blue V account? Can Douyin Blue V change its name? What are the steps to change the name of corporate Douyin Blue V account? Mar 22, 2024 pm 12:51 PM

Douyin Blue V certification is the official certification of a company or brand on the Douyin platform, which helps enhance brand image and credibility. With the adjustment of corporate development strategy or the update of brand image, the company may want to change the name of Douyin Blue V certification. So, can Douyin Blue V change its name? The answer is yes. This article will introduce in detail the steps to modify the name of the enterprise Douyin Blue V account. 1. Can Douyin Blue V change its name? You can change the name of Douyin Blue V account. According to Douyin’s official regulations, corporate Blue V certified accounts can apply to change their account names after meeting certain conditions. Generally speaking, enterprises need to provide relevant supporting materials, such as business licenses, organization code certificates, etc., to prove the legality and necessity of changing the name. 2. What are the steps to modify the name of corporate Douyin Blue V account?

How to modify the address location of published products on Xianyu How to modify the address location of published products on Xianyu Mar 28, 2024 pm 03:36 PM

When publishing products on the Xianyu platform, users can customize the geographical location information of the product according to the actual situation, so that potential buyers can more accurately grasp the specific location of the product. Once the product is successfully put on the shelves, there is no need to worry if the seller's location changes. The Xianyu platform provides a flexible and convenient modification function. So when we want to modify the address of a published product, how do we modify it? This tutorial guide will provide you with a detailed step-by-step guide. I hope it can help. Everyone! How to modify the release product address in Xianyu? 1. Open Xianyu, click on what I published, select the product, and click Edit. 2. Click the positioning icon and select the address you want to set.

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Detailed explanation of Word document operation: merge two pages into one Detailed explanation of Word document operation: merge two pages into one Mar 26, 2024 am 08:18 AM

Word documents are one of the most frequently used applications in our daily work and study. When working with documents, you may sometimes encounter a situation where you need to merge two pages into one. This article will introduce in detail how to merge two pages into one page in a Word document to help readers handle document layout more efficiently. In Word documents, the operation of merging two pages into one is usually used to save paper and printing costs, or to make the document more compact and neat. The following are the specific steps to merge two pages into one: Step 1: Open the Word that needs to be operated

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

See all articles