[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

developerWorks : XML : Features / Library - Papers



 
    
 
http://www-4.ibm.com/software/developer/library/php-xml-toolkit.html
 
Title: developerWorks : XML : Features / Library - Papers
IBM
Shop Support Downloads
Home Products Consulting Industries News About IBM Search
IBM : developerWorks : XML : Library - papers
 
Download it now!
PDF (52.2 KB)
Free Acrobat™ Reader

PHP developers:  Fill your XML toolbox
Essential tools and libraries for using XML with PHP

Craig Knudsen
Web Engineer, ePresence, Inc.
June 2000

Contents:
 expat in PHP 4
 Working with PHP 3
 Looking to the future
 Using Java objects
 Choosing your tools
 Resources
 About the author

The PHP scripting language's XML support has a few gaps, but you can fill them with Java objects in a pinch. In this article, find out what XML tools are available now for PHP developers looking to craft XML projects, and find out how to access Java objects until the PHP tool set matures.

PHP often has been one of the first languages to implement new technologies. In the case of XML, PHP first added support back in version 3.06, which was released in December 1998.

Building expat into PHP 4
PHP 4.0, which was recently released, uses James Clark's popular C-based expat parser, which is also the XML parser used by the Mozilla browser. PHP 4 includes expat in the distribution, so enabling XML support is as simple as using the --enable-xml switch as part of the configuration step when building PHP as seen below:

./configure --enable-xml -with-apache=../apache_1.3.12

Of course, you'd probably want to set other options as well when you configure.

Because PHP 4 uses the expat parser, limitations of that parser apply to PHP. For example, because expat is not a validating parser, PHP's XML module cannot do any validation against a DTD.

Working with PHP 3
If you're using PHP 3 and Apache 1.3.7 or later, you can use Apache's expat library. (The XML section of the PHP Manual has detailed instructions on setting up XML support in PHP 3. See
Resources.)

Looking to the future
The PHP team is currently working on adding support for a DOM extension using the GNOME Project's libxml (which uses the W3C's XML parser), which can turn any XML file into a DOM object tree and manipulate that tree. Experimental code is already in PHP 4, but it is scarce (as of PHP 4 RC1).

Using Java objects in a pinch
PHP does not have the wide variety of XML tools that are now available for Java developers and Perl developers. However, PHP 4 includes support for accessing Java objects, putting the wide variety of Java language XML tools at the disposal of PHP developers. You don't need to know how to write Java code to take advantage of this method, although you'll find it easier if you have some familiarity with Java language calling conventions (such as method overloading).

PHP's Java language support is new and documentation is not yet available, but expect this to improve. For now, developers interested in accessing Java objects need to examine two examples provided within the PHP distribution (found in the same directory as the Java extension.)

Some problems exist with the interaction between PHP and the Java language. The Java language's method of overloading is not 100% supported by PHP's Java extension because the PHP language does not support method overloading.

The setup process differs slightly for each implementation of Java objects. See the README file in the ext/java directory of your PHP distribution. (Only Red Hat 6.1 and Windows NT 4.0 instructions are included; it is not clear when other operating systems will be supported.)

When building on a Red Hat Linux 6.1 system with Blackdown?s JDK 1.2.2 RC 4, I did encounter a problem with the PHP configure script that would not allow me to use just the --with-java option. Instead, I had to specify the location of the JDK using --with-java=/opt/jdk1.2.2 instead.

be sure to copy or move the php_java.jar file and the libphp_java.so file into permanent locations (they are not installed as part of the "make install" process), so they can be referenced in your php.ini configuration file (as seen in the ext/java README file.)

The following example demonstrates how to call Java objects from within PHP.

Listing 1. Calling Java objects from within PHP


$system = new Java ( "java.lang.System" ); print "Java version: " . $system->getProperty ( "java.version" ) . "<br>\n"; print "Java vendor: " . $system->getProperty ( "java.vendor" ). "<p>\n"; print "OS: " . $system->getProperty ( "os.name" ) . " " .   $system->getProperty ( "os.version" ) . " on " .   $system->getProperty ( "os.arch" ) . "<br>\n"; Processing stylesheets

Developers who want to use XSL to translate XML documents into HTML will find it difficult to do so in PHP alone. Because the Java language has many XSLT tools, using the new Java extension will greatly simplify this task.

Choosing your tools
The following tools summary lists the XML tools that you can now use with PHP. If you can't find what you're looking for in the table, check out the XML tool summary for Java developers (see
Resources) and get moving on calling those Java objects from within your PHP scripts.

Tools summary

Parsers and object models:
Generic XML parser PHP class that uses PHP's XML API to parse arbitrary XML input.
PHP RAX A PHP port of the XML::RAX Perl module.
XMLtree Another PHP class used to help parse XML input.
XSLT processors:
PHP Sablot extension A PHP extension that provides an interface to Sablotron XSLT library.
Protocols:
XML-RPC PHP Remote procedure calling (RPC) using HTTP as the transport and XML as the encoding. This class implements XML-RPC for PHP.
WDDX Protocol for the exchange of data between different languages such as Perl, Java, ColdFusion, and PHP. This module converts PHP variables to and from WDDX packets. You must install the WDDX SDK to enable WDDX support in PHP.
Horde Projects's Jonah tool Protocol for retrieving and parsing RDF/RSS files. RSS (Rich Site Summary) is most often used to represent news items, commonly called channels.

Resources

About the author
Craig Knudsen has been developing Web applications since 1994 using a variety of tools and languages. Craig currently telecommutes from Fairfax, Virginia, as a Web Engineer for ePresence, Inc., of Red Bank, New Jersey. You can reach him at
cknudsen@xxxxxxxxx.



 
What do you think of this article?

Killer! Good stuff So-so; not bad Needs work Lame!

Comments?


Privacy Legal Contact
Google