简体   繁体   中英

selfwritten xml generator doesnt listen to the xml output

I have a page containing the following:

$xml = new DOMDocument('1.0', 'UTF-8');
// Set de format van het xml document
$xml->formatOutput = true;
$test = $xml->createElement('test');
$xml->appendChild($test);
$test->createElement('name','pierre');

XMLController::CreateTestXML($xml);

I have written 2 functions.

1.) CreateMultipleElements()
2.) CreateTestXML()

CreateTestXML() function contains:
This function is meant to be adding something to the xml and it does:

public static function CreateTestXML(&xml){
    $objectArray = self::CreateMultipleElements(
            $xml, 
            array(
                "Persoon" => null,
                "Paard" => "Wit"
            ),
            $parent = null, 
            $attributes = array(
                "Persoon" => array(
                    "Type" => "NPS"
                )
            )
        );
}

Then we have the function who actually creates the nodes and the elements which is working properly:

public static function CreateMultipleElements(&$xml,$objectNameValue,$parent = null, $attributes = null){
        $xmlObjecten = array();
        foreach($objectNameValue as $objectName => $nodeValue){
            $object = $xml->createElement($objectName,$nodeValue);
            if(isset($attributes[$objectName])){
                foreach($attributes[$objectName] as $key => $value){
                    $object->setAttribute($key,$value);
                }
            }
            if($parent === null){
                $xmlObjecten[$objectName] = $xml->appendChild($object);
            }
            else{
                $xmlObjecten[$objectName] = $parent->appendChild($object);
            }
        }
        return $xmlObjecten;
    }

The xml output would look like this:

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <name>pierre</name>
</test>
<persoon type="NPS"></persoon><paard>wit</paard>

So it seems like the generated part is ignoring the format output please help

Your XML is not valid: it cannot have multiple root nodes. You must only have one node (ie <test> ).

The behavior for multiple root nodes in a XML is certainly undefined, hence the "partially formatted output".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM