powerbuilder(pb)中 xml的應(yīng)用一例_Xml教程

      編輯Tag賺U幣

      推薦:存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼
      在數(shù)據(jù)提交到數(shù)據(jù)庫(kù)之前的簡(jiǎn)單HTML字符轉(zhuǎn)意,防止javascript惡意代碼,因?yàn)轫?xiàng)目中用到了輸出為xml,所以在輸出之前還要進(jìn)行二次轉(zhuǎn)意,把數(shù)據(jù)庫(kù)中之前加入的br轉(zhuǎn)義為正常。

      示例文件如下(不貼DTD了,舉簡(jiǎn)單例子說(shuō)明一下)

      復(fù)制代碼 代碼如下:www.wf0088.com

      <trans>
      <transdetail>
      <order><date/></order>
      <orderdetail><product/></orderdetail>
      <orderdetail><product/></orderdetail>
      </transdetail>
      <transdetail>
      <order><date/></order>
      <orderdetail><product/></orderdetail>
      <orderdetail><product/></orderdetail>
      </transdetail>
      </trans>

      我的表結(jié)構(gòu),我想大家的表也應(yīng)該都是這樣設(shè)計(jì)的
      order(銷(xiāo)售訂單,包括客戶,日期等信息)
      orderdetail(銷(xiāo)售訂單明細(xì),包括產(chǎn)品,數(shù)量及價(jià)格信息)

      至此,可能明眼人一眼就能看出,這個(gè)xml的格式設(shè)置有些問(wèn)題,例如這樣可能更加合理
      復(fù)制代碼 代碼如下:www.wf0088.com

      <trans>
      <!--transdetail 這個(gè)節(jié)或許是多余的-->
      <order>
      <date/>
      <detail><!-- 明細(xì)是一個(gè)訂單的一部分,不應(yīng)該脫離訂單頭-->
      <orderdetail><product/></orderdetail>
      <orderdetail><product/></orderdetail>
      </detail>
      </order>
      <order>
      <date/>
      <detail>
      <orderdetail><product/></orderdetail>
      <orderdetail><product/></orderdetail>
      </detail>
      </order>
      </trans>

      不過(guò)人家是ZF部門(mén),改不了的,所以蹩腳也得做

      pb9中的處理代碼:其實(shí)在pb9種只寫(xiě)了三行代碼,真正的代碼其實(shí)只有一行,就是增加了一個(gè)窗口,上面放了一個(gè)數(shù)據(jù)窗口,一個(gè)按鈕,按鈕里寫(xiě)了這么一行代碼,呵呵
      dw_export.save("c:\test.xml",xml!,false)

      其實(shí)真正要處理的是定義個(gè)兩個(gè)數(shù)據(jù)窗口,主要是定義他們的xml模版:
      d_order(訂單頭數(shù)據(jù)出口,第二行代碼,可以在EITX中設(shè)置)
      1.新建數(shù)據(jù)窗口(這里注意,如果如何條件的數(shù)據(jù)有多行時(shí),最好在SQL中進(jìn)行g(shù)roup,否則生成的數(shù)據(jù)會(huì)有重復(fù))
      3.在export/import template xml(下面簡(jiǎn)稱EITX)編輯區(qū)點(diǎn)右鍵,save as另一個(gè)名字
      4.把data export下的use template設(shè)置為你剛剛保存的模版名
      定義好的模版如下所示:
      復(fù)制代碼 代碼如下:www.wf0088.com

      <?xml version=~"1.0~" encoding=~"gb2312~" standalone=~"no~"?>
      <trans>
      <transdetail __pbband=~"detail~"><!-- 在EITX中的transdetail節(jié)上點(diǎn)右鍵選中"starts detail" [注1] -->
      <order>
      <date>order_date</date>
      </order>
      dw_detail <!-- 在EITX中的transdetail節(jié)上點(diǎn)右鍵選"add child"下的"datawindow control refrence" [注2] -->
      </transdetail>
      </trans>

      有兩點(diǎn)需要注意
      [注1]這個(gè)start detail,將控制數(shù)據(jù)的循環(huán),所以需要選中,但是每個(gè)xml只能定義一個(gè),這里就會(huì)產(chǎn)生一個(gè)問(wèn)題,如果我訂單頭循環(huán)后,如何再讓訂單明細(xì)循環(huán),結(jié)論是,在一個(gè)數(shù)據(jù)窗口中無(wú)法實(shí)現(xiàn),必須分?jǐn)?shù)據(jù)窗口進(jìn)行處理,也就有了注2
      [注2]我們需要在d_order中插入一個(gè)report,也就是d_orderdetail,在d_order中的control list(同在datawindow control refrence中相同)中就是dw_detail(默認(rèn)名稱是dw_1,我改名了)

      d_orderdetail(訂單明細(xì)數(shù)據(jù)出口,也就是上面report,dw_detail引用的數(shù)據(jù)窗口,第三行代碼,可以在EITX中設(shè)置)
      1.新建數(shù)據(jù)窗口
      2.在export/import template xml(下面簡(jiǎn)稱EITX)編輯區(qū)點(diǎn)右鍵,save as另一個(gè)名字
      3.把data export下的use template設(shè)置為你剛剛保存的模版名
      <?xml version=~"1.0~" encoding=~"gb2312~" standalone=~"no~"?>
      <detail><!-- 這兒定義為orderdetail是沒(méi)用的,牽套時(shí),會(huì)被忽略 [注3]-->
      <orderdetail __pbband=~"detail~"><!-- [注4] -->
      <product>product_name</product>
      </orderdetail>
      </detail>
      [注3]注意,當(dāng)我們?cè)赿_order中導(dǎo)出xml時(shí),d_orderdetail中的xml聲明和頂節(jié)點(diǎn)會(huì)被忽略
      [注4]這個(gè)地方定義的就是orderdetail部分,因?yàn)橐粋(gè)訂單可能會(huì)有多條明細(xì)信息,所以我們需要設(shè)置為start detail,也就是循環(huán)。

      最后生成的文件如下
      復(fù)制代碼 代碼如下:www.wf0088.com

      <trans>
      <transdetail>
      <order><date>20080101</date></order>
      <orderdetail><product>甲</product></orderdetail>
      <orderdetail><product>已</product></orderdetail>
      </transdetail>
      <transdetail>
      <order><date>20080102</date></order>
      <orderdetail><product>甲</product></orderdetail>
      <orderdetail><product>丙</product></orderdetail>
      </transdetail>
      </trans>

      注:如果讓你設(shè)計(jì)一個(gè)xml接口文件,請(qǐng)一定要考慮使用者的方便性

       

      分享:讀大數(shù)據(jù)量的XML文件的讀取問(wèn)題
      對(duì)于數(shù)據(jù)量較大的xml文件,使用xmlDocument讀取時(shí),雖然支持XPath,查詢比較方便,但是需要先load,這樣就浪費(fèi)了內(nèi)存,使用起來(lái)速度比較慢。

      來(lái)源:模板無(wú)憂//所屬分類:Xml教程/更新時(shí)間:2013-04-22
      相關(guān)Xml教程