Forum Moderators: open

Message Too Old, No Replies

Parse xml with php

newb question

         

bean1000

2:44 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Hi everyone. I am new to xml and would like to take the following example xml,

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<shirt>
<name>Long Sleved></name>
<link>index/1/1</link>
<Types>
<Type>
<name>blue</name>
<link>index/1/1/1/17743/60088</link>
</Type>
<Type>
<name>green</name>
<link>index/1/1/1/1/1</link>
</Type>
<Type>
<name>red</name>
<link>index/1/1/1/2/2</link>
</Type>
<Type>
<name>yellow</name>
<link>index/1/1/1/3/3</link>
</Type>
<Type>
<name>orange</name>
<link>index/1/1/1/4/4</link>
</Type>
</Types>
</shirt>
<shirt>
<name>short sleved</name>
<link>index/1/1</link>
<Types>
<Type>
<name>light blue</name>
<link>index/1/1/1/17743/60089</link>
</Type>
<Type>
<name>green</name>
<link>index/1/2/1/1/1</link>
</Type>
<Type>
<name>red</name>
<link>index/1/2/1/2/2</link>
</Type>
<Type>
<name>yellow</name>
<link>index/1/2/1/3/3</link>
</Type>
<Type>
<name>orange</name>
<link>index/1/2/1/4/4</link>
</Type>
</Types>
</shirt>

And display it on,screen using PHP in this format,

Long Sleved, blue, index/1/1/1/17743/60088
Long Sleved, green, index/1/1/1/1/1
Long Sleved, red, index/1/1/1/2/2
Long Sleved, yellow, index/1/1/1/3/3
Long Sleved, orange<, index/1/1/1/4/4
short sleved, light blue, index/1/1/1/17743/60089
short sleved, green, index/1/2/1/1/1
short sleved, red, index/1/2/1/2/2
short sleved, yellow, index/1/2/1/3/3
short sleved, orange<, index/1/2/1/4/4

My actual xml file is a lot bigger than the example but in the same format. I hope that someone can show me how to do this. Have tried using simpleXML dom functions and others but just cant seem to do it.
Thanks

cmarshall

3:52 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld!

Well, what you need to do is use XSLT. However, that's not something that you will learn from a quick posting on this forum.

I recommend that you look at the wee_test series I did:

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

[webmasterworld.com...]

Head-Exploding Bonus Materials:

[webmasterworld.com...]

Demaestro

3:56 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



bean1000

You can save yourself some reading by checking out a open source project called Json.... It is so big even Google uses it to parse XML

[json.org...]

There is a full PHP5.2 library available pre-written.

cmarshall

3:59 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a full PHP5.2 library available pre-written.

I was unaware of this. I've been hand-transforming to JSON.

Thanks!

cmarshall

5:06 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, this [us2.php.net] is where it lives.

Sadly, I don't know if any of my ISPs have turned on PHP 5.2 yet (It was all I could do to get one of them to support PHP 5.1.3, so I could use XSLT [us2.php.net]).

Demaestro

5:25 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



marshall.... even though it is for 5.2 I bet it would be easier for you to start off with that and customize what breaks in your version rather then hand coding it all out yourself.

bean1000

7:17 pm on Oct 3, 2007 (gmt 0)

10+ Year Member



Thanks everyone. Will check out the info.

httpwebwitch

7:40 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yo bean,
welcome to WebmasterWorld!
here's a valuable code nugget for PHP5

super-easy xslt transformation


<?php
// define the two ingredients...
$path_style = "myxslt.xslt";
$path_xml = "mydata.xml";

// do the transformation... usually no need to alter this
$xp = new XSLTProcessor();
$xsl = new DomDocument;
$xsl->load($path_style);
$xp->importStylesheet($xsl);
$xml_doc = new DomDocument;
$xml_doc->load($path_xml);
$output = $xp->transformToXML($xml_doc);

// then...
print($output);

I spent about an hour steamlining this example and now I cut+paste it anywhere that I need to do an XML transformation - a technique which is becoming increasingly common in my work.

In order for this example to execute, I had to harass my ISP to recompile PHP with some extra config options that weren't there before... "--with-xsl[=DIR]" as described here: http://ca.php.net/xsl [ca.php.net]

if you're just getting started with XSLT, I recommend downloading a trial of <oxygen/> from [oxygenxml.com...] and play with it for a while. This forum also has a few expert participants who enjoy answering questions. Bookmark us.

cheers

httpwebwitch

7:46 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know if any of my ISPs have turned on PHP 5.2

I also didn't know about the PHP JSON library! Gosh, that looks useful. I've also been writing JSON "by hand". Actually, I've (on at least 2 occasions) used PHP to create XML from SQL, then used XSLT to transform that XML into JSON, which is kind of a weird thing to do when you think about it

my server is still on 5.1.2

cmarshall

10:06 pm on Oct 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I already plan to establish a VPS account with a new ISP that has 5.2.3 installed with all the bells and whistles.

I'm furious right now with one of my current ISPs, because they -once again- made a major configuration change that broke half my AJAX scripts. They won't install PHP 5 or MySQL 5, but make gigantic changes to the servers with no notice, then blame us when we complain.

They used to be good, then they got big, and outsourced all their tech support. I think I've had it with big ISPs. The reliability has been for s**t, and I'm sick of seven-day turnarounds on problems that could be fixed in five minutes.
</rant>

httpwebwitch

1:30 am on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



oh, I SO hear you
I rent a VPS. the ISP occasionally breaks stuff without warning... Fortunately they have a talented IT service guy who works the night shift, so if I get him in an IM chat between 2AM and 5AM I can get things done and all it costs me is sleep.

i am a high-maintenance customer