Jump to content

PHP and unusual XML layout


matfish

Recommended Posts

Hi there,

 

The usual way I use XML is using:

 

<xml>
<item>
<id>1</id>
<body>content 1</body>
</item>
<item>
<id>2</id>
<body>content 2</body>
</item>
</xml>

 

However I'm having to pull the content from a CMS which is out of my control which uses:

 

<xml>
<item id="1" body="content 1" />
<item id="2" body="content 2" />
</xml>

 

Is there a way to parse and loop through this type of XML formatting easily and put into a PHP array?

 

Thanks in advance!

 

Link to comment
https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/
Share on other sites

Hi, thanks, I did have a look at that but does it support the layout I'm having trouble with?

I keep getting string errors.

 

Many thanks

Yes, simplexml can handle attributes (id & body), which is perfectly valid XML BTW.  Can you show us the problematic code?

I think I'm getting my head around it now.

 

I'm currently using:

 

$xmlstr = <<<XML
<xml>
<item id="1" body="content 1" />
<item id="2" body="content 2" />
</xml>
XML;

$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->item as $data) {
    echo $data['id'].": ".$data['body']."<br />";
}

 

Many thanks

Using the code from my previous post, I've got that working.

 

But another form of XML I'm dealing with is:

 

<xml>
<item Mon="9:00am" />
<item Tue="9:30am" />
<item Wed="10:00am" />
</xml>

 

I'm trying to get the node/variable name of "Mon/Tue/Wed" into an array alongside it's time in a loop?

 

Any ideas?

 

Thanks for your help.

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.