note 85948 modified in function.xml-parse-into-struct by danbrown
| From: | danbrown@php.net | Date: | Thu, 25 Sep 2008 23:43:38 +0000 |
| Subject: | note 85948 modified in function.xml-parse-into-struct by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-144718@lists.php.net to get a copy of this message | ||
here you can [re]assemble xml from a xml_parse_into_struct generated array: ie array2xml
<?php
$xml_parser = xml_parser_create( 'UTF-8' ); // UTF-8 or ISO-8859-1
xml_parser_set_option( $xml_parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $xml_parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $xml_parser, $my_text_xml, $aryXML );
xml_parser_free($xml_parser);
//now use aryXML array to xml string:
$o='';
foreach( $aryXML as $tag ){
//tab space:
for($i=1; $i < $tag['level'];$i++)
$o.="\t";
if($tag['type']!='close'){
if($tag['type']!='cdata')
$o.='<'.$tag['tag'];
if(isset($tag['attributes'])){
foreach($tag['attributes'] as $attr=>$aval){
$o.=' '.$attr.'="'.$aval.'"';
}
}
if($tag['type']!='cdata'){
$o.=($tag['type']=='complete')?'/>'."\n":'>'."\n";
}
$o.=(isset($tag['value']))?$tag['value']:'';
}else{
$o.='</'.$tag['tag'].'>';
}
}
?>
--was--
here you can [re]assemble xml from a xml_parse_into_struct generated array: ie array2xml
$xml_parser = xml_parser_create( 'UTF-8' ); // UTF-8 or ISO-8859-1
xml_parser_set_option( $xml_parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $xml_parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $xml_parser, $my_text_xml, $aryXML );
xml_parser_free($xml_parser);
//now use aryXML array to xml string:
$o='';
foreach( $aryXML as $tag ){
//tab space:
for($i=1; $i < $tag['level'];$i++)
$o.="\t";
if($tag['type']!='close'){
if($tag['type']!='cdata')
$o.='<'.$tag['tag'];
if(isset($tag['attributes'])){
foreach($tag['attributes'] as $attr=>$aval){
$o.=' '.$attr.'="'.$aval.'"';
}
}
if($tag['type']!='cdata'){
$o.=($tag['type']=='complete')?'/>'."\n":'>'."\n";
}
$o.=(isset($tag['value']))?$tag['value']:'';
}else{
$o.='</'.$tag['tag'].'>';
}
}
http://php.net/manual/en/function.xml-parse-into-struct.php