Hướng dẫn php simplexml get all child nodes - php simplexml lấy tất cả các nút con

Tôi có một câu hỏi và câu trả lời chắc chắn đơn giản, nhưng nó chỉ thiếu hiểu biết từ phía tôi.

Tôi có một tệp XML với giao diện sau (ví dụ ngắn)


    
        4402
    
    
    
    
    
    
    
    
    
    
    
    
        03-6756711
    


   ...

Vấn đề của tôi là, tôi muốn đọc tất cả các thuộc tính từ một mục với ID 1234 với thuộc tính và giá trị của chúng, nếu tồn tại, trong một mảng.

Tôi biết cách truy cập các mục nhất định với XPath. (Cảm ơn cộng đồng Stackoverflow tuyệt vời này :))

Nhưng làm thế nào tôi có thể sử dụng chức năng trẻ em () chỉ cho một mặt hàng nhất định?

Như thế này

foreach ($item[id="1234"]->children() as $property) {

Cảm ơn bạn rất nhiều!

(Php 5, Php 7, Php 8)

SimplexMlelement :: Trẻ em - Tìm trẻ em của NodeFinds children of given node

Sự mô tả

publicsimplexmlelement :: trẻ em (? chuỗi $namespaceOrPrefix = null, bool $isPrefix = false):? SimplexMlelement SimpleXMLElement::children(?string $namespaceOrPrefix = null, bool $isPrefix = false): ?SimpleXMLElement

Lưu ý: SimplexML đã đưa ra một quy tắc thêm các thuộc tính lặp vào hầu hết các phương thức. Chúng không thể được xem bằng var_dump () hoặc bất cứ thứ gì khác có thể kiểm tra các đối tượng.: SimpleXML has made a rule of adding iterative properties to most methods. They cannot be viewed using var_dump() or anything else which can examine objects.

Thông số

________số 8

Một không gian tên XML.

isPrefix

Nếu isPrefix

foreach ($item[id="1234"]->children() as $property) {
1, namespaceOrPrefix sẽ được coi là tiền tố. Nếu false, namespaceOrPrefix sẽ được coi là URL không gian tên.
foreach ($item[id="1234"]->children() as $property) {
1
, namespaceOrPrefix will be regarded as a prefix. If false, namespaceOrPrefix will be regarded as a namespace URL.

Trả về giá trị

Trả về một phần tử simplexmlelement, cho dù nút có con hay không, trừ khi nút đại diện cho một thuộc tính, trong trường hợp đó null được trả về.SimpleXMLElement element, whether the node has children or not, unless the node represents an attribute, in which case null is returned.

Ví dụ

Ví dụ số 1 đi qua một giả giả

foreach ($item[id="1234"]->children() as $property) {
6

foreach ($item[id="1234"]->children() as $property) {
7

foreach ($item[id="1234"]->children() as $property) {
8

foreach ($item[id="1234"]->children() as $property) {
9

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

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son

Ví dụ #2 sử dụng không gian tên

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
0

int(0)
int(2)
int(2)
int(0)
int(1)

Xem thêm

  • Simplexmlelement :: Count () - đếm con cái của một yếu tố
  • Count () - Đếm tất cả các phần tử trong một mảng hoặc trong một đối tượng có thể đếm được

aero ¶

15 năm trước

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
1

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
2

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
3

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
4

Sebastian ¶

17 năm trước

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
5

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
6

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
7

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
8

transglobe tại gmx dot de ¶

14 năm trước

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
9

int(0)
int(2)
int(2)
int(0)
int(1)
0

int(0)
int(2)
int(2)
int(0)
int(1)
1

int(0)
int(2)
int(2)
int(0)
int(1)
2

(Php 5, Php 7, Php 8)

SimplexMlelement :: Trẻ em - Tìm trẻ em của Node

int(0)
int(2)
int(2)
int(0)
int(1)
3

int(0)
int(2)
int(2)
int(0)
int(1)
4

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
8

Sự mô tả

publicsimplexmlelement :: trẻ em (? chuỗi $namespaceOrPrefix = null, bool $isPrefix = false):? SimplexMlelement

int(0)
int(2)
int(2)
int(0)
int(1)
6

int(0)
int(2)
int(2)
int(0)
int(1)
7

int(0)
int(2)
int(2)
int(0)
int(1)
8

int(0)
int(2)
int(2)
int(0)
int(1)
9

Lưu ý: SimplexML đã đưa ra một quy tắc thêm các thuộc tính lặp vào hầu hết các phương thức. Chúng không thể được xem bằng var_dump () hoặc bất cứ thứ gì khác có thể kiểm tra các đối tượng.

Thông số

$namespaceOrPrefix0

$namespaceOrPrefix1

$namespaceOrPrefix2

$namespaceOrPrefix3

$namespaceOrPrefix4

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
3

$namespaceOrPrefix6

________số 8

14 năm trước

$namespaceOrPrefix7

$namespaceOrPrefix8

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
8

________số 8

Thông số

null0

null1

null2

null3

________số 8

publicsimplexmlelement :: trẻ em (? chuỗi $namespaceOrPrefix = null, bool $isPrefix = false):? SimplexMlelement

null4

null5

null6

null7

null8

null9

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
8

Lưu ý: SimplexML đã đưa ra một quy tắc thêm các thuộc tính lặp vào hầu hết các phương thức. Chúng không thể được xem bằng var_dump () hoặc bất cứ thứ gì khác có thể kiểm tra các đối tượng.

Thông số

$isPrefix1

$isPrefix2

$isPrefix3

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
8

________số 8

17 năm trước

$isPrefix5

$isPrefix6

$isPrefix7

$isPrefix8

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
3

false0

zyxwvu tại người dùng dot sourceforge dot net ¶

18 năm trước

false1

false2

false3

false4

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
8

Rodmen tại Gmail Dot Com ¶

13 năm trước

false6

false7

The person begot a son who begot a daughter; The person
begot a daughter who begot a son; and that son begot a son
8

Crescentfreshpot tại Yahoo Dot Com ¶

14 năm trước

false9