下面是“php+Ajax处理xml与json格式数据的方法示例”的详细攻略。
一、xml数据处理
1.使用DOM进行xml数据解析
使用DOM可以很容易地解析xml数据,下面是一个简单的示例。
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="JAVA">
<title>Thinking in Java</title>
<author>Bruce Eckel</author>
<year>1998</year>
<price>49.95</price>
</book>
</bookstore>';
$dom = new DOMDocument();
$dom->loadXML($xml);
$books = $dom->getElementsByTagName('book');
foreach ($books as $book) {
$category = $book->getAttribute('category');
$title = $book->getElementsByTagName('title')->item(0)->nodeValue;
$author = $book->getElementsByTagName('author')->item(0)->nodeValue;
$year = $book->getElementsByTagName('year')->item(0)->nodeValue;
$price = $book->getElementsByTagName('price')->item(0)->nodeValue;
echo "$title by $author ($category, $year) - $price\n";
}
?>
2.使用SimpleXML进行xml数据解析
使用SimpleXML可以更方便地解析xml数据,下面是一个简单的示例。
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="JAVA">
<title>Thinking in Java</title>
<author>Bruce Eckel</author>
<year>1998</year>
<price>49.95</price>
</book>
</bookstore>';
$sxml = simplexml_load_string($xml);
foreach ($sxml->book as $book) {
$category = (string)$book['category'];
$title = (string)$book->title;
$author = (string)$book->author;
$year = (string)$book->year;
$price = (string)$book->price;
echo "$title by $author ($category, $year) - $price\n";
}
?>
二、json数据处理
使用json_decode可以将json数据解析为PHP对象或数组。
<?php
$json = '{
"name": "Alice",
"age": 18,
"sex": "Female",
"hobbies": ["music", "movies", "reading"],
"friends": [
{"name": "Bob", "age": 20, "sex": "Male"},
{"name": "Cathy", "age": 19, "sex": "Female"}
]
}';
$obj = json_decode($json); // 解析json为对象
$arr = json_decode($json, true); // 解析json为关联数组
echo $obj->name; // 输出 Alice
echo $arr['name']; // 输出 Alice
?>
三、使用Ajax进行xml和json数据的异步请求
使用Ajax可以在不刷新页面的情况下向服务器请求并接收xml或json数据,下面是一个示例。
1.发送xml数据请求,并将响应数据解析为HTML代码显示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>XmlHttpRequest Demo</title>
<script>
function loadXMLDoc() {
var xmlhttp;
if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var dom = xmlhttp.responseXML;
var books = dom.getElementsByTagName("book");
var html = '';
for (var i = 0; i < books.length; i++) {
var title = books[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
var author = books[i].getElementsByTagName("author")[0].childNodes[0].nodeValue;
var category = books[i].getAttribute("category");
var year = books[i].getElementsByTagName("year")[0].childNodes[0].nodeValue;
var price = books[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
html += '<h3>' + title + '</h3><p>' + author + ' (' + category + ', ' + year + ') - ' + price + '</p>';
}
document.getElementById("content").innerHTML = html;
}
}
xmlhttp.open("GET", "books.xml", true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>Books List</h2>
<button onclick="loadXMLDoc()">Get Books</button>
<div id="content"></div>
</body>
</html>
2.发送json数据请求,并将响应数据解析为HTML代码显示
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>XmlHttpRequest Demo</title>
<script>
function loadJSONDoc() {
var xmlhttp;
if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
var friends = data.friends;
var html = '';
for (var i = 0; i < friends.length; i++) {
var name = friends[i].name;
var age = friends[i].age;
var sex = friends[i].sex;
html += '<p>' + name + ' (' + sex + ', ' + age + ')</p>';
}
document.getElementById("content").innerHTML = html;
}
}
xmlhttp.open("GET", "friends.json", true);
xmlhttp.send();
}
</script>
</head>
<body>
<h2>Friends List</h2>
<button onclick="loadJSONDoc()">Get Friends</button>
<div id="content"></div>
</body>
</html>
以上就是使用php+Ajax处理xml与json格式数据的方法示例的完整攻略,希望能对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:php+Ajax处理xml与json格式数据的方法示例 - Python技术站