以 C 語言取得 XML 欄位內容

計算語言學

簡介

詞彙

語法

語意

理解

問題領域

語言生成

語法剖析

語意分析

處理方法

正規表達式

BNF 語法

掃描

剖析器

符號表

解譯

編譯

翻譯

各種語言

組合語言

程式語言

標記語言

維基語言

自然語言

中文

英文

程式實作

JavaScript

C

Python

相關書籍

自然語言處理

編譯器設計

系統程式

訊息

相關網站

參考文獻

最新修改

簡體版

English

內嵌字串版

程式:innerText.c

#include <stdio.h>
 
char *innerText(char *inner, char *pText, char *beginMark, char *endMark) {
     char *beginStart = strstr(pText, beginMark);
     if (beginStart == NULL) return NULL;
     char *beginEnd = beginStart + strlen(beginMark);
     char *endStart = strstr(beginEnd, endMark);
     if (endStart < 0) return NULL;
     int len = endStart-beginEnd;
     strncpy(inner, beginEnd, len);
     inner[len] = '\0';
}
 
int main() {
    char xml[] = "<people name=\"陳鍾誠\" sex=\"男\"><age>43</age><hometown>金門縣</hometown></people>";
    char name[30], sex[10], age[10], hometown[30];
    innerText(name, xml, "name=\"", "\"");
    printf("name=%s\n", name);
    innerText(sex,  xml, "sex=\"", "\"");
    printf("sex=%s\n", sex);
    innerText(age,  xml, "<age>", "</age>");
    printf("age=%s\n", age);
    innerText(hometown,  xml, "<hometown>", "</hometown>");
    printf("hometown=%s\n", hometown);
}

執行結果

G:\ccc102\Linguistics\exercise>gcc innerText.c -o innerText

G:\ccc102\Linguistics\exercise>innerText
name=陳鍾誠
sex=男
age=43
hometown=金門縣

檔案讀取版

程式:fileInnerText.c

#include <stdio.h>
#include <stdlib.h>

char *innerText(char *inner, char *pText, char *beginMark, char *endMark) {
     char *beginStart = strstr(pText, beginMark);
     if (beginStart == NULL) return NULL;
     char *beginEnd = beginStart + strlen(beginMark);
     char *endStart = strstr(beginEnd, endMark);
     if (endStart < 0) return NULL;
     int len = endStart-beginEnd;
     strncpy(inner, beginEnd, len);
     inner[len] = '\0';
}

// 檔案輸出入 
char* fileToStr(char *fileName, int *sizePtr) {
  printf("fileName=%s\n", fileName);
  FILE *file = fopen(fileName, "rb");
  fseek(file, 0 , SEEK_END);
  long size = ftell(file);
  rewind(file);
  char *buffer = (char*) malloc(size+1);
  fread (buffer,size,1,file);
  fclose(file);
  return buffer;
}

int main(int argc, char *argv[]) {
    int size;
    char *xml = fileToStr(argv[1], &size);
//    char xml[] = "<people name=\"陳鍾誠\" sex=\"男\"><age>43</age><hometown>金門縣</hometown></people>";
    char name[30], sex[10], age[10], hometown[30];
    innerText(name, xml, "name=\"", "\"");
    printf("name=%s\n", name);
    innerText(sex,  xml, "sex=\"", "\"");
    printf("sex=%s\n", sex);
    innerText(age,  xml, "<age>", "</age>");
    printf("age=%s\n", age);
    innerText(hometown,  xml, "<hometown>", "</hometown>");
    printf("hometown=%s\n", hometown);    
    free(xml);
}

輸入檔:cccBig5.xml

<?xml version="1.0" encoding="BIG5" ?>
<root>
<people name="陳鍾誠" sex="男">
  <age>43</age>
  <hometown>金門縣</hometown>
</people>
</root>

執行結果

G:\ccc102\Linguistics\exercise>gcc fileInnerText.c -o fileInnerText

G:\ccc102\Linguistics\exercise>fileInnerText cccBig5.xml
fileName=cccBig5.xml
name=陳鍾誠
sex=男
age=43
hometown=金門縣

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License