1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      phpxml數(shù)據(jù)編寫 phpxml解析

      PHP 讀取和編寫 XML

      什么是

      成都創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、榮昌網(wǎng)絡(luò)推廣、微信小程序、榮昌網(wǎng)絡(luò)營銷、榮昌企業(yè)策劃、榮昌品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供榮昌建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.cdcxhl.com

      XML?

      XML

      是一種數(shù)據(jù)存儲格式。它沒有定義保存什么數(shù)據(jù),也沒有定義數(shù)據(jù)的格式。XML

      只是定義了標(biāo)記和這些標(biāo)記的屬性。格式良好的

      XML

      標(biāo)記看起來像這樣:

      復(fù)制代碼

      代碼如下:

      nameJack

      Herrington/name

      DOM讀取

      XML

      復(fù)制代碼

      代碼如下:

      ?php

      $doc

      =

      new

      DOMDocument();

      $doc-load(

      'books.xml'

      );

      $books

      =

      $doc-getElementsByTagName(

      "book"

      );

      foreach(

      $books

      as

      $book

      )

      {

      $authors

      =

      $book-getElementsByTagName(

      "author"

      );

      $author

      =

      $authors-item(0)-nodeValue;

      $publishers

      =

      $book-getElementsByTagName(

      "publisher"

      );

      $publisher

      =

      $publishers-item(0)-nodeValue;

      $titles

      =

      $book-getElementsByTagName(

      "title"

      );

      $title

      =

      $titles-item(0)-nodeValue;

      echo

      "$title

      -

      $author

      -

      $publisher\n";

      }

      ?

      DOM

      編寫

      XML

      復(fù)制代碼

      代碼如下:

      ?php

      $books

      =

      array();

      $books

      []

      =

      array(

      'title'

      =

      'PHP

      Hacks',

      'author'

      =

      'Jack

      Herrington',

      );

      $doc

      =

      new

      DOMDocument();

      //創(chuàng)建dom對象

      $doc-formatOutput

      =

      true;

      $r

      =

      $doc-createElement(

      "books"

      );//創(chuàng)建標(biāo)簽

      $doc-appendChild(

      $r

      );

      //將$r標(biāo)簽,加入到xml格式中。

      foreach(

      $books

      as

      $book

      )

      {

      $b

      =

      $doc-createElement(

      "book"

      );

      //創(chuàng)建標(biāo)簽

      $author

      =

      $doc-createElement(

      "author"

      );

      $author-appendChild($doc-createTextNode(

      $book['author']

      ));

      //給標(biāo)簽添加內(nèi)容

      $b-appendChild(

      $author

      );

      //將子標(biāo)簽

      加入父標(biāo)簽

      $r-appendChild(

      $b

      );

      //加入父標(biāo)簽中!

      }

      echo

      $doc-saveXML();

      ?

      以上就是這2段讀取和編寫XML的DOM代碼了,小伙伴們了解了沒,有什么疑問可以給我留言

      php輸出xml文件應(yīng)該如何寫

      最簡單的辦法就是拼湊:

      echo '?xml version="1.0" encoding="utf-8" ?';

      echo 'skplyaer';

      ech ...

      echo '/ckplayer';

      php生成xml代碼快說把

      使用PHP DOMDocument創(chuàng)建動態(tài)XML文件

      當(dāng)處理基于XML應(yīng)用程序時(shí),開發(fā)者經(jīng)常需要建立XML編碼數(shù)據(jù)結(jié)構(gòu)。例如,Web中基于用戶輸入的XML狀態(tài)模板,服務(wù)器請求XML語句,以及基于運(yùn)行時(shí)間參數(shù)的客戶響應(yīng)。

      盡管XML數(shù)據(jù)結(jié)構(gòu)的構(gòu)建比較費(fèi)時(shí),但如果使用成熟的PHP DOM應(yīng)用程序接口,一切都會變得簡單明了。本文將向你介紹PHP DOM應(yīng)用程序接口的主要功能,演示如何生成一個(gè)正確的XML完整文件并將其保存到磁盤中。

      創(chuàng)建文檔類型聲明

      一般而言,XML聲明放在文檔頂部。在PHP中聲明十分簡單:只需實(shí)例化一個(gè)DOM文檔類的對象并賦予它一個(gè)版本號。查看程序清單A:

      程序清單 A

      復(fù)制代碼 代碼如下:

      ?php

      // create doctype

      $dom = new DOMDocument("1.0");

      // display document in browser as plain text

      // display document in browser as plain text

      // for readability purposes

      header("Content-Type: text/plain");

      // save and display tree

      echo $dom-saveXML();

      ?

      請注意DOM文檔對象的saveXML()方法。稍后我再詳細(xì)介紹這一方法,現(xiàn)在你只需要簡單認(rèn)識到它用于輸出XML文檔的當(dāng)前快照到一個(gè)文件或?yàn)g覽器。在本例,為增強(qiáng)可讀性,我已經(jīng)將ASCII碼文本直接輸出至瀏覽器。在實(shí)際應(yīng)用中,可將以text/XML頭文件發(fā)送到瀏覽器。

      如在瀏覽器中查看輸出,你可看到如下代碼:

      ?xml version="1.0"?

      添加元素和文本節(jié)點(diǎn)

      XML真正強(qiáng)大的功能是來自其元素與封裝的內(nèi)容。幸運(yùn)的是,一旦你初始化DOM文檔,很多操作變得很簡單。此過程包含如下兩步驟:

      對想添加的每一元素或文本節(jié)點(diǎn),通過元素名或文本內(nèi)容調(diào)用DOM文檔對象的createElement()或createTextNode()方法。這將創(chuàng)建對應(yīng)于元素或文本節(jié)點(diǎn)的新對象。

      通過調(diào)用節(jié)點(diǎn)的appendChild()方法,并把其傳遞給上一步中創(chuàng)建的對象,并在XML文檔樹中將元素或文本節(jié)點(diǎn)添加到父節(jié)點(diǎn)。

      以下范例將清楚地演示這2步驟,請查看程序清單B。

      程序清單 B

      復(fù)制代碼 代碼如下:

      ?php

      // create doctype

      $dom = new DOMDocument("1.0");

      // display document in browser as plain text

      // for readability purposes

      header("Content-Type: text/plain");

      // create root element

      $root = $dom-createElement("toppings");

      $dom-appendChild($root);

      // create child element

      $item = $dom-createElement("item");

      $root-appendChild($item);

      // create text node

      $text = $dom-createTextNode("pepperoni");

      $item-appendChild($text);

      // save and display tree

      echo $dom-saveXML();

      ?

      這 里,我首先創(chuàng)建一個(gè)名字為toppings的根元素,并使它歸于XML頭文件中。然后,我建立名為item的元素并使它 歸于根元素。最后,我又創(chuàng)建一個(gè)值為“pepperoni”的文本節(jié)點(diǎn)并使它歸于item元素。最終結(jié)果如下:

      復(fù)制代碼 代碼如下:

      ?xml version="1.0"?

      toppings

      itempepperoni/item

      /toppings

      如果你想添加另外一個(gè)topping,只需創(chuàng)建另外一個(gè)item并添加不同的內(nèi)容,如程序清單C所示。

      程序清單C

      復(fù)制代碼 代碼如下:

      ?php

      // create doctype

      $dom = new DOMDocument("1.0");

      // display document in browser as plain text

      // for readability purposes

      header("Content-Type: text/plain");

      // create root element

      $root = $dom-createElement("toppings");

      $dom-appendChild($root);

      // create child element

      $item = $dom-createElement("item");

      $root-appendChild($item);

      // create text node

      $text = $dom-createTextNode("pepperoni");

      $item-appendChild($text);

      // create child element

      $item = $dom-createElement("item");

      $root-appendChild($item);

      // create another text node

      $text = $dom-createTextNode("tomato");

      $item-appendChild($text);

      // save and display tree

      echo $dom-saveXML();

      ?

      以下是執(zhí)行程序清單C后的輸出:

      復(fù)制代碼 代碼如下:

      ?xml version="1.0"?

      toppings

      itempepperoni/item

      itemtomato/item

      /toppings

      添加屬性

      通過使用屬性,你也可以添加適合的信息到元素。對于PHP DOM API,添加屬性需要兩步:首先用DOM文檔對象的createAttribute()方法創(chuàng)建擁有此屬性名字的節(jié)點(diǎn),然后將文檔節(jié)點(diǎn)添加到擁有屬性值的屬性節(jié)點(diǎn)。詳見程序清單D。

      程序清單 D

      復(fù)制代碼 代碼如下:

      ?php

      // create doctype

      $dom = new DOMDocument("1.0");

      // display document in browser as plain text

      // for readability purposes

      header("Content-Type: text/plain");

      // create root element

      $root = $dom-createElement("toppings");

      $dom-appendChild($root);

      // create child element

      $item = $dom-createElement("item");

      $root-appendChild($item);

      // create text node

      $text = $dom-createTextNode("pepperoni");

      $item-appendChild($text);

      // create attribute node

      $price = $dom-createAttribute("price");

      $item-appendChild($price);

      // create attribute value node

      $priceValue = $dom-createTextNode("4");

      $price-appendChild($priceValue);

      // save and display tree

      echo $dom-saveXML();

      ?

      輸出如下所示:

      復(fù)制代碼 代碼如下:

      ?xml version="1.0"?

      toppings

      item price="4"pepperoni/item

      /toppings

      添加CDATA模塊和過程向?qū)?/p>

      雖然不經(jīng)常使用CDATA模塊和過程向?qū)В峭ㄟ^調(diào)用DOM文檔對象的createCDATASection()和createProcessingInstruction()方法, PHP API 也能很好地支持CDATA和過程向?qū)В堃姵绦蚯鍐蜤。

      程序清單 E

      復(fù)制代碼 代碼如下:

      ?php

      // create doctype

      // create doctype

      $dom = new DOMDocument("1.0");

      // display document in browser as plain text

      // for readability purposes

      header("Content-Type: text/plain");

      // create root element

      $root = $dom-createElement("toppings");

      $dom-appendChild($root);

      // create child element

      $item = $dom-createElement("item");

      $root-appendChild($item);

      // create text node

      $text = $dom-createTextNode("pepperoni");

      $item-appendChild($text);

      // create attribute node

      $price = $dom-createAttribute("price");

      $item-appendChild($price);

      // create attribute value node

      $priceValue = $dom-createTextNode("4");

      $price-appendChild($priceValue);

      // create CDATA section

      $cdata = $dom-createCDATASection(" Customer requests that pizza be sliced into 16 square pieces ");

      $root-appendChild($cdata);

      // create PI

      $pi = $dom-createProcessingInstruction("pizza", "bake()");

      $root-appendChild($pi);

      // save and display tree

      echo $dom-saveXML();

      ?

      輸出如下所示:

      復(fù)制代碼 代碼如下:

      ?xml version="1.0"?

      toppings

      item price="4"pepperoni/item

      ![CDATA[

      Customer requests that pizza be sliced into 16 square pieces

      ]]

      ?pizza bake()?

      /toppings

      保存結(jié)果

      一旦已經(jīng)實(shí)現(xiàn)你的目標(biāo),就可以將結(jié)果保存在一個(gè)文件或存儲于PHP的變量。通過調(diào)用帶有文件名的save()方法可以將結(jié)果保存在文件中,而通過調(diào)用saveXML()方法可存儲于PHP的變量。請參考以下實(shí)例(程序清單F):

      程序清單 F

      復(fù)制代碼 代碼如下:

      ?php

      // create doctype

      $dom = new DOMDocument("1.0");

      // create root element

      $root = $dom-createElement("toppings");

      $dom-appendChild($root);

      $dom-formatOutput=true;

      // create child element

      $item = $dom-createElement("item");

      $root-appendChild($item);

      // create text node

      $text = $dom-createTextNode("pepperoni");

      $item-appendChild($text);

      // create attribute node

      $price = $dom-createAttribute("price");

      $item-appendChild($price);

      // create attribute value node

      $priceValue = $dom-createTextNode("4");

      $price-appendChild($priceValue);

      // create CDATA section

      $cdata = $dom-createCDATASection(" Customer requests that pizza be

      sliced into 16 square pieces ");

      $root-appendChild($cdata);

      // create PI

      $pi = $dom-createProcessingInstruction("pizza", "bake()");

      $root-appendChild($pi);

      // save tree to file

      $dom-save("order.xml");

      // save tree to string

      $order = $dom-save("order.xml");

      ?

      下面是實(shí)際的例子,大家可以測試下。

      xml.php(生成xml)

      復(fù)制代碼 代碼如下:

      ?

      $conn = mysql_connect('localhost', 'root', '123456') or die('Could not connect: ' . mysql_error());

      mysql_select_db('vdigital', $conn) or die ('Can\'t use database : ' . mysql_error());

      $str = "SELECT id,username FROM `admin` GROUP BY `id` ORDER BY `id` ASC";

      $result = mysql_query($str) or die("Invalid query: " . mysql_error());

      if($result)

      {

      $xmlDoc = new DOMDocument();

      if(!file_exists("01.xml")){

      $xmlstr = "?xml version='1.0' encoding='utf-8' ?message/message";

      $xmlDoc-loadXML($xmlstr);

      $xmlDoc-save("01.xml");

      }

      else { $xmlDoc-load("01.xml");}

      $Root = $xmlDoc-documentElement;

      while ($arr = mysql_fetch_array($result)){

      $node1 = $xmlDoc-createElement("id");

      $text = $xmlDoc-createTextNode(iconv("GB2312","UTF-8",$arr["id"]));

      $node1-appendChild($text);

      $node2 = $xmlDoc-createElement("name");

      $text2 = $xmlDoc-createTextNode(iconv("GB2312","UTF-8",$arr["username"]));

      $node2-appendChild($text2);

      $Root-appendChild($node1);

      $Root-appendChild($node2);

      $xmlDoc-save("01.xml");

      }

      }

      mysql_close($conn);

      ?

      test.php(應(yīng)用測試)

      復(fù)制代碼 代碼如下:

      ?

      $xmlDoc = new DOMDocument();

      $xmlDoc-load("");

      $x=$xmlDoc-getElementsByTagName('name');

      for ($i=0; $i=$x-length-1; $i++)

      {

      if(strpos($x-item($i)-nodeValue,"fang")!==false)

      {

      echo $x-item($i)-parentNode-childNodes-item(1)-nodeValue;

      }

      }

      ?


      當(dāng)前題目:phpxml數(shù)據(jù)編寫 phpxml解析
      轉(zhuǎn)載注明:http://www.ef60e0e.cn/article/docsedo.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        精河县| 阿拉尔市| 中西区| 开化县| 常山县| 洪江市| 泾川县| 黄平县| 襄城县| 北川| 兴海县| 岗巴县| 临漳县| 贺兰县| 永靖县| 即墨市| 汾西县| 喜德县| 色达县| 壶关县| 曲麻莱县| 怀柔区| 晋城| 尼木县| 招远市| 清流县| 永嘉县| 南和县| 乡城县| 嘉义市| 保康县| 阳新县| 灵台县| 颍上县| 朝阳区| 龙井市| 惠来县| 兴宁市| 松阳县| 梅州市| 玉林市|