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 Node — Finds 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ố 8Một không gian tên XML.
isPrefix
Nếu isPrefix
là
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ả
6foreach [$item[id="1234"]->children[] as $property] {
foreach [$item[id="1234"]->children[] as $property] {
7foreach [$item[id="1234"]->children[] as $property] {
8foreach [$item[id="1234"]->children[] as $property] {
9Ví 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 son0
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 son1
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son2
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son3
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son4
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 son5
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son6
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son7
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son8
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 son9
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 son8
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ố
$namespaceOrPrefix
0
$namespaceOrPrefix
1
$namespaceOrPrefix
2
$namespaceOrPrefix
3
$namespaceOrPrefix
4
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son3
$namespaceOrPrefix
6________số 8 ¶
14 năm trước
$namespaceOrPrefix
7
$namespaceOrPrefix
8
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son8
________số 8 ¶
Thông số
null
0
null
1
null
2
null
3________số 8 ¶
publicsimplexmlelement :: trẻ em [? chuỗi $namespaceOrPrefix
= null
, bool $isPrefix
= false
]:? SimplexMlelement
null
4
null
5
null
6
null
7
null
8
null
9
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son8
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ố
$isPrefix
1
$isPrefix
2
$isPrefix
3
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son8
________số 8 ¶
17 năm trước
$isPrefix
5
$isPrefix
6
$isPrefix
7
$isPrefix
8
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son3
false
0zyxwvu tại người dùng dot sourceforge dot net ¶ ¶
18 năm trước
false
1
false
2
false
3
false
4
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son8
Rodmen tại Gmail Dot Com ¶ ¶
13 năm trước
false
6
false
7
The person begot a son who begot a daughter; The person begot a daughter who begot a son; and that son begot a son8
Crescentfreshpot tại Yahoo Dot Com ¶ ¶
14 năm trước
false
9