根据标签名称,查找xml文件内的标签数据
#include "Main.h"
#include<stdio.h>
#include<string.h>
int getTitleList(char* pszXmlData,int nXmlDataLen,char* pszTitleData,int* pnTitleLen) {
if (pszXmlData==NULL||nXmlDataLen <= 0) {
return 0;
}
*pnTitleLen = 0;
char c;
bool flag = false;
for (int i = 0; i < nXmlDataLen;i++) {
c = *(pszXmlData + i);
if (c == '<') {
flag = true;
continue;
}
else if(c=='>')
{
flag = false;
int temp = i;
*(pszTitleData + (*pnTitleLen)++) = '.';
do{
*(pszTitleData + (*pnTitleLen)++) = temp % 10+'0';
temp /= 10;
} while (temp / 1 != 0);
*(pszTitleData + (*pnTitleLen)++) = '.';
continue;
}
if (flag) {
*(pszTitleData+*pnTitleLen) = c;
(*pnTitleLen)++;
}
}
return 1;
}
int getPosition(char* pszTitleNameList,int nTitleNameListLen,char* pszTitleName,int nTitleNameLen, int* pnBegin, int* pnEnd) {
if (pszTitleNameList == NULL || nTitleNameListLen <= 0|| pszTitleName == NULL || nTitleNameLen <= 0) {
return 0;
}
int i = 0, j = 0;
while(i < nTitleNameListLen&&j<nTitleNameLen) {
if (pszTitleNameList[i] == pszTitleName[j]) {
i++; j++;
}
else {
i = i - j + 1;
j = 0;
}
if (j == nTitleNameLen - 1) {
break;
}
}
*pnBegin = 0;
int wei = 10;
while (pszTitleNameList[i+2]!='.') {
*pnBegin += wei * (pszTitleNameList[i + 2] - '0');
wei *= 10;
i++;
}
*pnBegin /= 10;
while (i < nTitleNameListLen && j < nTitleNameLen) {
if (pszTitleNameList[i] == pszTitleName[j]) {
i++; j++;
}
else {
i = i - j + 1;
j = 0;
}
if (j == nTitleNameLen - 1) {
break;
}
}
*pnEnd = 0;
wei = 10;
while (pszTitleNameList[i + 2] != '.') {
*pnEnd += wei * (pszTitleNameList[i + 2] - '0');
wei *= 10;
i++;
}
*pnEnd /= 10;
*pnBegin += 1;
*pnEnd -= (nTitleNameLen+2);
return 1;
}
int main() {
char a[1024] = "<html><head>aaa<title>A test</title>ccc</head><body>HelloWorld!</body></html>";
printf("%s\n", a);
int aLen = strlen(a);
char b[1024] = { 0 };
int bLen = 0;
int x = getTitleList(a, aLen, b, &bLen);
if (x) {
printf("Result:%s\n %d\n",b,bLen);
}
char c[1024] = "head";
int cLen = strlen(c);
int tou;
int wei;
x=getPosition(b,bLen,c,cLen, &tou, &wei);
if (x) {
printf("tou:%d\nwei:%d\n", tou, wei);
}
for (int i = tou; i < wei; i++) {
printf("%c", a[i]);
}
printf("\n");
return 0;
}