.C文件
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 600
int main()
{
FILE *fp;
char a[MAX] = { '0' };
int i=0,j=0,k=0;
fp = fopen("book.xml", "r");
if (fp ==NULL)
{
printf("无法打开此文件!\n");
exit(0);
}
while ((a[i]= fgetc(fp)) != EOF)// 文件指针fp指向的文件中读取一个字符并返回;EOF是文件结束的标准 =-1
{
i++;
}
a[i] = '\0';
printf("%s", a);
printf("\n");
for (j = 0; j <=i; j++)
{
if (a[j] == '>'&&a[j+1]!='\n')
{
while ( a[j+1] != '<')
{
printf("%c", a[j + 1]);
j++;
}
printf("\n");
}
}
fclose(fp);
return 0;
}
.XML文件
<?xml version="1.0" encoding="utf-8"?>
<books>
<book>
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book>
<title>Windows Core </title>
<author>Jeffrey Richter</author>
<year>2005</year>
<price>59.95</price>
</book>
</books>
结果
<?xml version="1.0" encoding="utf-8"?>
<books>
<book>
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book>
<title>Windows Core </title>
<author>Jeffrey Richter</author>
<year>2005</year>
<price>59.95</price>
</book>
</books>
Learning XML
Erik T. Ray
2003
39.95
Windows Core
Jeffrey Richter
2005
59.95