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
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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.