Classes in PHP-manipulating XML (1)

Source: Internet
Author: User
Tags xml parser

Http://www.hansanderson.com/me

<?php

/*
(c) Hans Anderson Corporation. All Rights Reserved.
You are are free to use and modify this class under the same
Guidelines found in the PHP License.

-----------

Bugs/me:
http://www.hansanderson.com/php/
Me@hansanderson.com

-----------

Version 1.0

-1.0 is the ' the ' ' actual release of the class. It ' s
Finally what I was hoping it would is, though there
are likely to still is some bugs in it. This is
A much changed version, and if you have downloaded
A previous version, this WON ' T work with your existing
scripts! You'll need to make some simple changes.

-. Didn fixed bug that ' t include tag attributes

(To use attributes, add _attributes[array_index]
To the "end of" the tag in question:
$xml _html_head_body_img would become
$xml _html_head_body_img_attributes[0],
For example)

--Nick Winfield <nick@wirestation.co.uk>
For reporting the This bug.

-No longer requires php4!

-All elements are array. Using Objects has
been discontinued.

-----------

What class.xml.php is:

A very, very easy to use XML parser class. It uses PHP ' s XML functions
For your, returning one array that has all the tag information. The only
Hard part is figuring out the syntax of the tags!

-----------

Sample use:

Require (' class.xml.php ');
$file = "Data.xml";
$data = Implode ("", File ($file)) or die ("could not open XML input file");
$obj = new XML ($data, "xml");


Print $xml ["Hans"][0]->num_results[0];
For ($i =0 $i <sizeof ($xml ["Hans"]); $i + +) {
Print $xml ["Hans"] [$i]->tag[0]. " ";
}

To print URL attributes (if they exist):

Print $xml ["Hans"][0]->attributes[0]["size"]; # where ' size ' is an attr name

(That ' s it! Slick, huh?)
-----------

Two ways to call XML class:

$xml = new XML ($DATA);
-OR-
$xml = new XML ($data, "jellyfish");

The second argument (jellyfish) is optional. Default is ' xml '.
All the second argument does are give you a chance to name the array
It returned something besides "XML" (in case you are already using
That name). Normal PHP variable name rules apply.

----------

Explanation of XML class:

This class takes valid XML data as a argument and
Returns the information in a complex but loopable array.

Here ' s how it works:

Data:

<title>hans Anderson ' s XML class</title>
<body>
</body>

Run the data through my class, then access the title as this:
$xml ["Html_head"][0]->title[0];

Or, loop through them:
For ($i =0 $i <sizeof ($xml ["Html_head"]); $i + +) {
Print $xml ["Html_head"] [$i]->title[0]. " ";
}

Yes, the variable names *are* long and messy, but it ' s
The best way to create the tree, IMO.


This is a complex explanation I sent to one class.xml.php user:

---------

> Now I ' ve run into another problem:
>
> <story timestamp= "2000-12-15t20:08:00,0" >
> <SECTION>Markets</SECTION>
> <byline>by <byline_author id= "378" >aaron L. Task</byline_author><br/>senior
> writer</byline>
> </STORY>
>
> How do I get byline_author?

Print $xml ["Story_byline"][0]->byline_author[0];

> and just a little question:is there a easy way to get TIMESTAMP?

Print $xml ["STORY"][0]->attributes[0]["TIMESTAMP"];

This is confusing, I know, but it's the only way I could really do
This. Here ' s the rundown:

The $xml is a array--an array of arrays. The The



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.