xml简单功能

前端之家收集整理的这篇文章主要介绍了xml简单功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<?PHP
header("Content-Type:text/xml;");

$product = array(
        array(
                'p_id' => '1','product_type' => '1','sub_category' => '1','name' => '','price' => '','pay_category' => '','rating' => '','icon_url' => '','short_description' => '','app_size' => '','source_type' => '','author_name' => '','packagename' => '','ratings_count' => '','is_star' => '','version_code' => '','hot_rating' => '','special_flag' => '','download_count' => '','version_name' => '','rsa_md5' => '',),array(
                'p_id' => '1',)
    );



/**
 * 创建XML
 * @param string $root
 * @param string $sub
 * @param array $data
 * 
 * @return xml
 */
function createXml( $root,$sub,$data)
{
    $dom = new DomDocument('1.0','utf-8');
    //创建根节点
    $responseTag = $dom->createElement('response');
    $dom->appendchild($responseTag);
    
    //创建根节点
    $rootTag = $dom->createElement($root);
    $responseTag->appendchild($rootTag);    
    
    foreach($data as $key=>$val){
        //创建子节点
        $subTag = $dom->createElement($sub);
        
        if(is_array($val))
        {
            foreach($val as $k=>$v)
            {
                //子节点属性
                $subTag->setAttribute($k,$v);
            }
        }
        $rootTag->appendchild($subTag);
    }    
    return $dom->saveXML();    
}

$xml = createXml( 'products','product',$product);

echo $xml;

/**
 * 
 * @param unknown_type $element
 * @param unknown_type $item
 */
function createXmlElement($element,$item)
{
    
}

猜你在找的XML相关文章