SimplexML là một phần mở rộng PHP cho phép chúng tôi dễ dàng thao tác và lấy dữ liệu XML.
Trình phân tích cú pháp SimplexML
SimplexML là một trình phân tích cú pháp dựa trên cây.
SimplexML cung cấp một cách dễ dàng để có được tên, thuộc tính và nội dung văn bản của một phần tử nếu bạn biết cấu trúc hoặc bố cục của tài liệu XML.
SimplexML biến một tài liệu XML thành cấu trúc dữ liệu mà bạn có thể lặp lại giống như một tập hợp các mảng và đối tượng.
So với DOM hoặc Trình phân tích cú pháp người nước ngoài, SimplexML có ít dòng mã hơn để đọc dữ liệu văn bản từ một phần tử.
Cài đặt
Từ PHP 5, các hàm simplexml là một phần của lõi PHP. Không cần cài đặt để sử dụng các chức năng này.
PHP Simplexml - Đọc từ Chuỗi
Hàm Php simplexml_load_string[]
được sử dụng để đọc dữ liệu XML từ một chuỗi.
Giả sử chúng ta có một biến chứa dữ liệu XML, như thế này:
$ myxmldata = "tove jani nhắc nhở đừng quên tôi vào cuối tuần này!";
"
Tove
Jani
Reminder
Don't forget me this weekend!
";
Ví dụ dưới đây cho thấy cách sử dụng hàm simplexml_load_string[]
để đọc dữ liệu XML từ chuỗi:
Thí dụ
print_r[$xml];
?>
Chạy ví dụ »
Đầu ra của mã ở trên sẽ là:
Đối tượng SimplexMlelement [[to] => tove [from] => jani [tiêu đề] => nhắc nhở [Body] => Đừng quên tôi vào cuối tuần này!]
Mẹo xử lý lỗi: Sử dụng chức năng LibXML để truy xuất tất cả các lỗi XML khi tải tài liệu và sau đó lặp lại các lỗi. Ví dụ sau cố gắng tải chuỗi XML bị hỏng: Use the libxml functionality to retrieve all XML errors when loading the document and then iterate over the errors. The following example tries to load a broken XML string:
Thí dụ
if [$xml === false] {
echo "Failed loading XML: ";
foreach[libxml_get_errors[] as $error] {
echo "
", $error->message;
}
} else {
print_r[$xml];
}
?>
Chạy ví dụ »
Đầu ra của mã ở trên sẽ là:
Đối tượng SimplexMlelement [[to] => tove [from] => jani [tiêu đề] => nhắc nhở [Body] => Đừng quên tôi vào cuối tuần này!]
Opening and ending tag mismatch: user line 3 and wronguser
Opening and ending tag mismatch: email line 4 and wrongemail
Mẹo xử lý lỗi: Sử dụng chức năng LibXML để truy xuất tất cả các lỗi XML khi tải tài liệu và sau đó lặp lại các lỗi. Ví dụ sau cố gắng tải chuỗi XML bị hỏng:
$ xml = simplexml_load_string [$ myxmldata]; if [$ xml === false] {& nbsp; echo "tải thất bại xml:"; & nbsp; foreach [libxml_get_errors [] là $ error] {& nbsp; & nbsp; & nbsp; echo "", $ error-> tin nhắn; & nbsp; }} khác {& nbsp; print_r [$ xml];}?>
Tải thất bại XML: Mở và kết thúc thẻ không phù hợp: Dòng người dùng 3 và Saiseropening và kết thúc thẻ không phù hợp: dòng email 4 và sai
PHP Simplexml - Đọc từ tệp
Tove
Jani
Reminder
Don't forget me this weekend!
Hàm Php simplexml_load_file[]
được sử dụng để đọc dữ liệu XML từ một tệp.
Thí dụ
Chạy ví dụ »
Đầu ra của mã ở trên sẽ là:
Đối tượng SimplexMlelement [[to] => tove [from] => jani [tiêu đề] => nhắc nhở [Body] => Đừng quên tôi vào cuối tuần này!]
Mẹo xử lý lỗi: Sử dụng chức năng LibXML để truy xuất tất cả các lỗi XML khi tải tài liệu và sau đó lặp lại các lỗi. Ví dụ sau cố gắng tải chuỗi XML bị hỏng: The next chapter shows how to get/retrieve node values from an XML file with SimpleXML!
$ xml = simplexml_load_string [$ myxmldata]; if [$ xml === false] {& nbsp; echo "tải thất bại xml:"; & nbsp; foreach [libxml_get_errors [] là $ error] {& nbsp; & nbsp; & nbsp; echo "", $ error-> tin nhắn; & nbsp; }} khác {& nbsp; print_r [$ xml];}?>
Tải thất bại XML: Mở và kết thúc thẻ không phù hợp: Dòng người dùng 3 và Saiseropening và kết thúc thẻ không phù hợp: dòng email 4 và sai
Max K.simplexml_load_file[].
Josef ¶
PHP solves all my web problems1
1 năm trước
PHP tại Keith Tyler Dot Com ¶
PHP solves all my web problems3
PHP solves all my web problems4
PHP solves all my web problems5
Ví dụ trên sẽ xuất ra:
So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary.
radams tại CirclePix Com ¶
13 năm trước
PHP solves all my web problems3
PHP solves all my web problems4
PHP solves all my web problems9
Ví dụ trên sẽ xuất ra:
PHP solves all my web problems
Làm thế nào bạn sẽ phân tích một tài liệu XML bằng PHP?
Hàm SimpleXML_LOAD_STRING [] được sử dụng để phân tích XML đã cho và sau đó đối tượng XML có thể được sử dụng để truy cập dữ liệu XML.
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr0
Ví dụ trên sẽ xuất ra:
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr
Php phân tích cú pháp XML là gì?:
Trình phân tích cú pháp XML là một chương trình dịch XML một tài liệu XML thành cấu trúc cây Dom như tài liệu. CDATA được sử dụng để bỏ qua các ký tự đặc biệt khi phân tích các tài liệu XML. PHP sử dụng simplexml_load_file để đọc các tài liệu XML và trả về kết quả dưới dạng mảng số. Lớp PHP DomDocument để tạo các tệp XML.
XML được xử lý như thế nào trong PHP?
Php XML Phân tích cú pháp Giới thiệu Các chức năng XML cho phép bạn phân tích cú pháp, nhưng không xác thực, các tài liệu XML. XML là một định dạng dữ liệu để trao đổi tài liệu có cấu trúc được tiêu chuẩn hóa. Thông tin thêm về XML có thể được tìm thấy trong hướng dẫn XML của chúng tôi. Phần mở rộng này sử dụng trình phân tích cú pháp XML của người nước ngoài.array.
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr2
Ví dụ trên sẽ xuất ra:
Làm thế nào bao gồm tệp XML trong PHP?
$ DOM2 = DomDocument mới; $ dom2-> tải ['hiện tạifile. xml']; $ dom2-> documentEuity-> appendChild [$ DOM2-> Nhập khẩu [$ Fragment, true]];
PHP solves all my web problems3
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr5
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
Ví dụ trên sẽ xuất ra:
My favorite movie.PHP: Behind the Parser
Ví dụ #7 so sánh hai yếu tố
Hai SimplexMlelements được coi là khác nhau ngay cả khi chúng chỉ vào cùng một yếu tố.
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr7
Ví dụ trên sẽ xuất ra:
Ví dụ #8 sử dụng XPath
SimplexML bao gồm hỗ trợ XPath tích hợp. Để tìm tất cả các yếu tố
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr8:
PHP solves all my web problems3
My favorite movie.PHP: Behind the Parser0
My favorite movie.PHP: Behind the Parser1
'
My favorite movie.PHP: Behind the Parser2' phục vụ như một ký tự đại diện. Để chỉ định các đường dẫn tuyệt đối, hãy bỏ qua một trong những dấu gạch chéo.
Ví dụ trên sẽ xuất ra:
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr
Ví dụ #9 Các giá trị cài đặt
Dữ liệu trong SimplexML không cần phải không đổi. Đối tượng cho phép thao tác tất cả các yếu tố của nó.
My favorite movie.PHP: Behind the Parser3
PHP solves all my web problems4
My favorite movie.PHP: Behind the Parser5
Ví dụ trên sẽ xuất ra:
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5
Ví dụ #10 Thêm các thành phần và thuộc tính
SimplexML đã có khả năng dễ dàng thêm trẻ em và thuộc tính.
My favorite movie.PHP: Behind the Parser6
PHP solves all my web problems4
My favorite movie.PHP: Behind the Parser5
Ví dụ trên sẽ xuất ra:
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG
Ví dụ #11 khả năng tương tác DOM
PHP có một cơ chế để chuyển đổi các nút XML giữa các định dạng SimplexML và DOM. Ví dụ này cho thấy cách người ta có thể thay đổi một phần tử DOM thành SimplexML.
My favorite movie.PHP: Behind the Parser9
PHP solves all my web problems4
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr1
Ví dụ trên sẽ xuất ra:
Rowan Dot Collins tại Gmail Dot Com ¶ ¶
7 năm trước
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr2
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr3
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr4
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr5
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
Jishcem tại gmail dot com ¶ ¶
9 năm trước
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr7
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr8
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr9
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 50
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 51
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 52
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
Ẩn danh ¶ ¶
5 năm trước
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 54
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 55
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 56
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 57
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
tức là dot raymond tại gmail dot com ¶ ¶
12 năm trước
PHP: Behind the Parser Miss Coder Onlivia Actora Mr. Coder El ActÓr So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 59
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG0
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG1
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG2
GKOKMDAM tại Zonnet Dot NL ¶ ¶
11 năm trước
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG3
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG4
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
KDOS ¶ ¶
11 năm trước
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG6
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG7
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
KDOS ¶ ¶
12 năm trước
PHP: Behind the Parser Ms. Coder Onlivia Actora Mr. Coder El ActÓr Mr. ParserJohn Doe So, this language. It's like, a programming language. Or is it a scripting language? All is revealed in this thrilling horror spoof of a documentary. PHP solves all my web problems 7 5 PG9
simplexml_load_string[]
0
simplexml_load_string[]
1
simplexml_load_string[]
2
simplexml_load_string[]
3
simplexml_load_string[]
4
simplexml_load_string[]
5
simplexml_load_string[]
6
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
GKOKMDAM tại Zonnet Dot NL ¶ ¶
11 năm trước
simplexml_load_string[]
8
simplexml_load_string[]
9
simplexml_load_string[]
0
simplexml_load_string[]
1
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
KDOS ¶ ¶
12 năm trước
simplexml_load_string[]
3
simplexml_load_string[]
4
Ms. Coder played by Onlivia Actora Mr. Coder played by El ActÓr6
GKOKMDAM tại Zonnet Dot NL ¶ ¶
11 năm trước
simplexml_load_string[]
6
simplexml_load_string[]
7
simplexml_load_string[]
8
simplexml_load_string[]
9