XML PHP
.
, ,
, XML ,
, PHP. , ,
XML . .
. , XML PHP
. , ,
XML-.
. ,
XML PHP, . , :
, XML-.
xml.php.
XML- ( test.xml).
. ,
DTD ( DDT :)).
PHP: XML-,
: US-ASCII, ISO-8859-1 UTF-8. ..
-, ,
.. .
. , , XML SciTE. ,
( PHP XML).
XML- :
<?xml version="1.0" encoding="UTF-8"?>
<album>
<foto smallfoto="Fotos/1smallvelo.jpg " bigfoto="Fotos/1bigvelo.jpg ">
<title> 1</title>
<comment>
1</comment>
<date>26.05.2003</date>
<color/>
<detailed>0</detailed>
</foto>
<foto smallfoto="Fotos/smallbardak.jpg " bigfoto="Fotos/bigbardak.jpg ">
<title> 2</title>
<comment>
2</comment>
<date>27.05.2003</date>
<color/>
<detailed>1</detailed>
</foto>
</album>
"" XML (
). , <color/>
. , .
, XML-.
XML PHP 20 .
. :
<?
$xmlfilename = "test.xml";
$code = "UTF-8"; // xml-
$curcode = "Windows-1251"; //
$level = 0; //
$list = array(); // xml-
// Unicode
function encoding ($str)
{
global $code;
global $curcode;
$str = mb_convert_encoding($str, $curcode, $code);
return $str;
}
function drawspace()
{
global $level;
for ($i = 0; $i < $level * 10; $i++)
{
echo " ";
}
}
//
function characterhandler ($parser, $data)
{
global $code;
global $curcode;
drawspace();
$data = encoding($data, $curcode, $code);
$data = trim($data)."<br>";
echo $data;
}
//
function starthandler ($parser, $name, $attribs)
{
global $level;
global $list;
global $code;
global $curcode;
$name = encoding($name, $curcode, $code);
$list[] = $name;
drawspace();
echo "<<font color='blue' size='+1'>$name</font>";
foreach ($attribs as $atname => $val)
{
echo encoding("$atname => $val");
}
echo "><br>";
$level++;
}
//
function endhandler ($parser, $name)
{
global $level;
global $list;
array_pop($list);
$level--;
drawspace();
echo "<<font color='blue' size='+1'>/$name</font>><p>";
}
//
$parser = xml_parser_create($code);
if (!$parser)
{
exit (" ");
}
else
{
echo " <p>";
}
//
xml_set_element_handler($parser, 'starthandler', 'endhandler');
xml_set_character_data_handler($parser, 'characterhandler');
// xml
$fp = fopen ($xmlfilename, "r");
if (!$fp)
{
xml_parser_free($parser);
exit(" ");
}
while ($data = fread($fp, 4096))
{
if (!xml_parse($parser, $data, feof($fp)))
{
die(sprintf(" : %s %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
}
fclose ($fp);
xml_parser_free($parser);
?>
,
. xml_parser_create
xml_parser_create_ns. ,
, XML-. ,
- , ISO-8859-1. , ,
UTF-8. ..
, ($code = "UTF-8";).
, ($curcode =
"Windows-1251";). xml_parser_create_ns (
) , ,
. .. ,
. , $parser
, .
XML,
XML. :
//
xml_set_element_handler($parser, 'starthandler', 'endhandler');
xml_set_character_data_handler($parser, 'characterhandler');
xml_set_element_handler
. ,
. - ,
,
.
.
:
//
function starthandler ($parser, $name, $attribs)
{
}
, ,
(, ).
, , ..
=> . :
foreach ($attribs as $atname => $val)
{
echo encoding("$atname => $val");
}
, ,
:
function endhandler ($parser, $name)
{
}
. ,
- . , ,
<color/> :
<COLOR>
</COLOR>
, ,
xml_set_character_data_handler.
, ,
:
function characterhandler ($parser, $data)
, .
" 1" " 2"
. , , - XML-.
- . ..
fopen, :
$fp = fopen ($xmlfilename, "r");
, xml_parse:
while ($data = fread($fp, 4096))
{
if (!xml_parse($parser, $data, feof($fp)))
{
die(sprintf(" : %s %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
}
xml_parse . - ,
- , () - ,
( , ).
. .
xml_get_error_code , xml_error_string
, .
.
xml_parser_free:
xml_parser_free($parser);
. .. XML Unicode,
.
(Koi8, Windows), Unicod' -
. . PHP,
XML, UTF-8.
utf8_decode, UTF-8, utf8_encode,
UTF-8. ,
ISO-8859-1, . ,
PHP - ,
- mb_convert_encoding.
:
$str = mb_convert_encoding($str, $curcode, $code);
$curcode $code ,
(, ?). :
- , - ,
, () - ,
. . , ,
, ( ), ,
, , PHP - mbstring (multi byte
string). , Windows, php.ini
extension=php_mbstring.dll.
, , , ()
. ,
:
// Unicode
function encoding ($str)
{
global $code;
global $curcode;
$str = mb_convert_encoding($str, $curcode, $code);
return $str;
}
, mb_convert_encoding -
XML. ,
( ,
) $list
, -
. .. $list ,
.
, .
color . <color>,
. PHP: " : mismatched tag 16".
. "mismatched tag" ,
<data/> <foto/>.
. XML-
Windows-1251 <?xml version="1.0" encoding="Windows-1251"?>
( ),
PHP... :) , .
: Win2000 + SP3; Apache 1.3.27; PHP 4.3.1.
. -
.
, ,
( , + ). !
: www.wr-script.ru, 2004-2024.
. WR-Script.ru!
|
|