Jump to content

[SOLVED] Parsing XML


gijew

Recommended Posts

Haven't played around with this much but after some reading I've done a few things involving XML, I'm just not sure if they're the right / easiest way.  That's where you guys come in.

Pulling data from a database and creating an XML document.
Data should be stored like this...

GROUP 1
  PROPERTY 1
  PROPERTY 1 DESCRIPTION

  PROPERTY 2
  PROPERTY 2 DESCRIPTION

GROUP 2
  PROPERTY 1
  PROPERTY 1 DESCRIPTION

So this repeats and it changes for each group.  That's the gist there.  I've managed to throw together some functional PHP that will create the XML file and I'll sum up the output (not sure if this is the best way).

[code]
<eligible_properties>
  <property_group>
    <group>Auto</group>
    <properties>
      <property>Auto Body Shop</property>
      <description>No description available for this property.</description>
      <property>Auto Rental Yards</property>
      <description>These loans should be sought from your local bank as they require frequent inspections for loan advances.</description>
      <property>Auto Service Center</property>
      <description>&lt;p&gt;Gasoline and diesal tanks are not allowed.&lt;/p&gt;&lt;p&gt;Waste oil storage tanks should have secondary containment.&lt;/p&gt;&lt;p&gt;Auto lifts should ideally be above ground&lt;/p&gt;</description>
    </properties>
  </property_group>
<eligible_properties>
[/code]

This repeats over a dozen or so groups with miscellaneous properties in each group.  Where I'm having trouble right now is parsing the XML in a nicely readable bit of text.  I'm not going to show the output because it's not important.  What's happening is that for each group I'm getting the first property of each group.  So instead of parsing through the list of properties for that particular group it's taking the first of each (I think that sums it up).

Here's the code I have worked on for parsing the XML.

[code]
<?php
$doc = new DOMDocument();
$doc->load('rss_eligible_properties.xml');

$GROUPS = $doc->getElementsByTagName('property_group');

echo '<table>';

ForEach ($GROUPS AS $GROUP) {
  $theGroups = $GROUP->getElementsByTagName('group');
  $theGroup = $theGroups->item(0)->nodeValue;
  echo '<tr><td><b>'.$theGroup.'</b></td></tr>';

  $PROPERTIES = $doc->getElementsByTagName('properties');
  ForEach ($PROPERTIES AS $PROPERTY) {
    $theProperties  = $PROPERTY->getElementsByTagName('property');
    $theProperty = $theProperties->item(0)->nodeValue;

    $theDescriptions = $PROPERTY->getElementsByTagName('description');
    $theDescription = $theDescriptions->item(0)->nodeValue;

    echo '<tr><td style="padding-left:20px;">
          <div><i><u>'.$theProperty.'</u></i></div>
          <div>'.$theDescription.'</div>
          </td></tr>';
  }
}
echo '</table>';
?>
[/code]

Any insight here would be great.  Thanks a lot!
Link to comment
https://forums.phpfreaks.com/topic/32122-solved-parsing-xml/
Share on other sites

Thanks for the suggestion and I WILL check it out.  I'm a semi-newb here to a lot of XML type functions and this was my first real stab at doing it on my own...I pretty much did it on my own too :P

For future reference (if anyone every comes to visit this thread) this was my solution...

I changed the XML output.
[code]
<eligible_properties>
  <property_group>
    <group>Auto</group>
    <property>
      <name>Auto Body Shop</name>
      <description>&lt;p&gt;No description available for this property.&lt;/p&gt;</description>
    </property>
    <property>
      <name>Auto Rental Yards</name>
      <description>&lt;p&gt;These loans should be sought from your local bank as they require frequent inspections for loan advances.&lt;/p&gt;</description>
    </property>
  </property_group>
</eligible_properties>
[/code]

And the PHP pretty much fell inline after that.
[code]<?php
$doc = new DOMDocument();
$doc->load('rss_eligible_properties.xml');

$GROUPS = $doc->getElementsByTagName('property_group');

echo '<table>';

ForEach ($GROUPS AS $GROUP) {
  $theGroups = $GROUP->getElementsByTagName('group');
  $theGroup = $theGroups->item(0)->nodeValue;
  echo '<tr><td><b>'.$theGroup.'</b></td></tr>';

  $PROPERTIES = $GROUP->getElementsByTagName('property');
  ForEach ($PROPERTIES AS $PROPERTY) {
    $theProperties  = $PROPERTY->getElementsByTagName('name');
    $theProperty = $theProperties->item(0)->nodeValue;

    $theDescriptions = $PROPERTY->getElementsByTagName('description');
    $theDescription = $theDescriptions->item(0)->nodeValue;

    echo '<tr><td style="padding-left:20px;">
          <div><i><u>'.$theProperty.'</u></i></div>
          <div>'.$theDescription.'</div>
          </td></tr>';
  }
}
echo '</table>';
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32122-solved-parsing-xml/#findComment-149158
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.