用C语言读取xml文件,怎么实现?

2025-02-25 05:54:05
推荐回答(4个)
回答1:

xml文件和txt文件相同,使用普通的文本操作函数即可读取。

1、C语言标准库提供了一系列文件操作函数。文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。
2、例程:

#include
int a;
char b,c[100];
int main(){
    FILE * fp1 = fopen("input.xml", "r");//打开xml格式输入文件
    FILE * fp2 = fopen("output.txt", "w");//打开输出文件
    if (fp1==NULL || fp2==NULL) {//若打开文件失败则退出
        puts("不能打开文件!");
        rturn 0;
    }
    fscanf(fp1,"%d",&a);//从输入文件读取一个整数
    b=fgetc(fp1);//从输入文件读取一个字符
    fgets(c,100,fp1);//从输入文件读取一行字符串
    
    printf("%ld",ftell(fp1));//输出fp1指针当前位置相对于文件首的偏移字节数
    
    fputs(c,fp2);//向输出文件写入一行字符串
    fputc(b,fp2);//向输出文件写入一个字符
    fprintf(fp2,"%d",a);//向输出文件写入一个整数
    
    fclose(fp1);//关闭输入文件
    fclose(fp2);//关闭输出文件,相当于保存
    return 0;
}

回答2:

我上次才给人写过 
xml文件内容

 



7 
0 
1 
93 
阿富汗 
Afghanistan 
阿富汗 
アフガニスタン 
?????? 
 
AFuHan 
0 
1 


8 
0 
2 
355 
阿尔巴尼亚 
Albania 
阿尔巴尼亚 
アルバニア 
 
 
AErBaNiYa 
0 
1 




运行结果

Info[0]=[id:7|pid:0|continent_id:1|guohao:93|cntitle:阿富汗|entitle:Afghanistan|
hztitle:阿富汗|jptitle:アフガニスタン|kotitle:??????|jp_pinyin:ア|pinyin:AFuHan|
sid:0|jibie:1|]
Info[1]=[id:7|pid:0|continent_id:1|guohao:93|cntitle:阿富汗|entitle:Afghanistan|
hztitle:阿富汗|jptitle:アフガニスタン|kotitle:??????|jp_pinyin:ア|pinyin:AFuHan|
sid:0|jibie:1|]
Press any key to continue

代码

#include 
#include 
main()
{
 int i=0;
 FILE *fp;
 char szFileBuff[1024] = {0}, szBuff[100][1024];
 char id[10] = {0}, pid[10] = {0}, continent_id[10] = {0}, guohao[10] = {0},
  cntitle[64]= {0},entitle[64]= {0},hztitle[64] = {0},jptitle[64] = {0}, 
  kotitle[64] = {0},jp_pinyin[64] = {0}, pinyin[64] = {0},sid[10] = {0},jibie[10] = {0};
 char *lFirst, *lEnd;
 
 fp = fopen("country.txt","r");
 if (fp==NULL)
 {
  printf("read XML file error!\n");
 }
 while(fgets(szFileBuff, 1023, fp))
 {
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(id, lFirst + 4, lEnd - lFirst - 4);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(pid, lFirst + 5, lEnd - lFirst - 5);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(continent_id, lFirst + 14, lEnd - lFirst - 14);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(guohao, lFirst + 8, lEnd - lFirst - 8);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(cntitle, lFirst + 9, lEnd - lFirst - 9);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(entitle, lFirst + 9, lEnd - lFirst - 9);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(hztitle, lFirst + 9, lEnd - lFirst - 9);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(jptitle, lFirst + 9, lEnd - lFirst - 9);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(kotitle, lFirst + 9, lEnd - lFirst - 9);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(jp_pinyin, lFirst + 11, lEnd - lFirst - 11);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(pinyin, lFirst + 8, lEnd - lFirst - 8);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(sid, lFirst + 5, lEnd - lFirst - 5);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   lEnd = strstr(lFirst + 1, "
");
   memcpy(jibie, lFirst + 7, lEnd - lFirst - 7);
  }
  if ((lFirst = strstr(szFileBuff, "")) != NULL)
  {
   sprintf(szBuff[i],"id:%s|pid:%s|continent_id:%s|guohao:%s|cntitle:%s|entitle:%s|hztitle:%s|jptitle:%s|kotitle:%s|jp_pinyin:%s|pinyin:%s|sid:%s|jibie:%s|",
   id,pid,continent_id,guohao,cntitle,entitle,hztitle,jptitle,kotitle,jp_pinyin, pinyin,sid,jibie);
   printf("Info[%d]=[%s]\n",i++, szBuff);
  }
 }
 fclose(fp);
}

回答3:

用System.xml命名空间

回答4:

/**/
///


/// XML 操作基类
///

public class XmlHelper
{
/**/
///
/// 读取Xml到DataSet中

///

/// 路径
/// 结果集
public static DataSet GetXml(string XmlPath)
{
DataSet ds = new DataSet();
ds.ReadXml(@XmlPath);
return ds;
}

/**/
///
/// 读取xml文档并返回一个节点:适用于一级节点

///

/// xml路径
/// 节点
///
public static string ReadXmlReturnNode(string XmlPath, string Node)
{
XmlDocument docXml = new XmlDocument();

docXml.Load(@XmlPath);
XmlNodeList xn = docXml.GetElementsByTagName(Node);
return xn.Item(0).InnerText.ToString();
}

/**/
///
/// 查找数据,返回当前节点的所有下级节点,填充到一个DataSet中

///

/// xml文档路径
/// 节点的路径:根节点/父节点/当前节点
///
public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds;
}

/**/
///
/// 更新Xml节点内容
///

/// xml路径
/// 要更换内容的节点:节点路径 根节点/父节点/当前节点
/// 新的内容
public static void XmlNodeReplace(string xmlPath, string Node, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
objXmlDoc.SelectSingleNode(Node).InnerText = Content;
objXmlDoc.Save(xmlPath);

}

/**/
///
/// 更改节点的属性值

///

/// 文件路径
/// 节点路径
/// 要更改的节点属性的名称
/// 更改的属性值
public static void XmlAttributeEdit(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}

/**/
///
/// 删除XML节点和此节点下的子节点

///

/// xml文档路径
/// 节点路径
public static void XmlNodeDelete(string xmlPath, string Node)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
objXmlDoc.Save(xmlPath);
}

/**/
///
/// 删除一个节点的属性

///

/// 文件路径
/// 节点路径(xpath)
/// 属性名称
public static void xmlnNodeAttributeDel(string xmlPath, string NodePath, string NodeAttribute)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
XmlElement xe = (XmlElement)nodePath;
xe.RemoveAttribute(NodeAttribute);
objXmlDoc.Save(xmlPath);
}

/**/
///
/// 插入一个节点和此节点的子节点

///

/// xml路径
/// 当前节点路径
/// 新插入节点
/// 插入节点的子节点
/// 子节点的内容
public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}

/**/
///
/// 向一个节点添加属性

///

/// xml文件路径
/// 节点路径
/// 要添加的节点属性的名称
/// 要添加属性的值
public static void AddAttribute(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlAttribute nodeAttribute = objXmlDoc.CreateAttribute(NodeAttribute1);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
nodePath.Attributes.Append(nodeAttribute);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}

/**/
///
/// 插入一节点,带一属性

///

/// Xml文档路径
/// 当前节点路径
/// 新节点
/// 属性名称
/// 属性值
/// 新节点值
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib, AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}

/**/
///
/// 插入一节点,不带属性

///

/// Xml文档路径
/// 当前节点路径
/// 新节点
/// 新节点值
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}

/**/
///
/// 在根节点下添加父节点
///

public static void AddParentNode(string xmlPath, string parentNode)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
// 创建一个新的menber节点并将它添加到根节点下
XmlElement Node = xdoc.CreateElement(parentNode);
xdoc.DocumentElement.PrependChild(Node);
xdoc.Save(xmlPath);
}

/**/
///
/// 根据节点属性读取子节点值(较省资源模式)
///

/// xml路径
/// 父节点值
/// 属性名称
/// 属性值
/// 返回的数组长度
///
public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
{
System.Collections.ArrayList al = new System.Collections.ArrayList();
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn;
xn = docXml.DocumentElement.SelectNodes("//" + FatherElement + "[" + @AttributeName + "='" + AttributeValue + "']");
XmlNodeList xx = xn.Item(0).ChildNodes;
for (int i = 0; i < ArrayLength & i < xx.Count; i++)
{

al.Add(xx.Item(i).InnerText);
}

return al;
}