檔案xml的讀取

2021-06-07 10:00:32 字數 1967 閱讀 6306

檔案複製到沙箱中

nsstring

*filename1 = 

@"help.xml"

;nsarray

*paths1 = 

nssearchpathfordirectoriesindomains

(nsdocumentdirectory

, nsuserdomainmask

, yes

);nsstring

*sandboxpath1 = [paths1 

objectatindex:0

];nsstring

*mainbundlepath1 = [[

nsbundle

mainbundle

] resourcepath

];//document目錄檔案路徑

nsstring

*dst_path1 = [

nsstring

stringwithformat

:@"%@/%@"

,sandboxpath1,filename1];

//源檔案目錄檔案路徑

nsstring

*src_path1 = [

nsstring

stringwithformat

:@"%@/%@"

,mainbundlepath1,filename1];

nsfilemanager

*fm1 = [

nsfilemanager

defaultmanager

];//把源檔案目錄下的檔案拷貝到document目錄檔案路徑

[fm1 

copyitematpath

:src_path1 

topath

:dst_path1 

error

:nil

];這以後我的document目錄下就有這個xml檔案了!

程式要求在有網路的情況下,從伺服器解析獲取資料來解析;在沒網路的情況下讀本地的xml檔案;

於是我就在程式中寫了檔案的讀和寫:

//檔案的讀

-(nsstring

*)readfile

document

//檔案的寫

-(void

)writefile:(

nsstring

*)file

在有網路的情況下,我在程式裡調檔案的寫的方法把help.xml寫到documents目錄下;

在沒網路的情況下,我在程式裡調檔案的讀的方法

nsstring

*text = [

self

readfile

];nslog

(@"%@"

,text);

也能列印出來,可當我解析的時候就解析不出來了?

不知道為什麼? 另外,我每次程序式,在有網路的情況下,我把解析下來的資料寫到document目錄下,這是不是代表,如果伺服器資料變化,我這就更新了......?因為每次調這個寫的方法?

對了,我補充一句,我要在沒網路情況下,是去讀document目錄下的檔案嗎?還是其他的......另外,求大神們指點下我的思路對嗎?

解決了,原來是我在沒有進行解碼出來的檔案直接複製到程式建立的檔案中;

nsdata* newdata = [encode gb2312toutf8fornsdataxml:data];

text = [encode nsdata2nsstringforutf8:newdata]; 

只有經過這步,才再複製,別看他和沒解碼前長的一樣,其實你在解析的時候是解不出來的;

我是把在有網路解析出來,解過碼的xml檔案,把內容複製到到程式的xml檔案中。

追加一句,檢視程式中的xml檔案轉碼沒有,直接看xml檔案的頭

<?xml version="1.0" encoding="utf-8"?>,後面跟沒跟encoding="utf-8"?,如果跟了,就沒解碼,沒跟就解了!

XML檔案的讀取

creates an xmlelement from a stream xmlelement pointing to the root of the xml public static xmlelement createxmlnode stream stream xmlelement doc.doc...

xml檔案的讀取

一.讀取xml檔案的步驟 1.遍歷所有節點元素,分別讀取 遞迴 2.根據節點名稱或者屬性名稱獲取節點的值 例子 把xml解析的 建立xml物件 xmldocument xmldoc new xmldocument xml路徑 載入 xmldoc.load xmlpath 1.獲取xml檔案的根節點 ...

讀取XML檔案

private void button2 click object sender,eventargs e using filestream fs new filestream filename,filemode.open,fileaccess.read n xl.name 是否有屬性 if xl.h...