真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

php取xml數(shù)據(jù),php xml解析

如何通過PHP生成和獲取XML格式數(shù)據(jù)

1自己拼,XML編碼

成都創(chuàng)新互聯(lián)公司服務(wù)項(xiàng)目包括南安網(wǎng)站建設(shè)、南安網(wǎng)站制作、南安網(wǎng)頁制作以及南安網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,南安網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到南安省份的部分城市,未來相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

?php

header('Content-type:text/xml');

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

echo "book";

echo "PHP";

echo "namePHP程序開發(fā)范例寶典/name";

echo "price 單位='元/本'89.00/price";

echo "date2007-09-01/date";

echo "/PHP";

echo "/book";

?

拼接的效果

2從數(shù)據(jù)庫中查詢?cè)倨碭ML編碼

?php

$dsn="mysql:host=localhost;dbname=test";

try {

$pdo = new PDO($dsn,'root','passwowd'); //初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫連接對(duì)象$pdo

$query="select * from book"; ? //定義SQL語句

$pdo-query('set names utf8');

$result=$pdo-prepare($query); //準(zhǔn)備查詢語句

$result-execute(); ? ? ? //執(zhí)行查詢語句,并返回結(jié)果集

$arr='';

while($res=$result-fetch()){

$arr.='PHPid'.$res[0].'/idname'.$res[1].'/namedate'.$res[2].'/date'.'price'.$res[3].'/price/PHP';

}

echo "?xml version='1.0' encoding='utf-8'?book".$arr.'/book';

} catch (PDOException $e) {

die ("Error!: ".$e-getMessage()."br");

}

?

拼接的效果

3使用ajax獲取,DOM解析

!DOCTYPE html

html lang="en"

head

meta charset="UTF-8"

titlexml/title

/head

body

script

function check(){

var xhr=new XMLHttpRequest();

xhr.open('GET','xml.php');

xhr.onreadystatechange=function(){

if(xhr.readyState==4 xhr.status==200){

console.log(xhr.responseText);

//初始化 DOM解析對(duì)象

var domParser = new DOMParser();

//字符串解碼為對(duì)象

var xmlDoc = domParser.parseFromString(xhr.responseText,'text/xml');

//按標(biāo)簽名獲取元素 返回?cái)?shù)組

var elements = xmlDoc.getElementsByTagName('PHP');

//拼接html格式字符串

var str ='trthid/ththname/ththdate/ththprice/th/tr';

for (var i=0;ielements.length;i++){

var id=elements[i].getElementsByTagName('id')[0].firstChild.nodeValue;

var name=elements[i].getElementsByTagName('name')[0].firstChild.nodeValue;

var date=elements[i].getElementsByTagName('date')[0].firstChild.nodeValue;

var price=elements[i].getElementsByTagName('price')[0].firstChild.nodeValue;

str+= 'trtd'+id+'/tdtd'+name+'/tdtd'+date+'/tdtd'+price+'/td/tr';

}

document.getElementById('table2').innerHTML=str;

}

};

xhr.send(null);

}

/script

button onclick="check();"點(diǎn)我/button

table id="table2" border="2" cellspacing="0"

/table

/body

/html

效果

1

2

補(bǔ)充:

使用JSON

1數(shù)據(jù)庫查詢,自己拼?JSON 編碼

?php

$dsn="mysql:host=localhost;dbname=test";

try {

$pdo = new PDO($dsn,'root','password'); //初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫連接對(duì)象$pdo

$query="select * from book"; ? //定義SQL語句

$pdo-query('set names utf8');

$result=$pdo-prepare($query); //準(zhǔn)備查詢語句

$result-execute(); ? ? ? //執(zhí)行查詢語句,并返回結(jié)果集

$a=$arr='';

while($res=$result-fetch()){

$arr.='{"id":'.'"'.$res[0].'",'.'"name":'.'"'.$res[1].'",'.'"time":'.'"'.$res[2].'",'.'"jia":'.'"'.$res[3].'",'.'"zhe":'.'"'.$res[4].'",'.'"chu":'.'"'.$res[5].'"},';

}

echo $a="[".substr($arr,0,strlen($arr)-1)."]";

} catch (PDOException $e) {

die ("Error!: ".$e-getMessage()."br");

}

?

2數(shù)據(jù)庫查詢,函數(shù)?JSON 編碼

?php

$dsn="mysql:host=localhost;dbname=test";

try {

$pdo = new PDO($dsn,'root','password'); //初始化一個(gè)PDO對(duì)象,就是創(chuàng)建了數(shù)據(jù)庫連接對(duì)象$pdo

$query="select * from book"; ? //定義SQL語句

$pdo-query('set names utf8');

$result=$pdo-prepare($query); //準(zhǔn)備查詢語句

$result-execute(); ? ? ? //執(zhí)行查詢語句,并返回結(jié)果集

$res=$result-fetchAll();

//JSON 編碼

echo json_encode($res);

} catch (PDOException $e) {

die ("Error!: ".$e-getMessage()."br/");

}

?

效果

3ajax獲取,JSON解析

!DOCTYPE html

html lang="en"

head

meta charset="UTF-8"

titleJSON/title

/head

body

script

function check() {

var XHR = new XMLHttpRequest();

XHR.open('GET','JSON.php');

XHR.onreadystatechange = function (){

if(XHR.readyState == 4 XHR.status ==200){

var books =JSON .parse(XHR.responseText);

var books2='trthid/ththbookname/ththtime/ththprice/ththmarker/ththpublisher/th/tr'

for (var i=0;ibooks.length;i++){

books2+= 'trtd'+(books[i ].id)+'/tdtd'+(books[i ].name)+'/tdtd'+(books[i ].time)+'/tdtd'+(books[i ].jia)+'/tdtd'+(books[i ].zhe)+'/tdtd'+(books[i ].chu)+'/td/tr';

}

document.getElementById('table2').innerHTML=books2;

}

};

XHR.send(null);

}

/script

input type="button" value="點(diǎn)我" onclick="check();"

table id="table2" border="2" cellspacing="0"/table

/body

/html

php 讀取xml 用哪種方法好

1、首先我們做一個(gè)簡單的xml文件

?xml version="1.0 encoding="UTF-8"?humanszhangyingname張映/namesex男/sexold28/old/zhangyingtanknametank/namesex男/sexold28/old/tank/humans

2、

php讀取xml的幾種方法:

1)DOMDocument讀取xml

?php $doc = new DOMDocument(); $doc-load('person.xml'); //讀取xml文件 $humans = $doc-getElementsByTagName( "humans" ); //取得humans標(biāo)簽的對(duì)象數(shù)組 foreach( $humans as $human ) { $names = $human-getElementsByTagName( "name" ); //取得name的標(biāo)簽的對(duì)象數(shù)組 $name = $names-item(0)-nodeValue; //取得node中的值,如name /name $sexs = $human-getElementsByTagName( "sex" ); $sex = $sexs-item(0)-nodeValue; $olds = $human-getElementsByTagName( "old" ); $old = $olds-item(0)-nodeValue; echo "$name - $sex - $old\n"; } ?

2)simplexml讀取xml

?php $xml_array=simplexml_load_file('person.xml'); //將XML中的數(shù)據(jù),讀取到數(shù)組對(duì)象中 foreach($xml_array as $tmp){ echo $tmp-name."-".$tmp-sex."-".$tmp-old."br"; } ?

3)用php正則表達(dá)式來記取數(shù)據(jù)

?php $xml = ""; $f = fopen('person.xml', 'r'); while( $data = fread( $f, 4096 ) ) { $xml .= $data; } fclose( $f ); // 上面讀取數(shù)據(jù) preg_match_all( "/\humans\(.*?)\\/humans\/s", $xml, $humans ); //匹配最外層標(biāo)簽里面的內(nèi)容 foreach( $humans[1] as $k=$human ) { preg_match_all( "/\name\(.*?)\\/name\/", $human, $name ); //匹配出名字 preg_match_all( "/\sex\(.*?)\\/sex\/", $human, $sex ); //匹配出性別 preg_match_all( "/\old\(.*?)\\/old\/", $human, $old ); //匹配出年齡 } foreach($name[1] as $key=$val){ echo $val." - ".$sex[$key][1]." - ".$old[$key][1]."br" ; } ?

4)xmlreader來讀取xml數(shù)據(jù)

?php $reader = new XMLReader(); $reader-open('person.xml'); //讀取xml數(shù)據(jù) $i=1; while ($reader-read()) { //是否讀取 if ($reader-nodeType == XMLReader::TEXT) { //判斷node類型 if($i%3){ echo $reader-value; //取得node的值 }else{ echo $reader-value."br" ; } $i++; } } ?

PHP如何取XML中數(shù)據(jù)

轉(zhuǎn)成數(shù)組

?php/*** function:使用字符串方式讀XML文件* author:JetWu* date:2016.12.03**/$file = './write_str.xml';$con = file_get_contents($file); //XML標(biāo)簽配置$xmlTag = array( 'starttime', 'endtime', 'school'); $arr = array();foreach($xmlTag as $x) { preg_match_all("/".$x.".*\/".$x."/", $con, $temp); $arr[] = $temp[0];}//去除XML標(biāo)簽并組裝數(shù)據(jù)$data = array();foreach($arr as $key = $value) { foreach($value as $k = $v) { $a = explode($xmlTag[$key].'', $v); $v = substr($a[1], 0, strlen($a[1])-2); $data[$k][$xmlTag[$key]] = $v; }}echo 'pre';print_r($data);

PHP 獲取XML數(shù)據(jù)

給個(gè)小例子:

1.直接是xml字符串

?php

$string?=?XML

?xml?version='1.0'??

document

titleForty?What?/title

fromJoe/from

toJane/to

body

I?know?that's?the?answer?--?but?what's?the?question?

/body

/document

XML;

$xml?=?simplexml_load_string($string);

print_r($xml);

??

2.?xml文件的

?php

if?(file_exists('test.xml'))?{

$xml?=?simplexml_load_file('test.xml');

print_r($xml);

}?else?{

exit('Failed?to?open?test.xml.');

}

?

PHP讀取XML數(shù)據(jù)

編碼問題,function

characterData改成如下

function

characterData($parser_instance,

$xml_data)

{

global

$name,$position;

$xml_data=iconv("utf-8","gb2312",$xml_data);//轉(zhuǎn)換成gb2312

if($position)

echo

$xml_data."br";

if($name)

echo

$xml_data."br";

}

顯示的那個(gè)頁面也要有charset=gb2312


當(dāng)前題目:php取xml數(shù)據(jù),php xml解析
本文地址:http://weahome.cn/article/dsgogdh.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部