High score online help XML parsing-php Tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.