Jump to content

[SOLVED] XML Reading


graham23s

Recommended Posts

Hi Guys,

 

i am trying to parse the information from an .xml or .nzb file which is practically the same as far as i can read a test file is here:

 

<?php
<file poster="graham23s" date="1195251842" subject="TEST SUBJECT">     <groups>         <group>alt.binaries.test</group>     </groups>     <segments>         <segment bytes="651504" number="1">[email protected]</segment>         <segment bytes="608371" number="2">[email protected]</segment>     </segments>     </file> 
?>

 

to get the information i basically use:

 

<?php
// xml testing //
$xml_file = "5300538.xml";

// load the nzb //
$xml = simplexml_load_file("$xml_file");

// test //
foreach ($xml as $details)
{
  print("<b>Date</b> - ") . $details['date'];
  print("<br />");
  print("<b>Posted By</b> - ") . $details['poster'];
  print("<br />");
  print("<b>Subject</b> - ") . $details['subject'];
  print("<br />");
  print("<b>Group</b> - ") . $details['groups'];
  print("<br />");
  print("<br />");
  print("<hr />");
}
?>

 

this works great up untill i try to get the child nodes, the group for example, its in groups->group, i foreach all the lines to get them all, i was wondering how best to get the child nodes

 

any input would be great

 

cheers

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/100795-solved-xml-reading/
Share on other sites

<?php 
$str = '<file poster="graham23s" date="1195251842" subject="TEST SUBJECT">     <groups>         <group>alt.binaries.test</group>     </groups>     <segments>         <segment bytes="651504" number="1">[email protected]</segment>         <segment bytes="608371" number="2">[email protected]</segment>     </segments>     </file>';

$xml = simplexml_load_string($str);


  print("<b>Date</b> - " . $xml['date']);
  print("<br />");
  print("<b>Posted By</b> - " . $xml['poster']);
  print("<br />");
  print("<b>Subject</b> - " . $xml['subject']);
  print("<br />");

foreach ($xml->groups as $gp)
{
  print("<b>Group</b> - " . $gp->group);
  print("<br />");
  print("<br />");
  print("<hr />");
}

?> 

Link to comment
https://forums.phpfreaks.com/topic/100795-solved-xml-reading/#findComment-515514
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.