Hướng dẫn simplexmlelement php example - ví dụ php simplexmlelement

Nhiều ví dụ trong tài liệu tham khảo này yêu cầu một chuỗi XML. Thay vì lặp lại chuỗi này trong mỗi ví dụ, chúng tôi đặt nó vào một tệp mà chúng tôi đưa vào mỗi ví dụ. Tệp bao gồm này được hiển thị trong phần ví dụ sau. Ngoài ra, bạn có thể tạo một tài liệu XML và đọc nó bằng SimplexML_Load_File ().simplexml_load_file().

Ví dụ #1 bao gồm tệp ví dụ.php với chuỗi XML

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>

Tính đơn giản của SimplexML xuất hiện rõ ràng nhất khi một người trích xuất một chuỗi hoặc số từ tài liệu XML cơ bản.

Ví dụ #2 Nhận

include 'example.php';$movies = new SimpleXMLElement($xmlstr);

PHP solves all my web problems
0

PHP solves all my web problems
1

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.

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

include 'example.php';$movies = new SimpleXMLElement($xmlstr);

PHP solves all my web problems
0

PHP solves all my web problems
5

Ví dụ trên sẽ xuất ra:

PHP solves all my web problems

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

PHP solves all my web problems
6

Ví dụ trên sẽ xuất ra:

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.:

Ví dụ #3 Nhận

PHP solves all my web problems
2

Ví dụ #4 Truy cập các yếu tố không duy nhất trong SimplexML

Khi nhiều trường hợp của một yếu tố tồn tại dưới dạng con của một yếu tố cha mẹ đơn lẻ, các kỹ thuật lặp thông thường được áp dụng.array.

PHP solves all my web problems
8

Ví dụ trên sẽ xuất ra:

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

include 'example.php';$movies = new SimpleXMLElement($xmlstr);

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
1

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ví dụ trên sẽ xuất ra:

My favorite movie.PHP: Behind the Parser

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
3

Ví dụ trên sẽ xuất ra:

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

include 'example.php';$movies = new SimpleXMLElement($xmlstr);

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
6

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
7

Ví dụ #4 Truy cập các yếu tố không duy nhất trong SimplexML

Ví dụ trên sẽ xuất ra:

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
9

PHP solves all my web problems
0

My favorite movie.PHP: Behind the Parser
1

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
 

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

My favorite movie.PHP: Behind the Parser
2

PHP solves all my web problems
0

My favorite movie.PHP: Behind the Parser
1

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

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

My favorite movie.PHP: Behind the Parser
5

PHP solves all my web problems
0

My favorite movie.PHP: Behind the Parser
7

Ví dụ trên sẽ xuất ra:

Việc truy cập các phần tử trong tài liệu XML có chứa các ký tự không được phép theo quy ước đặt tên của PHP (ví dụ: dấu gạch nối) có thể được thực hiện bằng cách đóng gói tên phần tử trong niềng răng và apostrophe.

Ví dụ #3 Nhận

PHP solves all my web problems
2

My favorite movie.PHP: Behind the Parser
8

My favorite movie.PHP: Behind the Parser
9

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
0

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
1

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ví dụ #4 Truy cập các yếu tố không duy nhất trong SimplexML

Khi nhiều trường hợp của một yếu tố tồn tại dưới dạng con của một yếu tố cha mẹ đơn lẻ, các kỹ thuật lặp thông thường được áp dụng.

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
3

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
4

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
5

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
6

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
7

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
8

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ghi chú:

Thuộc tính (

PHP solves all my web problems
7 Trong ví dụ trước) không phải là mảng. Chúng là những đối tượng có thể truy cập và có thể truy cập.


 
  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
 

0


 
  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
 

1


 
  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
 

2


 
  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
 

3

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ví dụ #5 Sử dụng các thuộc tính

Cho đến nay, chúng tôi chỉ đề cập đến công việc đọc tên phần tử và giá trị của chúng. SimplexML cũng có thể truy cập các thuộc tính phần tử. Các thuộc tính truy cập của một phần tử giống như các phần tử của một mảng.


 
  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
 

5


 
  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
 

6


 
  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
 

7


 
  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
 

8

Ví dụ #6 so sánh các yếu tố và thuộc tính với văn bản

Để so sánh một phần tử hoặc thuộc tính với một chuỗi hoặc chuyển nó vào một hàm yêu cầu một chuỗi, bạn phải chuyển nó vào một chuỗi bằng cách sử dụng

PHP solves all my web problems
9. Nếu không, PHP coi phần tử là một đối tượng.


 
  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
 

9


 
  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

0

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ví dụ #7 so sánh hai yếu tố

Để so sánh một phần tử hoặc thuộc tính với một chuỗi hoặc chuyển nó vào một hàm yêu cầu một chuỗi, bạn phải chuyển nó vào một chuỗi bằng cách sử dụng

PHP solves all my web problems
9. Nếu không, PHP coi phần tử là một đối tượng.


 
  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

2


 
  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

3

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ví dụ #7 so sánh hai yếu tố

Cho đến nay, chúng tôi chỉ đề cập đến công việc đọc tên phần tử và giá trị của chúng. SimplexML cũng có thể truy cập các thuộc tính phần tử. Các thuộc tính truy cập của một phần tử giống như các phần tử của một mảng.


 
  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

5


 
  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

6


 
  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

7


 
  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

8


 
  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

9

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>
0

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>
1

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>
2

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ví dụ #6 so sánh các yếu tố và thuộc tính với văn bản

Để so sánh một phần tử hoặc thuộc tính với một chuỗi hoặc chuyển nó vào một hàm yêu cầu một chuỗi, bạn phải chuyển nó vào một chuỗi bằng cách sử dụng

PHP solves all my web problems
9. Nếu không, PHP coi phần tử là một đối tượng.

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>
4

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>
5

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>
6

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>
7

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ví dụ #7 so sánh hai yếu tố

Cho đến nay, chúng tôi chỉ đề cập đến công việc đọc tên phần tử và giá trị của chúng. SimplexML cũng có thể truy cập các thuộc tính phần tử. Các thuộc tính truy cập của một phần tử giống như các phần tử của một mảng.

$xmlstr = <<

 
  PHP: Behind the Parser
  
   
    Ms. 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
 


XML;
?>
9

0

Ms. Coder played by Onlivia Actora
Mr. Coder played by El ActÓr
2

Ví dụ #6 so sánh các yếu tố và thuộc tính với văn bản

Để so sánh một phần tử hoặc thuộc tính với một chuỗi hoặc chuyển nó vào một hàm yêu cầu một chuỗi, bạn phải chuyển nó vào một chuỗi bằng cách sử dụng

PHP solves all my web problems
9. Nếu không, PHP coi phần tử là một đối tượng.

2

3

4

5