利用xml库解析xml解决xml文件生成没有空格和间距的问题

安装xml库

sudo apt-get install libxml2
sudo apt-get install libxml2-dev
ln -s /usr/include/libxml2/libxml   /usr/include/libxml

生成xml文件

creatparaxml.c


#include <stdio.h>

#include <libxml/parser.h>

#include <libxml/tree.h>
#include <stdio.h>

int main()
{

    //定义文档和节点指针

    xmlDocPtr doc = xmlNewDoc(BAD_CAST"1.0");

    xmlNodePtr root_node = xmlNewNode(NULL,BAD_CAST"root");

    //设置根节点

    xmlDocSetRootElement(doc,root_node);

    //在根节点中直接创建节点

    xmlNewTextChild(root_node, NULL, BAD_CAST "newNode1", BAD_CAST "newNode1 content");

    xmlNewTextChild(root_node, NULL, BAD_CAST "newNode2", BAD_CAST "newNode2 content");

    xmlNewTextChild(root_node, NULL, BAD_CAST "newNode3", BAD_CAST "newNode3 content");

    //创建一个节点,设置其内容和属性,然后加入根结点

    xmlNodePtr node = xmlNewNode(NULL,BAD_CAST"node2");

    xmlNodePtr content = xmlNewText(BAD_CAST"NODE CONTENT");

    xmlAddChild(root_node,node);

    xmlAddChild(node,content);

    xmlNewProp(node,BAD_CAST"attribute",BAD_CAST "yes");

    //创建一个儿子和孙子节点

    node = xmlNewNode(NULL, BAD_CAST "son");

    xmlAddChild(root_node,node);

    xmlNodePtr grandson = xmlNewNode(NULL, BAD_CAST "grandson");

    xmlAddChild(node,grandson);                                                                                                                                                                                           
                                                                                                                                                                                                                          
    xmlAddChild(grandson, xmlNewText(BAD_CAST "This is a grandson node"));                                                                                                                                                
                                                                                                                                                                                                                          
    //存储xml文档                                                                                                                                                                                                         
                                                                                                                                                                                                                          
    int nRel = xmlSaveFormatFileEnc("CreatedXml.xml",doc, "UTF-8", 1);                                                                                                                                                    
                                                                                                                                                                                                                          
    if (nRel != -1)                                                                                                                                                                                                                                                                                                                                                                                                                                 
    {                                                                                                                                                                                                                     
                                                                                                                                                                                                                          
       printf("write successful!\n");                                                                                                                                                                                     
                                                                                                                                                                                                                          
    }                                                                                                                                                                                                                     
                
   //释放文档内节点动态申请的内存                                                                                                                                                                                        
                                                                                                                                                                                                                          
    xmlFreeDoc(doc);                                                                                                                                                                                                      
                                                                                                                                                                                                                          
    return 0;                                                                                                                                                                                                             
                                                                                                                                                                                                                          
}  

解析xml  paramxml.c

#include <libxml/parser.h>
#include <string.h>
#include <stdio.h>

int main(int argc, char* argv[])
{

    xmlDocPtr doc;           //定义解析文档指针

    xmlNodePtr curNode;      //定义结点指针(你需要它为了在各个结点间移动)

    xmlChar *szKey;          //临时字符串变量

    char *szDocName;

    if (argc <= 1)
    {

       printf("Usage: %s docname\n", argv[0]);

       return(0);
    }

    szDocName = argv[1];

    doc = xmlReadFile(szDocName, "UTF-8", XML_PARSE_NOBLANKS); //解析文件

    //检查解析文档是否成功,如果不成功,libxml将指一个注册的错误并停止。

    //一个常见错误是不适当的编码。XML标准文档除了用UTF-8或UTF-16外还可用其它编码保存。

    //如果文档是这样,libxml将自动地为你转换到UTF-8。更多关于XML编码信息包含在XML标准中.

    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;                                                                                                                                                                                                         
                                                                                                                                                                                                                          
    }       
/*在这个例子中,我们需要确认文档是正确的类型。“root”是在这个示例中使用文档的根类型。*/                                                                                                                                
                                                                                                                                                                                                                          
    if (xmlStrcmp(curNode->name, BAD_CAST "root"))                                                                                                                                                                                                                                                                                                                                                                                         
    {                                                                                                                                                                                                                     
                                                                                                                                                                                                                          
       fprintf(stderr,"document of the wrong type, root node != root");                                                                                                                                                   
                                                                                                                                                                                                                          
       xmlFreeDoc(doc);                                                                                                                                                                                                   
                                                                                                                                                                                                                          
       return -1;                                                                                                                                                                                                         
                                                                                                                                                                                                                          
    }                                                                                                                                                                                                                     
                                                                                                                                                                                                                          
    curNode = curNode->xmlChildrenNode;                                                                                                                                                                                   
                                                                                                                                                                                                                          
    xmlNodePtr propNodePtr = curNode;                                                                                                                                                                                     
                                                                                                                                                                                                                          
    while(curNode != NULL)                                                                                                                                                                                                                                                                                                                                                                                                                 
    {                                                                                                                                                                                                                     
                                                                                                                                                                                                                          
       //取出节点中的内容                                                                                                                                                                                                 
                                                                                                                                                                                                                          
       if ((!xmlStrcmp(curNode->name, (const xmlChar *)"newNode1")))                                                                                                                                                                                                                                                                                                                                                                       
       {                                                                                                                                                                                                                  
                                                                                                                                                                                                                          
           szKey = xmlNodeGetContent(curNode);                                                                                                                                                                            
                                                                                                                                                                                                                          
           printf("newNode1: %s\n", szKey);                                                                                                                                                                               
                                                                                                                                                                                                                          
           xmlFree(szKey);                                                                                                                                                                                                
                                                                                                                                                                                                                          
       }                                                                                                                                                                                                                  
                                                                                                                                                                                                                          
       //查找带有属性attribute的节点                                                                                                                                                                                      
                                                                                                                                                                                                                          
       if (xmlHasProp(curNode,BAD_CAST "attribute"))                                                                                                                                                                                                                                                                                                                                                                                     
       {                                                                                                                                                                                                                  
                                                                                                                                                                                                                          
           propNodePtr = curNode;                                                                                                                                                                                         
                                                                                                                                                                                                                          
       }                                                                                                                                                                                                                  
                                                                                                                                                                                                                          
       curNode = curNode->next;                                                                                                                                                                                           
                                                                                                                                                                                                                          
    }                                                                                                                                                                                                                     
                                                                                                                                                                                                                          
    //查找属性                                                                                                                                                                                                            
                                                                                                                                                                                                                          
    xmlAttrPtr attrPtr = propNodePtr->properties;                                                                                                                                                                         
                                                                                                                                                                                                                          
    while (attrPtr != NULL)                                                                                                                                                                                                                                                                                                                                                                                                                 
    {                                                                                                                                                                                                                     
                                                                                                                                                                                                                          
       if (!xmlStrcmp(attrPtr->name, BAD_CAST "attribute"))                                                                                                                                                                                                                                                                                                                                                                              
       {                                                                                                                                                                                                                  
                                                                                                                                                                                                                          
           xmlChar* szAttr = xmlGetProp(propNodePtr,BAD_CAST "attribute");                                                                                                                                                
                                                                                                                                                                                                                          
           printf("get attribute = %s\n",szAttr);                                                                                                                                                                         
                                                                                                                                                                                                                          
           xmlFree(szAttr);                                                                                                                                                                                               
                                                                                                                                                                                                                          
       }                                                                                                                                                                                                                  
                                                                                                                                                                                                                          
       attrPtr = attrPtr->next;                                                                                                                                                                                           
                                                                                                                                                                                                                          
    }                                                                                                                                                                                                                     
                                                                                                                                                                                                                          
    xmlFreeDoc(doc);                                                                                                                                                                                                      
                                                                                                                                                                                                                          
    return 0;                                                                                                                                                                                                             
                                                                                                                                                                                                                          
}  

makefile

all:
        gcc paramxml.c -o paramxml -lxml2 
        gcc creatparaxml.c -o creatparaxml -lxml2 
clean:
        rm -rf paramxml creatparaxml CreatedXml.xml

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值