xml文件生成的两个函数
来源:互联网 发布:担保企业软件 编辑:程序博客网 时间:2024/06/07 18:46
/***************************************************
* 数组转xml字符串格式
* @aurhor liuniuyyou
* @version 2009-09-09
* @access public
* @param array $data
* @return string
***************************************************/
function array2xml($data) {
$xml = '<?xml version="1.0" encoding="UTF-8"?>';
foreach ($data as $tag => $tmp) { #
if ($tag == "0")
$xml .= '<conf type="' . $tmp["type"] . '" >';
else {
$single_start = '<' . $tag;
$mult_start = '<' . $tag . '>';
$single_end = '/>';
$mult_end = '</' . $tag . '>';
$head = $single_start;
$end = $single_end;
foreach ($tmp as $key => $attr) {
if (!is_array($attr)) {
$body .= ' ' . $key . '="' . $attr . '" ';
} else {
$head = $mult_start;
$end = $mult_end;
foreach ($attr as $value) {
$body .= '<' . $key;
foreach ($value as $chil_key => $chil_value) {
$body .= ' ' . $chil_key . '="' . $chil_value . '" ';
}
$body .= ' />';
}
}
}
$xml .= $head . $body . $end;
unset ($head, $body, $end);
}
}
$xml .= '</conf>';
return $xml;
}
/*
* 生成xml文件
* @aurhor liuniuyyou
* @version 2009-09-09
* @access public
* @param object $pDB
* @return void
*/
function general_xml($pDB){
$groupssql = " select team_name,team_des, (select node_title from treenode where node_id= parent ) as parent from team;";
$groups_arr = $pDB->FetchTable($groupssql,true);
foreach ( $groups_arr as $key => $value ){
$i=$key;
if($value["parent"]==null) $value["parent"]='null';
$Phonebookarr["groups"]["group"]["$i attr"]= array('team_name' =>iconv('utf-8','gb2312',$value["team_name"]),'team_des' =>iconv('utf-8','gb2312',$value["team_des"]),
'parent'=>iconv('utf-8','gb2312',$value["parent"]));
}
$contactssql = " select contacts_name , contacts_des , contacts_company , contacts_address , contacts_profession , contacts_offphone , contacts_mobile , contacts_homephone , contacts_email , contacts_fax , (select node_title from treenode where node_id = contacts_group ) as contacts_group , contacts_innerphone from contacts;";
$contacts_arr = $pDB->FetchTable($contactssql,true);
foreach ( $contacts_arr as $key => $value ){
$key=$key." attr";
$Phonebookarr["contacts"]["contact"][$key]=array('contacts_name'=>iconv('utf-8','gb2312',$value["contacts_name"]),
'contacts_des'=> iconv('utf-8','gb2312',$value["contacts_des"]), 'contacts_company'=>iconv('utf-8','gb2312',$value["contacts_company"]),
'contacts_address'=>iconv('utf-8','gb2312',$value["contacts_address"]),'contacts_profession'=>iconv('utf-8','gb2312',$value["contacts_profession"]),'contacts_offphone' => iconv('utf-8','gb2312',$value["contacts_offphone"]),'contacts_mobile' =>iconv('utf-8','gb2312',$value["contacts_mobile"]),'contacts_homephone' => iconv('utf-8','gb2312',$value["contacts_homephone"]),'contacts_email' => iconv('utf-8','gb2312',$value["contacts_email"]),'contacts_fax' => iconv('utf-8','gb2312',$value["contacts_fax"]) ,'contacts_group' => iconv('utf-8','gb2312',$value["contacts_group"]),'contacts_innerphone'=> iconv('utf-8','gb2312',$value["contacts_innerphone"]));
}
echo "<pre>";
#print_r($Phonebookarr);
echo "</pre>";
exec("sudo -u root:root chmod -R 777 /var/www/html/modules/phonecontact/data/ ");
exec("sudo -u root:root chown -R root:root /var/www/html/modules/phonecontact/data/");
$xmlstr = array2xml($Phonebookarr); # &XML_serialize($Phonebookarr);
$kl = file_put_contents("/var/www/html/modules/phonecontact/data/Phonebook.xml",$xmlstr);
}
asterisk 里面的xml处理类 很不错的(择出以后可以用):
/* Usage
Grab some XML data, either from a file, URL, etc. however you want. Assume storage in $strYourXML;
$xml = new xml2Array($strYourXML);
xml array is in $xml->data;
This is basically an array version of the XML data (no attributes), striaght-up. If there are
multiple items with the same name, they are split into a numeric sub-array,
eg, <items><item test="123">foo</item><item>bar</item></items>
becomes: array('item' => array(0=>array('item'=>'foo'), 1=>array('item'=>'foo'))
attributes are in $xml->attributes;
These are stored with xpath type paths, as $xml->attributes['/items/item/0']["test"] == "123"
Other way (still works, but not as nice):
$objXML = new xml2Array();
$arrOutput = $objXML->parse($strYourXML);
print_r($arrOutput); //print it out, or do whatever!
*/
class xml2Array {
var $arrOutput = array();
var $resParser;
var $strXmlData;
var $attributes;
var $data;
function xml2Array($strInputXML = false) {
if (!empty($strInputXML)) {
$this->data = $this->parseAdvanced($strInputXML);
}
}
function parse($strInputXML) {
$this->resParser = xml_parser_create ();
xml_set_object($this->resParser,$this);
xml_set_element_handler($this->resParser, "tagOpen", "tagClosed");
xml_set_character_data_handler($this->resParser, "tagData");
$this->strXmlData = xml_parse($this->resParser,$strInputXML );
if(!$this->strXmlData) {
die_freepbx(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->resParser)),
xml_get_current_line_number($this->resParser)));
}
xml_parser_free($this->resParser);
return $this->arrOutput;
}
function tagOpen($parser, $name, $attrs) {
$tag=array("name"=>$name,"attrs"=>$attrs);
@array_push($this->arrOutput,$tag);
}
function tagData($parser, $tagData) {
if(trim($tagData)) {
if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] .= "/n".$tagData;
}
else {
$this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData;
}
}
}
function tagClosed($parser, $name) {
@$this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1];
array_pop($this->arrOutput);
}
function recursive_parseLevel($items, &$attrs, $path = "") {
$array = array();
foreach (array_keys($items) as $idx) {
@$items[$idx]['name'] = strtolower($items[$idx]['name']);
$multi = false;
if (isset($array[ $items[$idx]['name'] ])) {
// this child is already set, so we're adding multiple items to an array
if (!is_array($array[ $items[$idx]['name'] ]) || !isset($array[ $items[$idx]['name'] ][0])) {
// hasn't already been made into a numerically-indexed array, so do that now
// we're basically moving the current contents of this item into a 1-item array (at the
// original location) so that we can add a second item in the code below
$array[ $items[$idx]['name'] ] = array( $array[ $items[$idx]['name'] ] );
if (isset($attrs[ $path.'/'.$items[$idx]['name'] ])) {
// move the attributes to /0
$attrs[ $path.'/'.$items[$idx]['name'].'/0' ] = $attrs[ $path.'/'.$items[$idx]['name'] ];
unset($attrs[ $path.'/'.$items[$idx]['name'] ]);
}
}
$multi = true;
}
if ($multi) {
$newitem = &$array[ $items[$idx]['name'] ][];
} else {
$newitem = &$array[ $items[$idx]['name'] ];
}
if (isset($items[$idx]['children']) && is_array($items[$idx]['children'])) {
$newitem = $this->recursive_parseLevel($items[$idx]['children'], $attrs, $path.'/'.$items[$idx]['name']);
} else if (isset($items[$idx]['tagData'])) {
$newitem = $items[$idx]['tagData'];
} else {
$newitem = false;
}
if (isset($items[$idx]['attrs']) && is_array($items[$idx]['attrs']) && count($items[$idx]['attrs'])) {
$attrpath = $path.'/'.$items[$idx]['name'];
if ($multi) {
$attrpath .= '/'.(count($array[ $items[$idx]['name'] ])-1);
}
foreach ($items[$idx]['attrs'] as $name=>$value) {
$attrs[ $attrpath ][ strtolower($name) ] = $value;
}
}
}
return $array;
}
function parseAdvanced($strInputXML) {
$array = $this->parse($strInputXML);
$this->attributes = array();
return $this->data = $this->recursive_parseLevel($array, $this->attributes);
}
}
/*
Return a much more manageable assoc array with module data.
*/
class xml2ModuleArray extends xml2Array {
function parseModulesXML($strInputXML) {
$array = $this->parseAdvanced($strInputXML);
if (isset($array['xml'])) {
foreach ($array['xml'] as $key=>$module) {
if ($key == 'module') {
// copy the structure verbatim
$modules[ $module['name'] ] = $module;
}
}
}
// if you are confused about what's happening below, uncomment this why we do it
// echo "<pre>"; print_r($arrOutput); echo "</pre>";
// ignore the regular xml garbage ([0]['children']) & loop through each module
if(!is_array($arrOutput[0]['children'])) return false;
foreach($arrOutput[0]['children'] as $module) {
if(!is_array($module['children'])) return false;
// loop through each modules's tags
foreach($module['children'] as $modTags) {
if(isset($modTags['children']) && is_array($modTags['children'])) {
$$modTags['name'] = $modTags['children'];
// loop if there are children (menuitems and requirements)
foreach($modTags['children'] as $subTag) {
$subTags[strtolower($subTag['name'])] = $subTag['tagData'];
}
$$modTags['name'] = $subTags;
unset($subTags);
} else {
// create a variable for each tag we find
$$modTags['name'] = $modTags['tagData'];
}
}
// now build our return array
$arrModules[$RAWNAME]['rawname'] = $RAWNAME; // This has to be set
$arrModules[$RAWNAME]['displayName'] = $NAME; // This has to be set
$arrModules[$RAWNAME]['version'] = $VERSION; // This has to be set
$arrModules[$RAWNAME]['type'] = isset($TYPE)?$TYPE:'setup';
$arrModules[$RAWNAME]['category'] = isset($CATEGORY)?$CATEGORY:'Unknown';
$arrModules[$RAWNAME]['info'] = isset($INFO)?$INFO:'http://www.freepbx.org/wiki/'.$RAWNAME;
$arrModules[$RAWNAME]['location'] = isset($LOCATION)?$LOCATION:'local';
$arrModules[$RAWNAME]['items'] = isset($MENUITEMS)?$MENUITEMS:null;
$arrModules[$RAWNAME]['requirements'] = isset($REQUIREMENTS)?$REQUIREMENTS:null;
$arrModules[$RAWNAME]['md5sum'] = isset($MD5SUM)?$MD5SUM:null;
//print_r($arrModules);
//unset our variables
unset($NAME);
unset($VERSION);
unset($TYPE);
unset($CATEGORY);
unset($AUTHOR);
unset($EMAIL);
unset($LOCATION);
unset($MENUITEMS);
unset($REQUIREMENTS);
unset($MD5SUM);
}
//echo "<pre>"; print_r($arrModules); echo "</pre>";
return $arrModules;
}
}
- xml文件生成的两个函数
- C#生成XML文件的 函数
- XML文件的生成
- Xml文件的生成
- 将两个不同格式的XML文件,进行节点对照,并生成一个用于对照功能的XML
- PHP生成的XML文件
- 生成XML文件的步骤
- Delphi读写XML的两个函数
- ASP生成随机密码的两个函数:
- ASP生成随机密码的两个函数
- 对比两个xml文件的标签
- 配置NHibernate的两个XML文件
- php生成csv文件的两个类
- XML文件生成XSD文件的方法
- java 关于xml的注解,自动生成xml文件 - @XML***
- java 关于xml的注解,自动生成xml文件 - @XML***
- java 关于xml的注解,自动生成xml文件 - @XML***
- java 关于xml的注解,自动生成xml文件 - @XML
- 【转】C++程序员成长之路
- Centos-Permission denied,selinux rpm问题 Iptraf流量监控
- 俄罗斯的官方节日——程序员节
- 别以为你中文标英文很牛B,看看人家用怎样用英文标中文的
- log4j详解与实战
- xml文件生成的两个函数
- Hashtable用法
- 坚持就是胜利
- Sortedlist用法
- redhat enterprise linux as4 update 8(RHEL AS4 U8) 下载
- csdn的心理测试挺准的
- 每周四十小时,你有多少是在为自己干活?
- VC2005之远程调试
- 赞刘伯承