Jump to content

XML Grouping


radox

Recommended Posts

I'm using xml_parse_into_struct to get all my elements, but now I need to group them. For example, here's the XML

 

<categories>

            <category id="12345">

      <title>category title 1</title>

      <tips>

          <tip id="0">

              <title>hello world</title>

              <content>some content</content>

          </tip>

          <tip id="1">

              <title>foo</title>

              <content>foo content</content>

          </tip>

        </tips>

    </category>

    <category id="7890">

    <title> category title 2</title>

        <tips>

          <tip id="2">

              <title>another tip title</title>

              <content>blah blah</content>

          </tip>

          <tip id="3">

              <title>another tip title</title>

              <content>blah blah</content>

          </tip>

 

        </tips>

      </category>

</categories> 

 

 

How would I just grab the tip titles for the 2nd category (7890)? Hope I'm making sense.

I can loop through the array using foreach, but have no way to determine if a tip belongs to the 1st or 2nd category. Ideas??

 

Please help!

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/177157-xml-grouping/
Share on other sites

  • 2 weeks later...

Here's the solution:

 

//open xml files

$xmlperfile = 'file.xml';

 

//open personal xml file for processing

$perfp = fopen($xmlperfile, "r") or die("Could not open file");

$perdata = fread($perfp, filesize($xmlperfile)) or die("Could not read file");

 

//create xml parser and extract personal data

$xmlperparser = xml_parser_create();

xml_parser_set_option($xmlperparser, XML_OPTION_CASE_FOLDING, 0);

xml_parser_set_option($xmlperparser, XML_OPTION_SKIP_WHITE, 1);

xml_parse_into_struct($xmlperparser,$perdata,$pervals,$perindex);

xml_parser_free($xmlperparser);

 

//close both xml file reads

fclose($perfp);

 

 

$tipcat=0;

foreach($pervals as $element){

if($element['level']==6){

if($element['tag']=='Title'){

$pertiptitle[$tipcat][]=$element['value'];

}

if($element['tag']=='Content'){

$pertipcontent[$tipcat][]=$element['value'];

}

}

if($element['tag']=='Category'&&$element['type']=='close'){

$tipcat++;

}

}

Link to comment
https://forums.phpfreaks.com/topic/177157-xml-grouping/#findComment-940672
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.