libxml2实现xml文档的节点的修改
其实在网上很容易能找到使用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>
编译和运行格式在代码中有。
参考文章:
http://blog.sina.com.cn/s/blog_669e9f6a0100sbql.html
http://wenku.baidu.com/view/02d0fdea172ded630b1cb61c.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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? 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

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?

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 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.

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

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

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.
