在XML PHP中添加Child作为列名和列值

时间:2015-01-29 06:48:59

标签: php sql xml oracle

我有一个包含此查询的表,我在此查询中设置代码= SPEED和代码= PSTN。我设置代码= SPEED:

SELECT ID,NAME,xs1 as download, xs2 as upload
  FROM sc_params
  WHERE rfen = 'SERVICE_REQUESTED'
  AND code = 'SPEED' 
  OR code IN
  ( 
    SELECT code 
    FROM sc_params 
    WHERE rfen = 'SERVICE_REQUESTED'
    AND id = (
            SELECT parent 
             FROM sc_params 
             WHERE rfen = 'SERVICE_REQUESTED' 
             AND code = 'SPEED' )
  );

结果是:

ID    NAME       DOWNLOAD    UPLOAD
-----------------------------------
1     Internet   null        null
6     SPEED      1024        256

如果我设置代码= PSTN,这就是结果:

 ID    NAME       DOWNLOAD    UPLOAD
 -----------------------------------
 1     VOICE     null        null
 6     PSTN      null        null        

我将此查询添加到PHP,这里是代码:

$serviceName="PSTN, SPEED";    
public function get_xml_product($serviceName) {
            $product    = Array();
            $idx        = -1;
            $explodeResult=explode(", ",$serviceName);
            foreach($explodeResult as $value)
            {
                  $sql = "SELECT id,parent,code,name,xs1, xs2 
                           FROM sc_params
                          WHERE rfen = 'SERVICE_REQUESTED'
                          AND code = '".$value."' 
                            OR code IN
                            ( 
                                SELECT code 
                                 FROM sc_params 
                                 WHERE rfen = 'SERVICE_REQUESTED'
                                 AND id = (
                                SELECT parent 
                                     FROM sc_params 
                                     WHERE rfen = 'SERVICE_REQUESTED' 
                                     AND code = '".$value."' )
                            )";  
                $stmt = oci_parse($this->_conn,$sql);
                $rs = oci_execute($stmt);
                if (!$rs) {
                    $error = oci_error();
                    $this->_err = $error;
                    return false;
                }
                while ($data = oci_fetch_array($stmt, OCI_BOTH)) {
                    if($data['PARENT'] == 0) {
                        $idx++;
                        $idx2 = 0;
                        $product[$idx]['id']        = $data['ID'];
                        $product[$idx]['name']      = $data['NAME'];
                    }
                else {
                    $product[$idx]['download'][$idx2]['name']   = 'DOWNLOAD';
                    $product[$idx]['download'][$idx2]['value'] = $data['DOWNLOAD'];
                    $product[$idx]['upload'][$idx2]['name']   = 'UPLOAD';
                    $product[$idx]['upload'][$idx2]['value'] = $data['UPLOAD'];
                    $idx2++;

                }
            }
//to make xml

        $xml = new SimpleXMLElement("<services/>");
        foreach($product as $key => $value) {
            if (is_array($value)) {
                $subnode = $xml->addChild("service");
                $this->array_to_xml($value, $subnode);
            } else {
               $xml->addChild(htmlspecialchars("$key"),htmlspecialchars("$value"));
            }
        }
        $string = $xml->asXML();
        $result = htmlentities($string);

        oci_free_statement($stmt);
        oci_close($this->_conn);
        return $result;
    }

我也将函数数组设为xml,这里是:

public function array_to_xml($product, &$xml) {
        foreach($product as $key => $value) {
            if (is_array($value)) {
                $subnode = $xml->addChild("service");
                array_to_xml($value, $subnode);
            } else {
               $xml->addChild(htmlspecialchars("$key"),htmlspecialchars("$value"));
            }
        }
        return $xml;
    }

从上面的代码中,我得到了xml中的结果:

<xml version=1.0>
<services>
    <service>
        <id> 2 </id>
        <name> VOICE </name>
    </service>
    <service>
        <id> 1 </id>
        <name> INTERNET </name>
    </service>
</services>

但是,我想要显示的结果是这样的:

<?xml version="1.0"?>
         <services>
             <service>
                 <id>1</id>
                 <name>INTERNET</name>
                 <attribute>
                    <name>DOWNLOAD</name>
                    <value>1024</value>
                 </attribute>
                 <attribute>
                    <name>UPLOAD</name>
                    <value>512</value>
                 </attribute>
             </service>
             <service>
                <id>2</id>
                <name>VOICE</name>
             </service>
         </services>

1 个答案:

答案 0 :(得分:0)

据我了解,您只需传递从查询中检索到的有关下载,上传的信息:

if ($data['PARENT'] == 0) {
        $idx++;
        $idx2 = 0;
        $product[$idx]['id']        = $data['ID'];
        $product[$idx]['name']      = $data['NAME'];
        $product[$idx]['attribute'] = array('name' => 'DOWNLOAD', 'value' => $data['DOWNLOAD']); // <= HERE !
        $product[$idx]['attribute'] = array('name' => 'UPLOAD', 'value' => $data['UPLOAD']); // <= HERE !
    }