Re: how to edit a xml file
| From: | Ron Tarrant | Date: | Sat, 09 Sep 2006 04:20:33 +0000 |
| Subject: | Re: how to edit a xml file | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-7235@lists.php.net to get a copy of this message | ||
suomaf wrote:
Hello all, I think this is a php question again, but i can't seem to find information on how to edit a xml after reading it in. Perhaps you mean "parse" rather than "edit." Anyway, here's an example using SimpleXML:The XML file: <?xml version="1.0" encoding="UTF-8"?> <mybook isbn="0836217462"> <title>Being a Dog Is a Full-Time Job</title> <author>Charles M. Schulz</author> <character creature="dog" gender="male">
<name>
Snoopy
</name>
<friend_of>
Peppermint Patty
</friend_of>
<since>
1950-10-04
</since>
<qualification>
extroverted beagle
</qualification>
</character>
<character creature="human" gender="female">
<name>
Peppermint Patty
</name>
<friend_of>
Snoopy
</friend_of>
<since>
1966-08-22
</since>
<qualification>
outgoing, bold, and brash human female
</qualification>
</character>
</mybook>
The XSD file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="mybook">
<xs:complexType>
<xs:sequence>
<xs:element ref="title"/>
<xs:element ref="author"/>
<xs:element maxOccurs="unbounded" ref="character"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
<xs:element name="character">
<xs:complexType>
<xs:sequence>
<xs:element ref="name"/>
<xs:element minOccurs="0" ref="friend_of"/>
<xs:element ref="since"/>
<xs:element ref="qualification"/>
</xs:sequence>
<xs:attribute name="creature" use="required" type="xs:NCName"/>
<xs:attribute name="gender" use="required" type="xs:NCName"/>
</xs:complexType>
</xs:element>
<xs:element name="name" type="xs:string"/>
<xs:element name="friend_of" type="xs:string"/>
<xs:element name="since" type="xs:NMTOKEN"/>
<xs:element name="qualification" type="xs:string"/>
</xs:schema>
The parser:
<?php
/*
* Notes:
* The following casts are allowed in PHP: * - (int), (integer) - cast to integer * - (bool), (boolean) - cast to boolean- (float), (double), (real) - cast to float
* - (string) - cast to string * - (array) - cast to array * - (object) - cast to object* * In order to keep an XML file as human readable as possible, * it's best to have the opening tag, the content and the closing * tag on three separate lines. However, this creates a problem * for the XML interpreter because all that extra white space * becomes part of the content. One way to avoid this is to not do * any indenting, but that causes another problem in human readability. * To avoid both the readability problem and the extra white space * problem, the following code can be used. What it does is strip all * white space from the tag's contents and replace it with nothing (""). * Here's what it looks like:
* $val = preg_replace("/\s\s+/", "", $val);
*
* Elements (the contents of a tag) in an XML file are accessed as
* PHP object properties using the '->' object operator.
* Example:
* $book->title* * Tag attributes are accessed using the attribute name * as if it was an array key. * Example:
* $book['isbn'] * OR * $character["creature"]*/ $book = simplexml_load_file("library3.xml"); // var_dump($book); parse_xml($book); function parse_xml($book) { $isbn = $book["isbn"]; echo("isbn = $isbn\n"); $title = preg_replace("/\s\s+/", "", $book->title); echo("title = $title\n"); $author = preg_replace("/\s\s+/", "", $book->author); echo("author = $author\n"); foreach($book->character as $character) {
$creature = preg_replace("/\s\s+/", "", $character['creature']); // attribute
if($character["gender"] == "male") // attribute
{
$pronoun = "he";
}
else
{
$pronoun = "she";
}
$name = preg_replace("/\s\s+/", "", $character->name);
$friend_of = preg_replace("/\s\s+/", "", $character->friend_of);
$qualification = preg_replace("/\s\s+/", "", $character->qualification);
$since = preg_replace("/\s\s+/", "", $character->since);
echo("The character's name is $name");
echo(" and $pronoun is a $creature.\n");
echo("$name has been a friend of $friend_of");
echo(" since $since.\n");
echo("$name is an $qualification.\n");
echo("\n\n");
}
} // parse_xml()
?>
Hope this helps.
Take care.
-Ron T.
--
Ron Tarrant
Blog:<A HREF="http://www.writingup.com/blog/phpgtk2">PHP-Gtk2</A>