Author Topic: Working with XML within PHP  (Read 6054 times)

RevDisk

  • friend
  • Senior Member
  • ***
  • Posts: 12,633
    • RevDisk.net
Working with XML within PHP
« on: December 12, 2010, 11:37:48 PM »

Anyone do any PHP work?  Trying to read in info from an XML but I'm having a heck of a time. 
"Rev, your picture is in my King James Bible, where Paul talks about "inventors of evil."  Yes, I know you'll take that as a compliment."  - Fistful, possibly highest compliment I've ever received.

CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Working with XML within PHP
« Reply #1 on: December 12, 2010, 11:48:10 PM »
Hi there!
On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

RevDisk

  • friend
  • Senior Member
  • ***
  • Posts: 12,633
    • RevDisk.net
Re: Working with XML within PHP
« Reply #2 on: December 13, 2010, 12:35:45 AM »
Hi there!


Heyo there!

Alright, suppose I have a fairly simple XML file

<?xml version="1.0" encoding="UTF-8" ?>
<ServerInfo>
<host>Something</host>
<time>SomethingElse</time>
</ServerInfo>

Could you give me an example of how to display the short example in PHP?  Every single example I've hunted has a loop and overly nested structure that I don't need or want.  I've been trying a bunch of different things to try to get it working and think I'm missing something.  I'll probably break down and just buy a couple books on XML as there's few good online resources I've found. 


(Real XML file is at http://revdisk.org/sm.xml , generated from a shell script. Work in progress)
"Rev, your picture is in my King James Bible, where Paul talks about "inventors of evil."  Yes, I know you'll take that as a compliment."  - Fistful, possibly highest compliment I've ever received.

Nick1911

  • Administrator
  • Senior Member
  • *****
  • Posts: 8,492
Re: Working with XML within PHP
« Reply #3 on: December 13, 2010, 01:01:38 AM »
You sir, need to become acquainted with the document object model, or DOM.

http://www.w3schools.com/php/php_xml_dom.asp

Nick1911

  • Administrator
  • Senior Member
  • *****
  • Posts: 8,492
Re: Working with XML within PHP
« Reply #4 on: December 13, 2010, 01:22:46 AM »
Here, this should get you started:  :angel:


<?php
/* Constructor */
$xmlDoc = new DOMDocument();

/* Load the doc into memory */
$xmlDoc->load("sm.xml");

/* Parse that sucker.  x = array of server*/
$x = $xmlDoc->getElementsByTagName("server");

/* Loop through each server*/
foreach ($x AS $item)   
  {
    /* (There's probably a better way to do this!  RTFM) */

    /* Get all hosts */
    $hosts = $item->getElementsByTagName("host");

    /* Get first host */
    $host = $hosts->item(0)->nodeValue;

    echo $host;
  }
?>


CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Working with XML within PHP
« Reply #5 on: December 13, 2010, 01:37:32 AM »
Sorry to jump in then leave you hangin like that.  Got distracted.

Nick is on the right path, but PHP's SimpleXML class might be a better and easier way to go.

I'm PMing you an example I did recently


On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

Nick1911

  • Administrator
  • Senior Member
  • *****
  • Posts: 8,492
Re: Working with XML within PHP
« Reply #6 on: December 13, 2010, 01:40:10 AM »
Sorry to jump in then leave you hangin like that.  Got distracted.

Nick is on the right path, but PHP's SimpleXML class might be a better and easier way to go.

I'm PMing you an example I did recently

Ohh, I like that.  If you're going to use a dynamically typed language, might as well take advantage of that.

RevDisk

  • friend
  • Senior Member
  • ***
  • Posts: 12,633
    • RevDisk.net
Re: Working with XML within PHP
« Reply #7 on: December 13, 2010, 09:24:32 PM »
IT IS ALIVE!

I forgot a critical step.  Sacrificed a goat to the Elder Gods, burned a copy of "The Art of Unix Programming", and chanting "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn" for a while

Live:      http://revdisk.org/server.php
Source:  http://revdisk.org/server.txt
XML:      http://revdisk.org/sm.xml
Script:    http://revdisk.org/sm.sh

« Last Edit: December 13, 2010, 09:30:39 PM by RevDisk »
"Rev, your picture is in my King James Bible, where Paul talks about "inventors of evil."  Yes, I know you'll take that as a compliment."  - Fistful, possibly highest compliment I've ever received.

CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Working with XML within PHP
« Reply #8 on: December 13, 2010, 09:44:03 PM »
Source:  http://revdisk.org/server.txt

Do liek dees:

cd /path/to/webroot
ln -s server.php server.phps

On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

roo_ster

  • Kakistocracy--It's What's For Dinner.
  • friend
  • Senior Member
  • ***
  • Posts: 21,225
  • Hoist the black flag, and begin slitting throats
Re: Working with XML within PHP
« Reply #9 on: December 13, 2010, 10:10:41 PM »
Anybody care to enlighten me as to why one would use php or python if perl is available?  Gotta be a reason.

I am not sure about php or python, but perl is really, really fast when I am crunching number & parsing text.
Regards,

roo_ster

“Fallacies do not cease to be fallacies because they become fashions.”
----G.K. Chesterton

RevDisk

  • friend
  • Senior Member
  • ***
  • Posts: 12,633
    • RevDisk.net
Re: Working with XML within PHP
« Reply #10 on: December 13, 2010, 10:13:06 PM »
Do liek dees:

cd /path/to/webroot
ln -s server.php server.phps



Na.  I have a bunch more things I gotta do to finish up the site.  I'll set up an automated tar script in crontab.  Or whatever.php?=viewsource ?   Na, tar.   =D

Script should update every 10 minutes.  Looks like it's working



Anybody care to enlighten me as to why one would use php or python if perl is available?  Gotta be a reason.

I am not sure about php or python, but perl is really, really fast when I am crunching number & parsing text.

You're entirely correct.  I just want more practice with PHP webapps.  I'll eventually port over to perl, python and anything else I can find.   Next is PHP webapp with SQL backend.
« Last Edit: December 13, 2010, 10:16:28 PM by RevDisk »
"Rev, your picture is in my King James Bible, where Paul talks about "inventors of evil."  Yes, I know you'll take that as a compliment."  - Fistful, possibly highest compliment I've ever received.

GigaBuist

  • friends
  • Senior Member
  • ***
  • Posts: 4,345
    • http://www.justinbuist.org/blog/
Re: Working with XML within PHP
« Reply #11 on: December 14, 2010, 10:04:45 AM »
Anybody care to enlighten me as to why one would use php or python if perl is available?  Gotta be a reason.

The nice thing about PHP is it comes with a pretty big library of functions baked right in.  With Perl (and Python) you're going to have to hunt them down, figure out which one you want, install them, and then get to reading their individual documentation.  PHP just gets you one stop shopping for, oh, 90% of the things a web developer is going to want to do in the course of a day.

Plus, with Perl, you always have the potential problem of writing something that you can't read again.  eg:  while(<>) { print; }
« Last Edit: December 14, 2010, 11:13:32 AM by GigaBuist »

roo_ster

  • Kakistocracy--It's What's For Dinner.
  • friend
  • Senior Member
  • ***
  • Posts: 21,225
  • Hoist the black flag, and begin slitting throats
Re: Working with XML within PHP
« Reply #12 on: December 14, 2010, 10:15:15 AM »
The nice thing about PHP is it comes with a pretty big library of functions baked right in.  With Perl (and Python) you're going to have to hunt them down, figure out which one you want, install them, and then get to reading their individual documentation.  PHP just gets you one stop shopping for, oh, 90% of the things a web developer is going to want to do in the course of a day.

Plus, with Perl, you always have the potential program of writing something that you can't read again.  eg:  while(<>) { print; }

 :lol:

Yeah, the perl I write is deliberately staid.
Regards,

roo_ster

“Fallacies do not cease to be fallacies because they become fashions.”
----G.K. Chesterton

CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Working with XML within PHP
« Reply #13 on: December 14, 2010, 10:19:27 AM »
Na.  I have a bunch more things I gotta do to finish up the site.  I'll set up an automated tar script in crontab.  Or whatever.php?=viewsource ?   Na, tar.   =D

I'm just showing you a better way to show off the code, instead of a .txt file.
.phps files are php code, but Apache displays them and inserts html font tags for format highlighting instead of invoking the PHP interpretter and running the script.

Make a symlink named .phps to your actual .php file, and you can show off the live code (like the example I sent you).
On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

RevDisk

  • friend
  • Senior Member
  • ***
  • Posts: 12,633
    • RevDisk.net
Re: Working with XML within PHP
« Reply #14 on: December 14, 2010, 11:08:37 AM »
I'm just showing you a better way to show off the code, instead of a .txt file.
.phps files are php code, but Apache displays them and inserts html font tags for format highlighting instead of invoking the PHP interpretter and running the script.

Make a symlink named .phps to your actual .php file, and you can show off the live code (like the example I sent you).

Yea, gathered onto that trick a couple years ago.  A persistent link is live.  That's what I'm intentionally avoiding. 



Meh.  Fine fine, I'll find some need for a perl script and do that.  I was in the middle of PHP arrays, but eh.  Perl is probably more my spec.  Mind you, my background is C/C++ and shell scripts.   Some of these new fangled "languages" I'm not overly partial to as they're built for ease of use over clean architecture.  Like PHP being seemingly unable to use XML without a loop.  That STILL drives me up a wall.  That is bad design.  Easy to use, sure, for 95% of situations, but Gods alone will be able to help you that remaining 5%.

"Rev, your picture is in my King James Bible, where Paul talks about "inventors of evil."  Yes, I know you'll take that as a compliment."  - Fistful, possibly highest compliment I've ever received.

CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Working with XML within PHP
« Reply #15 on: December 14, 2010, 11:15:29 AM »
Yea, gathered onto that trick a couple years ago.  A persistent link is live.  That's what I'm intentionally avoiding. 



Meh.  Fine fine, I'll find some need for a perl script and do that.  I was in the middle of PHP arrays, but eh.  Perl is probably more my spec.  Mind you, my background is C/C++ and shell scripts.   Some of these new fangled "languages" I'm not overly partial to as they're built for ease of use over clean architecture.  Like PHP being seemingly unable to use XML without a loop.  That STILL drives me up a wall.  That is bad design.  Easy to use, sure, for 95% of situations, but Gods alone will be able to help you that remaining 5%.



Maybe you need to try it in XSLT
On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

GigaBuist

  • friends
  • Senior Member
  • ***
  • Posts: 4,345
    • http://www.justinbuist.org/blog/
Re: Working with XML within PHP
« Reply #16 on: December 14, 2010, 11:18:56 AM »
Just checked out your source code.  You're pretty much just transforming XML 1:1 into another markup language.  You could, if you wanted, just make an XSL sheet to do the work and use PHP to apply the stylesheet to the source, present the output.

Might be a fun exercise.  I've done a fair amount of XSL(T) going from XML into XML:FO to create PDF reports.  Forces you to think about stuff in a different manner.

Heh, I see CNYCacher just said the same thing.

CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Working with XML within PHP
« Reply #17 on: December 14, 2010, 11:24:09 AM »
Ha!
On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

RevDisk

  • friend
  • Senior Member
  • ***
  • Posts: 12,633
    • RevDisk.net
Re: Working with XML within PHP
« Reply #18 on: December 14, 2010, 02:21:30 PM »
Maybe you need to try it in XSLT

Daddy like

http://revdisk.org/sm2.xml


Took three minutes to implement.  Three minutes to live status.  Updated shell script, cron, et al.  XSLT gets the thumbs up. 
"Rev, your picture is in my King James Bible, where Paul talks about "inventors of evil."  Yes, I know you'll take that as a compliment."  - Fistful, possibly highest compliment I've ever received.

CNYCacher

  • friend
  • Senior Member
  • ***
  • Posts: 4,438
Re: Working with XML within PHP
« Reply #19 on: December 14, 2010, 02:27:02 PM »
Daddy like

http://revdisk.org/sm2.xml


Took three minutes to implement.  Three minutes to live status.  Updated shell script, cron, et al.  XSLT gets the thumbs up. 

[grandpa]Now you're cooking with gas![/grandpa]
On two occasions, I have been asked [by members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question.
Charles Babbage

RevDisk

  • friend
  • Senior Member
  • ***
  • Posts: 12,633
    • RevDisk.net
Re: Working with XML within PHP
« Reply #20 on: December 14, 2010, 03:25:15 PM »
[grandpa]Now you're cooking with gas![/grandpa]

Figured out how to embed in PHP as well

Check out server.php
"Rev, your picture is in my King James Bible, where Paul talks about "inventors of evil."  Yes, I know you'll take that as a compliment."  - Fistful, possibly highest compliment I've ever received.

RevDisk

  • friend
  • Senior Member
  • ***
  • Posts: 12,633
    • RevDisk.net
Re: Working with XML within PHP
« Reply #21 on: December 14, 2010, 11:33:27 PM »

PHP-XML done.
XSL done.

(Both in need of prettying, but I'll do that last.)

Guess perl and python implementations are next.  Unless anyone else has suggestions for more or other implementations?  I'll pick the best, keep it, pretty it up and toss the rest in Projects for archiving.
"Rev, your picture is in my King James Bible, where Paul talks about "inventors of evil."  Yes, I know you'll take that as a compliment."  - Fistful, possibly highest compliment I've ever received.