簡體   English   中英

在PHP中格式化XML

[英]Format XML In PHP

我有一個轉換為XML的數組,我為XML編寫了代碼。 這里是:

.....
$xml = new SimpleXMLElement('<?xml version="1.0"?><services></services>');

    $subnode=$xml->addChild("service");
    $this->array_to_xml($test_array,$subnode);
    $string = $xml->asXML(); 
    $result = $string;  
    oci_free_statement($stmt);
    oci_close($this->_conn);
    return $result;
}

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


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

結果是:

<services>
    <service>
        <service>
            <id>1</id>
            <name>INTERNET<\/name>
            <attribute>
                <attributeName>DOWNLOAD</attributeName>
                <attributeValue>3072</attributeValue>
            </attribute>
            <attribute>
                <attributeName>UPLOAD<\/attributeName>
                <attributeValue>512<\/attributeValue>
            </attribute>
        </service>
        <service>
            <id>1<\/id>
            <name>INTERNET<\/name>
            <attribute>
                <attributeName>DOWNLOAD<\/attributeName>
                <attributeValue>5120<\/attributeValue>
            </attribute>
            <attribute>
                <attributeName>UPLOAD<\/attributeName>
                <attributeValue>1024<\/attributeValue>
            </attribute>
        </service>
        <service>
            <id>2<\/id>
            <name>VOICE<\/name>
        </service>
    </service>
</services>

問題是我想刪除第二行中的service標簽。 我想要的結果是..

<?xml version="1.0"?>
<services>
    <service>
        <id>1</id>
        <name>INTERNET<\/name>
        <attribute>
            <attributeName>DOWNLOAD</attributeName>
            <attributeValue>3072</attributeValue>
        </attribute>
        <attribute>
            <attributeName>UPLOAD<\/attributeName>
            <attributeValue>512<\/attributeValue>
        </attribute>
    </service>
    <service>
        <id>1<\/id>
        <name>INTERNET<\/name>
        <attribute>
            <attributeName>DOWNLOAD<\/attributeName>
            <attributeValue>5120<\/attributeValue>
        </attribute>
        <attribute>
            <attributeName>UPLOAD<\/attributeName>
            <attributeValue>1024<\/attributeValue>
        </attribute>
    </service>
    <service>
        <id>2<\/id>
        <name>VOICE<\/name>
    </service>
</services>

只需刪除$subnode=$xml->addChild("service"); $this->array_to_xml($test_array,$subnode);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM