PHP XML创建者不支持波斯/阿拉伯语编码(UTF-8) [英] PHP XML creator doesn't support Persian/Arabic encoding (UTF-8)

查看:167
本文介绍了PHP XML创建者不支持波斯/阿拉伯语编码(UTF-8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我的php XML生成器类的问题;我使用XML Creator类:

  class _XmlWriter {

var $ xml;
var $ indent;
var $ stack = array();

函数_XmlWriter($ indent =''){
$ this-> indent = $ indent;
$ this-> xml ='<?xml version =1.0encoding =UTF-8?>'。 \\\
;
}

函数_indent(){
for($ i = 0,$ j = count($ this-> stack); $ i <$ j; $ i ++){
$ this-> xml。= $ this-> indent;
}
}

function push($ element,$ attributes = array()){
$ this-> _indent();
$ this-> xml。='<'。 $元素;
foreach($ attributes as $ key => $ value){
$ this-> xml。=''。 $键。 '='。htmlentities($ value)。'';
}
$ this-> xml。=> \\\
;
$ this-> stack [] = $ element;
}

函数元素($ element,$ content,$ attributes = array()){
$ this-> _indent();
$ this-> xml。='<'。 $元素;
foreach($ attributes as $ key => $ value){
$ this-> xml。=''。 $键。 '='。htmlentities($ value)。'';
}
$ this-> xml。='>'。 htmlentities($ content)。 '< /'。 $元素。 '>'。 \\\
;
}

函数emptyelement($ element,$ attributes = array()){
$ this-> _indent();
$ this-> xml。='<'。 $元素;
foreach($ attributes as $ key => $ value){
$ this-> xml。=''。 $键。 '='。htmlentities($ value)。'';
}
$ this-> xml。=/> \\\
;
}

函数pop(){
$ element = array_pop($ this-> stack);
$ this-> _indent();
$ this-> xml。=< / $ element> \\\
;
}

函数getXml(){
return $ this-> xml;
}

函数create($ root,$ array){
$ xml = new _XmlWriter();
$ xml-> push($ root);
foreach($ array as $ record){
$ xml-> push('music',array('name'=> $ record [0],
'artist'= > $ record [1],
'album'=> $ record [2]));
$ xml-> pop();
}
$ xml-> pop();
return $ xml-> getXml();
}
}
?>

它生成XML文件,但不支持波斯文或阿拉伯语编码。

  XML解析错误:格式不正确
行号3,列98:
<音乐名称=name1艺术家=与 & Ugrave;&安培; Oslash;&安培;教派;&安培; Uacute;&放大器;复制;专辑= album1 >

当我在记事本中更改XML文件时,它在波斯文中也可以工作,但是我的php类生成不正确的编码。
tnx为您的帮助。

解决方案

我用自己写的函数,而不是xml_writer类:

  function xml_generator($ root,$ tag_array,$ att_array,$ val_array){
$ xml_str ='<?xml version =1.0 encoding =UTF-8?>'。 \\\
;
$ xml_str。='<'。 $ root。 '>'。 \\\
; ($ i = 0; $ i< sizeof($ val_array); $ i ++){
$ xml_str。='<'
$ tag_array [$ i]。
($ j = 0; $ j $ xml_str。= $ att_array [$ j]。 '=''$ val_array [$ i] [$ j]。'';
}
$ xml_str。='>'。 '< /'。 $ tag_array [$ i]。 '>'。 \\\
;
}
$ xml_str。='< /'。 $ root。 >;
return $ xml_str;
}

现在它支持Unicode(UTF-8)。


I have a problem with my php XML generator class; I used XML Creator class :

class _XmlWriter {

var $xml;
var $indent;
var $stack = array();

function _XmlWriter($indent = '  ') {
    $this->indent = $indent;
    $this->xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
}

function _indent() {
    for ($i = 0, $j = count($this->stack); $i < $j; $i++) {
        $this->xml .= $this->indent;
    }
}

function push($element, $attributes = array()) {
    $this->_indent();
    $this->xml .= '<' . $element;
    foreach ($attributes as $key => $value) {
        $this->xml .= ' ' . $key . '="' . htmlentities($value) . '"';
    }
    $this->xml .= ">\n";
    $this->stack[] = $element;
}

function element($element, $content, $attributes = array()) {
    $this->_indent();
    $this->xml .= '<' . $element;
    foreach ($attributes as $key => $value) {
        $this->xml .= ' ' . $key . '="' . htmlentities($value) . '"';
    }
    $this->xml .= '>' . htmlentities($content) . '</' . $element . '>' . "\n";
}

function emptyelement($element, $attributes = array()) {
    $this->_indent();
    $this->xml .= '<' . $element;
    foreach ($attributes as $key => $value) {
        $this->xml .= ' ' . $key . '="' . htmlentities($value) . '"';
    }
    $this->xml .= " />\n";
}

function pop() {
    $element = array_pop($this->stack);
    $this->_indent();
    $this->xml .= "</$element>\n";
}

function getXml() {
    return $this->xml;
}

function create($root, $array) {
    $xml = new _XmlWriter();
    $xml->push($root);
    foreach ($array as $record) {
        $xml->push('music', array('name' => $record[0],
            'artist' => $record[1],
            'album' => $record[2]));
        $xml->pop();
    }
    $xml->pop();
    return $xml->getXml();
}
}
?>

it generates XML file but doesn't support persian or arabic encoding.

XML Parsing Error: not well-formed
Line Number 3, Column 98:  
<music name="name1" artist="&Ugrave;&Oslash;&sect;&Uacute;&copy;" album="album1">

when I change XML file in notepad it works in persian too, but my php class generates incorrect encoding. tnx for your help.

解决方案

I used myself written function instead of xml_writer class :

function xml_generator($root, $tag_array, $att_array, $val_array) {
    $xml_str = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    $xml_str .= '<' . $root . '>' . "\n";
    for ($i = 0; $i < sizeof($val_array); $i++) {
        $xml_str .= '<' . $tag_array[$i] . ' ';
        for ($j = 0; $j < sizeof($att_array); $j++) {
            $xml_str .= $att_array[$j] . '="' . $val_array[$i][$j] . '" ';
        }
        $xml_str .= '>' . '</' . $tag_array[$i] . '>' . "\n";
    }
    $xml_str .= '</' . $root . '>';
    return $xml_str;
}

now it supports Unicode(UTF-8).

这篇关于PHP XML创建者不支持波斯/阿拉伯语编码(UTF-8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆