High score online help XML parsing this post was last edited by HUENKE at 15:58:22
Xml
The structure is nodes. There are nodes under the node.
I want to create a triple loop
Foreach ()
{
Company name
Foreach ()
{
Save department name
Foreach ()
{Store leaf name}
}
}
How can I resolve it,
Reply to discussion (solution)
Have you used SimpleXML?
SimpleXML is not parsed.
Can you perform simple debugging?
What data format do you want ?, Organize the data structure by yourself.
XML; $ xml = new SimpleXMLElement ($ string); $ result = $ xml-> xpath ('// node/@ name'); while (list (, $ node) = each ($ result) {echo '@ name:', $ node, "\ n ";} $ result = $ xml-> xpath ('// node/@ ID'); while (list (, $ node) = each ($ result) {echo' @ id: ', $ node, "\ n" ;}$ result = $ xml-> xpath (' // node [@ type = 1]/.. /@ ID'); while (list (, $ node) = each ($ result) {echo '@ department's parent_id:', $ node, "\ n ";} $ result = $ xml-> xpath ('// node [@ Type = 2]/... /@ ID'); while (list (, $ node) = each ($ result) {echo '@ Leaf's parent_id:', $ node, "\ n" ;}?>
$ S = <XML
XML; $ xml = simplexml_load_string ($ s); foreach ($ xml-> node as $ node) {foreach ($ node-> attributes () as $ k => $ v) echo "$ k => $ v \ n"; foreach ($ node-> node as $ node) {foreach ($ node-> attributes () as $ k => $ v) echo "$ k => $ v \ n"; foreach ($ node-> node as $ node) {foreach ($ node-> attributes () as $ k => $ v) echo "$ k => $ v \ n ";}}}
C5569e2c-0f4a-4ec2-86ba-736b79eca18c
Name => Company
Type => 0
Id => 4f2b55e9-e10a-496b-8bca-60e6f26daee5
Name => department 1
Type => 1
Id = & gt; 363
Name => leaf 1
Type => 2
Playurl => http: // XXXXXXX363
Id => 0c5ce42f-ba31-4b7a-8173-79ecae4a73ca
Name => Department 2
Type => 1
Id = & gt; 241
Name => leaf 1
Type => 2
Playurl => http: // xxxxxxx = 241
Id = & gt; 356
Name => leaf 1
Type => 2
Playurl => xxxxxxxxx = 356
Is the original tree structure.
For example
Company
Department 1
Leaf 1
Department 2
....
Let's give you another one. modify the tree by yourself.
XML; $ xml = new SimpleXMLElement ($ string); // var_export ($ xml); $ xmlIterator = new inline (new SimpleXMLIterator ($ string), RecursiveIteratorIterator: SELF_FIRST ); foreach ($ xmlIterator as $ nodeName => $ node) {echo str_repeat ('-', (int) $ node ['type']); echo $ node ['id'], ',', $ node ['name'], PHP_EOL;}?>
Above #6
$ Xml = new SimpleXMLElement ($ string );
This sentence can be deleted.