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">bN2dncgGHIofhaPanZ2dnUVZ8tvinZ2d@giganews.com</segment>         <segment bytes="608371" number="2">bN2dncsGHIoahaPanZ2dnUVZ8tvinZ2d@giganews.com</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
Share on other sites

Hello,

 

Can you show me the "dump" of $xml.

 

From $xml = simplexml_load_file("$xml_file");

 

My initial thought is to do

 

if(is_array($data['element'])){
//we have children
}

 

but not sure if simplexml setups children nodes as multilevel arrays.

 

Hence the dump haha :)

 

Link to comment
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">bN2dncgGHIofhaPanZ2dnUVZ8tvinZ2d@giganews.com</segment>         <segment bytes="608371" number="2">bN2dncsGHIoahaPanZ2dnUVZ8tvinZ2d@giganews.com</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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.