2015-11-14 41 views
1

我正在通過套接字讀取數據並使用LibXML解析它。在C中編碼HTML字符

我目前遇到的問題是,有時在數據中會有一個Web鏈接打破解析器。

http://example.com/?key=value&key2=value

有沒有辦法將其轉換成HTML字符?

有點像上面套接字數據的

http://example.com/?key=value&key2=value

例子:

<node link="http://example.com/?key=value&key2=value" />

編輯: Found a solution that works for my problem here

+1

我瞎了還是這兩個例子沒有什麼區別? – bolov

+0

這兩個例子是相同的,不是他們! – Ikbel

+0

使用反引號'否則放大器不會顯示。 – drum

回答

0

我發現利用 Find and Replace使用一個查找和替換由bolov提出方法的代碼,很好的解決方案。

retval = str_replace(message, size, "&", "&amp;"); 
if (!retval) { 
    printf("Not enough room to replace & with `&amp;'\n"); 
} 
1

你將不得不做一個預在這裏過濾。與其他跡象相反,搜索和替換不會削減它。考慮你的搜索方面是&,這太匹配了。

構建以下的有限狀態機:

NORMAL: 
    if next matches "<" then TAG 

TAG: 
    if next matches "![CDATA[" then CDATA 
    TAGSCAN 

TAGSCAN: 
    if next matches whitespace then TAGSCAN2 
    if next matches > or next matches /> then NORMAL 

TAGSCAN2: 
    if next matches whitespace then TAGSCAN2 
    if next matches SRC= or next matches HREF= then URL 
    TAGSCAN 

URL: 
    we found an attribute with a URL in it. Do your search and replace 
    on the contents of the URL attribute value, advance past the URL and 
    go back to TAGSCAN 

CDATA: 
    if next doesn't match ]]> then CDATA 
    NORMAL 
+1

「![CDATA [」in url ???他只想重新格式化網址,而不是解析文件內容。 – milevyo

+0

Joshua