gijew Posted December 28, 2006 Share Posted December 28, 2006 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 DESCRIPTIONGROUP 2 PROPERTY 1 PROPERTY 1 DESCRIPTIONSo 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><p>Gasoline and diesal tanks are not allowed.</p><p>Waste oil storage tanks should have secondary containment.</p><p>Auto lifts should ideally be above ground</p></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 More sharing options...
maexus Posted December 29, 2006 Share Posted December 29, 2006 Try simplexml? I used to hate XML but needed to work with it, much easier with simplexml Link to comment https://forums.phpfreaks.com/topic/32122-solved-parsing-xml/#findComment-149062 Share on other sites More sharing options...
gijew Posted December 29, 2006 Author Share Posted December 29, 2006 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 :PFor 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><p>No description available for this property.</p></description> </property> <property> <name>Auto Rental Yards</name> <description><p>These loans should be sought from your local bank as they require frequent inspections for loan advances.</p></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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.