DimasW
DimasW

Reputation: 181

Format XML In PHP

I have an array that convert to XML, i made code for XML. here it is:

.....
$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"));
        }
    }
}

And the result is:

<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>

The problem is i want to delete service tag in second line. The result that i want is..

<?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>

Upvotes: 1

Views: 41

Answers (1)

Capsule
Capsule

Reputation: 6159

Just remove the line $subnode=$xml->addChild("service"); before $this->array_to_xml($test_array,$subnode);

Upvotes: 1

Related Questions