php获取xml中的数据,php 生成xml
PHP读取XML数据
编码问题,function
公司主营业务:网站设计制作、成都做网站、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联公司推出梨树免费做网站回馈大家。
characterData改成如下
function
characterData($parser_instance,
$xml_data)
{
global
$name,$position;
$xml_data=iconv("utf-8","gb2312",$xml_data);//转换成gb2312
if($position)
echo
$xml_data."br";
if($name)
echo
$xml_data."br";
}
显示的那个页面也要有charset=gb2312
如何通过PHP生成和获取XML格式数据
1自己拼,XML编码
?php
header('Content-type:text/xml');
echo "?xml version='1.0' encoding='utf-8'";
echo "book";
echo "PHP";
echo "namePHP程序开发范例宝典/name";
echo "price 单位='元/本'89.00/price";
echo "date2007-09-01/date";
echo "/PHP";
echo "/book";
?
拼接的效果
2从数据库中查询再拼XML编码
?php
$dsn="mysql:host=localhost;dbname=test";
try {
$pdo = new PDO($dsn,'root','passwowd'); //初始化一个PDO对象,就是创建了数据库连接对象$pdo
$query="select * from book"; //定义SQL语句
$pdo-query('set names utf8');
$result=$pdo-prepare($query); //准备查询语句
$result-execute(); //执行查询语句,并返回结果集
$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解析对象
var domParser = new DOMParser();
//字符串解码为对象
var xmlDoc = domParser.parseFromString(xhr.responseText,'text/xml');
//按标签名获取元素 返回数组
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();"点我/button
table id="table2" border="2" cellspacing="0"
/table
/body
/html
效果
1
2
补充:
使用JSON
1数据库查询,自己拼 JSON 编码
?php
$dsn="mysql:host=localhost;dbname=test";
try {
$pdo = new PDO($dsn,'root','password'); //初始化一个PDO对象,就是创建了数据库连接对象$pdo
$query="select * from book"; //定义SQL语句
$pdo-query('set names utf8');
$result=$pdo-prepare($query); //准备查询语句
$result-execute(); //执行查询语句,并返回结果集
$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数据库查询,函数 JSON 编码
?php
$dsn="mysql:host=localhost;dbname=test";
try {
$pdo = new PDO($dsn,'root','password'); //初始化一个PDO对象,就是创建了数据库连接对象$pdo
$query="select * from book"; //定义SQL语句
$pdo-query('set names utf8');
$result=$pdo-prepare($query); //准备查询语句
$result-execute(); //执行查询语句,并返回结果集
$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="点我" onclick="check();"
table id="table2" border="2" cellspacing="0"/table
/body
/html
PHP 读取 XML中指定内容
$xml = "returnsmsreturnstatusSuccess/returnstatusmessageok/messageremainpoint-43779/remainpoint taskid496370/taskid/returnsms";
$ob = simplexml_load_string($xml);
echo "returnstatus is:".$ob-returnstatus;
echo "br /";
echo "message is:".$ob-message;
echo "br /";
echo "remainpoint is:".$ob-remainpoint;
echo "br /";
echo "taskid is:".$ob-taskid;
//var_dump($ob);
//用simplexml_load_string()函数来解析
php 获取 XML 数值
$strxml = file_get_contents(url);
$xml = simplexml_load_string($strxml);
下面就会了吧!!
PHP 获取XML数据
给个小例子:
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数据包
//提取XML数据源
$xmlUrl= '';
echo $xmlUrl;
$curl= curl_init();
curl_setopt($curl, CURLOPT_URL, $xmlUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_HEADER, 0);
$xmlData= curl_exec($curl);
curl_close($curl);
//判断XML编码
if(preg_match("/encoding\=[\'\"](.*?)[\'\"]/i", $xmlData, $encoding)){
$encoding = $encoding[1];
}else{
$encoding = mb_detect_encoding($xmlData, array('ASCII', 'GBK', 'GB2312', 'UTF-8', 'iso-8859-1'));
}
echo $encoding;
//转换XML编码
if($encoding){
$xmlData = preg_replace('/encoding\=[\'\"](.*?)[\'\"]/i', 'encoding="utf-8"', $xmlData);
switch(strtolower($encoding)){
case 'utf-8': break;
case 'iso-8859-1':
$xmlData = utf8_encode($xmlData);
break;
default:
$xmlData = iconv($encoding, 'UTF-8//IGNORE', $xmlData);
break;
}
}
$xmlData= simplexml_load_string($xmlData);
print_r($xmlData);
网站名称:php获取xml中的数据,php 生成xml
本文URL:http://myzitong.com/article/hegeij.html