PHP - XML
1. The ExtensibleMarkup Language (XML): là ngôn ngữ đánh dấu với mục tiêu là đơn giản hóa
việc chia sẻ dữ liệu giữa các hệ thống khác nhau
Cú pháp XML cơ bản cho một phần tử là
<tên thuộc_tính="giá trị">nội dung</tên>
Cấu trúc XML đơn giản:
<?xml version="1.0"?>
<message>Hello, World!</message>
Ví dụ: Hãy tạo tệp tin XML có nội dung như sau:
Đây là tệp tin XML:
<?xml version="1.0" encoding="utf-8"?>
<library>
<book isbn="0345342968">
<title>Fahrenheit 451</title>
<author>R. Bradbury</author>
<publisher>Del Rey</publisher>
</book>
<book isbn="0048231398">
<title>The Silmarillion</title>
<author>J.R.R. Tolkien</author>
<publisher>G. Allen . Unwin</publisher>
</book>
<book isbn="0451524934">
<title>1984</title>
Title Author Publisher ISBN
TheMoon Is a HarshMistress R. A. Heinlein Orb 312863551
Fahrenheit 451 R. Bradbury Del Rey 345342968
The Silmarillion J.R.R. Tolkien G Allen & Unwin 48231398
1984 G. Orwell Signet 451524934
Frankenstein M. Shelley Bedford 031219126X<author>G. Orwell</author>
<publisher>Signet</publisher>
</book>
<book isbn="031219126X">
<title>Frankenstein</title>
<author>M. Shelley</author>
<publisher>Bedford</publisher>
</book>
<book isbn="0312863551">
<title>The Moon Is a Harsh Mistress</title>
<author>R. A. Heinlein</author>
<publisher>Orb</publisher>
</book>
</library>
2. SimpleXML
• Parsing XML Documents
// Load an XML string
$xmlstr = file_get_contents(’library.xml’);
$library = simplexml_load_string($xmlstr);
hoặc dùng
// Load an XML file
$library = simplexml_load_file(’library.xml’);
• Truy cập Children and Attributes
<?php
// Load an XML string
$library = new SimpleXMLElement("library.xml", NULL, true);
foreach ($library->book as $book)
{
echo $book["isbn"] . "<br>";
echo $book->title . "<br>";
echo $book->author . "<br>";echo $book->publisher . "<br><br>";
}
?>
• Để hiển thị các thuộc tính và giá trị: dùng hàm SimpleXMLElement::children() và
SimpleXMLElement::attributes(), SimpleXMLElement::getName()
foreach ($library->children() as $child)
{
echo $child->getName() . ":\n";
// Get attributes of this element
foreach ($child->attributes() as $attr)
{
echo " " . $attr->getName() . ": " . $attr . "<br>";
}
// Get children
foreach ($child->children() as $subchild)
{
echo " " . $subchild->getName() . ": " . $subchild . "<br>";
}
echo "<br>";
3. XPath Queries: được dùng để lấy ra values của các Elements hay Attributes XPath đóng một
vai trò quan trọng trong việc trao đổi dữ liệu giữa các computers hay giữa các chương trình ứng
dụng vì nó cho phép ta lựa chọn hay sàng lọc ra những tin tức nào mình muốn để trao đổi hay
hiển thị.
$library = new SimpleXMLElement("library.xml", NULL, true);
$results = $library->xpath("/library/book/title");
foreach ($results as $title)
{
echo $title . "<br>";
}
// Search the first child element
$results = $library->book[0]->xpath("title");foreach ($results as $title)
{
echo $title . "<br>";
}
• Thay đổi tài liệu XML
$library = new SimpleXMLElement("library.xml", NULL, true);
$book = $library->addChild("book");
$book->addAttribute("isbn", "0812550706");
$book->addChild("title", "Ender's Game");
$book->addChild("author", "Orson Scott Card");
$book->addChild("publisher", "Tor Science Fiction");
header("Content-type: text/xml");
echo $library->asXML();
• Xóa một phần tử:
$library->book[0] = NULL;
• Làm việc với Namespaces:
<?xml version="1.0"?>
<library xmlns="http://example.org/library"
xmlns:meta="http://example.org/book-meta"
xmlns:pub="http://example.org/publisher"
xmlns:foo="http://example.org/foo">
<book meta:isbn="0345342968">
<title>Fahrenheit 451</title>
<author>Ray Bradbury</author>
<pub:publisher>Del Rey</pub:publisher>
</book>
</library>$namespaces = $library->getDocNamespaces();
foreach ($namespaces as $key => $value)
{
echo "{$key} => {$value}<br>";
}
Bài tập:
1. Tìm hiểu DOM
2. Tìm hiểu Drupal XML-RPC
No comments:
Post a Comment