2

I'm having issues attempting to parse an XML file I'm pulling in from another site with SimpleXML. I need to display the rent for certain properties. However, I only want to display the rent for certain properties. I've never seen an XML file structured in this manner: http://dl.dropbox.com/u/1925679/example.xml

First, how would I parse through this file based on

Property->PropertyID->MITS:Identification->MITS:PrimaryID

Then, echo out the Property->Floorplan->MarketRent(Min and Max) based on an ID?

TIA!!!!

1
  • are you thrown off by the namespaces ("MITS:")? If so, don't worry about them, just treat the whole thing ("MITS:CompanyInfo") as a node name. Commented Jun 29, 2011 at 0:35

1 Answer 1

2
// get all properties
$properties = $xml->xpath('//Property');

// get document namesapces in prefix => uri format
$namespaces = $xml->getNamespaces(true);


foreach($properies as $property)
{
   // get namespaced nodes
   $identification = $property->PropertyID->children($namespaces['MITS']);
   $id = $identification->children($namespaces['MITS'])->PrimaryID;

   // do your check id is an expected value or whatever and if so then...

   foreach($property->Floorplan as $floorplan){
   {
      echo $floorplan->MarketRent['min'];
      echo $floorplan->MarketRent['max'];
   }
}

You could probably com up with an xpath query to ONLY select properties with a given id or set of ids straight away so then you would only have to loop over them and invoke the floorplan loop but ill leave that to your investigation :-) I will say though if you go that route, you will need to register the namespaces with xpath i think. Pretty sure thats not automatic.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly! The only change I had to make was $xml->getNamespaces(true); Thanks!!!
No problem! Glad you worked it out. Will update answer with your adjustment for others' benefit :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.