xml文件的创建

前端之家收集整理的这篇文章主要介绍了xml文件的创建前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

include

include

include

include

这里写代码

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,BAD_CAST “newNode2”,BAD_CAST “newNode2 content”);
  xmlNewTextChild(root_node,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 = xmlSaveFile(“Create.xml”,doc);
  if (nRel != -1) {

    printf(“一个xml文档被创建,写入%d个字节。\n”,nRel);
  }

  //释放文档内节点动态申请的内存
  xmlFreeDoc(doc);

  return 0; }

猜你在找的XML相关文章