PHP and XML?

Da_iMac_Daddy

Not-so-Neo-DumbA$$
Is there any easy way to use xml documents with php. My webhost only offers me one MySql DB and I don't want to have to go learning how to work a database and mess up the only one I have. Plus I don't need the functionality of a database. I want to have a book list of books I am reading.
The xml looks like this:
Code:
<book>
<title>A Different Kind of Teacher</title>
<author>John Taylor Gatto</author>
<progress>page 100</progress>
</book>

Any help is greatly appreciated.


::edit:: I did look this up on php.net but they were talking about compiling libraries and I don't have access through my web host to do anything like that.::/edit::
 
Ive done PHP/XML apps in the past. the sad truth is that PHP has not built any easy ways of parsing XML. There are 2 main ways to go about parsing. The first is the built in EXPAT functions of PHP4. Ive never really used, as they seemed a bit cumbersome. But, heres a good tutorial on using expat from zend. http://www.zend.com/zend/art/parsing.php

The second is using COM functions. This tends to be a bit easier in my opinion. I remeber finding a tutorial on at phpbuilder.com.

Honestly, to tell you the truth, it would probably less of a learning curve for you to learn how to code for mysql, especially if you plan on writing to the XML file....that can get iffy. Then if you actually need the XML functionality of your data, writing a script to output XML from mysql is a breeze.

Hope this helps.
 
Well is there a way for me to grab snippets from a file like,

Code:
#This isn't valid PHP just a kind of psuedo code

$xmlFile = "./books.xml";
$fileXML = open($xmlFile);

while (!EOF){

$title = getSubStr($fileXML, "<title>" * "</title>");

$author = getSubStr($fileXML, "<author>" * "</author>");

$progress =  getSubStr($fileXML, "<progress>" * "</progress>");

}
close($xmlFile);

If I could do that I wouldn't need to have to even use the php parser.
 
yes of course you can do it string based... but if you plan on writing back to the XML doc that can get tricky.
 
What exactly is the function to use when trying to obtain a string from within another string. Also does php use regular expressions for it's text parsing or what?

On my site there will be a form for entering book information. Couldn't I set it up to print to a file in a way that will emulate a valid xml document? Like:
Code:
 $title = $_POST['title'];
 $title = "<title>$title</title>";

Or should I just format the text as html and just use PHP to import it onto the page wholly?
 
It really is a mess dude. I had to copy over all the xml, write new data finish xml to a temp then copy all of its contents back to the original xml doc. Its harsh. If i was u id just do a srtaight text file and include it. Or seriously spend some time figuring out php/mysql. Its a sinch man, seriously.
 
If you're just keeping a simple list of books, you could easily use MySQL and just create a single table to hold your data with the rows you need. If you already have a datase setup and you know the name and password to access the database it's very easy. If you download and install MySQL on your Mac, you can then run the MySQL client on your machine and connect remotely to your host.

Download from MySQL the Mac OS X Standard Installer Package: http://www.mysql.com/downloads/mysql-4.0.html

You really don't have to worry about much of the setup for it unless you want to run your own local MySQL database server for testing purposes. To connect to your host you would run the mysql client located at:

/usr/local/mysql/bin/

$./mysql -h hostname -u username -p and then it will ask you for a password

you can type /q to quit.


I already knew a bunch of MySQL stuff from college, but I picked up this new book onsale at Borders and it makes a very good cheap refresher for little things and a great intro into how to use MySQL for beginers: http://www.amazon.com/exec/obidos/t...=sr_1_18/102-2276304-8393723?v=glance&s=books . I also got this book on PHP and MySQL and used it quite a bit: http://www.amazon.com/exec/obidos/A...6462373/sr=2-2/ref=sr_2_2/102-2276304-8393723 It's probably overkill for what you need right now, but the online PHP and MySQL documentation is pretty good.
 
btoth said:
I also got this book on PHP and MySQL and used it quite a bit: http://www.amazon.com/exec/obidos/A...2276304-8393723
I've got that book as well. The second chapter deals with outputting data to text files and importing it later. If you don't want the whole book, I suggest you look for the book at your local bookstore, read the 2nd chapter, and take notes on how to do what you want it to do. From what you described, I think MySQL itself would be overkill.
 
You will have a problem if you ever want to write to the xml file when there is more than one person browsing your website. What happens if there are 2 or more writes at the same time? It will not work and the PHP will probably crash because it can't write to the file.

This is where a database comes in handy because it looks after when to write the changes to disk for you so you can have multiple changes happen at once without any problems.

Something as simple as a list of books in a database is extreamly easy and would be easier than trying to parse a text file and making sure you don't try and write to it while it's already being written to etc.
 
Captain Code said:
You will have a problem if you ever want to write to the xml file when there is more than one person browsing your website. What happens if there are 2 or more writes at the same time? It will not work and the PHP will probably crash because it can't write to the file.

This is where a database comes in handy because it looks after when to write the changes to disk for you so you can have multiple changes happen at once without any problems.

Something as simple as a list of books in a database is extreamly easy and would be easier than trying to parse a text file and making sure you don't try and write to it while it's already being written to etc.
That's what the PHP function flock is for. (And no, I don't mean seagulls.)
 
Arden said:
That's what the PHP function flock is for. (And no, I don't mean seagulls.)

It's still not going to work because you can't lock the file twice at once. If there are 2 people updating it then it wouldn't be able to lock the file when it's already locked. Also, you can't update one record in the file while someone else updates another record in the same file.
 
Even MySQL wouldn't be the final word, here.

Although row-level locking is possible with the innoDB table format, there's still the risk of phantom read / write, but it's rare and it's going to have to be a pretty busy web site to get in such a tangle in the first place.

Apparently -- though I've yet to discover for myself -- PostGre is far more suited for this kind of thing: better rollback support et cetera.

Anyway .. learning MySQL is pretty easy. It might look hard -- it did to me the first time I started out -- but it's much more rewarding than messing around with files, file permissions et cetera...
 
Captain Code said:
It's still not going to work because you can't lock the file twice at once. If there are 2 people updating it then it wouldn't be able to lock the file when it's already locked. Also, you can't update one record in the file while someone else updates another record in the same file.
Yes, but when you tell a PHP script to lock a page, it can't be used by another connection until the first connection unlocks it. The flock function is built to prevent exactly what you're describing.
 
Arden said:
Yes, but when you tell a PHP script to lock a page, it can't be used by another connection until the first connection unlocks it. The flock function is built to prevent exactly what you're describing.

True, you can't lock a database record twice at the same time(AFAIK) with MySQL, but what would you rather have, only one record locked, or the entire database(in the form of a file) locked?
 
Anyway I would be the only one using this site. I'm just trying this out for fun. So the file writting permissions aren't really my biggest concern. Thanks for posting guys but I'm off to bigger and better things. I am learning MySQL now but for other reasons, I'm doing my dad's site and I've got to get a shopping cart going and a user database so I'm doing my best to learn MySQL. :p

Weird thing is I was trying to create a new table with information like contact_id int auto-increment, userName varchar(20) not null, and a few other text columns in phppmyadmin or what ever it's called and it kept giving me an error. I'll figure it out though. :p
 
When you don't have the access to build other xml support in, your PHP engine only has the default expat engine, which is a pain to work with. Good old Bas van Gaalen has written a nice xml-dom class for PHP which uses only the expat engine. Get it here:

http://www.webtweakers.com/phpdomxml/

If you just include this file at the top of your page, you can then use standard DOM syntax to load, navigate, and manipulate your XML trees. It's very useful in situations as yours.

On a related topic, I was working on a very basic xslt class (using van Gaalen's xml-dom class), but I've since run out of time and lost it. Anybody know of an xslt class which uses only the expat library?
 
Back
Top