Suhail Gupta
Suhail Gupta

Reputation: 23206

Incomplete XML file generated

I need to generate a XML file. For generating I created the following class.

class MXML extends DOMDocument {

    private $version,$encodingType,$rootNode,$videoParam,$encodingFormat,$audioParam;

    /**
     * Initilizes the DOM with version number and encoding
     * @param  $version
     * @param  $encoding
     */
    public function __construct($version,$encoding) {
        parent::__construct($version,$encoding);
        $this->formatOutput = true; // Nicely formats output with indentation and extra space.
    }

    /**
     * Sets the root tag for XML
     * @param unknown $node
     */
    public function setRoot($node) {
        $this->rootNode = $this->createElement($node);
        $this->appendChild($this->rootNode);

    }

    /**
     * Sets the video param node.
     * @param unknown $videoParamNode
     */
    public function setVideoParamNode($videoParamNode) {
        $this->videoParam = $this->createElement($videoParamNode);
        $this->rootNode->appendChild($this->videoParam);
    }

    /**
     * Sets the audio param node
     * @param unknown $audioParamNode
     */
    public function createAudioParamNode($audioParamNode) {
        $this->audioParam = $this->createElement($audioParamNode);
        $this->rootNode = $this->appendChild($this->audioParam);
    }

    /**
     * Set Encoding Format. Example : MPEG4,H264
     * @param unknown $eformat
     */
    public function setEncodingFormat($eformat) {
        $this->encodingFormat = $this->createElement($eformat);
        $this->videoParam->appendChild($this->encodingFormat);
    }

    /**
     * It creates a container in DOM with Parent Node at the top and children containg respective values.
     * 
     *    <Parameter>
     *       <IQuant>2</IQuant>
     *       <PQuant>2</PQuant>
     *       <FramesToSkip>0</FramesToSkip>
     *       <PBetweenI>50</PBetweenI>
     *       <FrameRate>24.00</FrameRate>
     *       <SearchWindow>31</SearchWindow>
     *    </Parameter>
     *    
     * @param array $containerArray
     * $containerArray = array(
     *      'Parent' => 'Parameter'
     *      'Children' => array(
     *                      'IQuant'            => 2
     *                      'PQuant'            => 3        
     *                      'FramesToSkip'      => 0
     *                      'FrameRate'         => 21.00
     *                    )
     * );
     */
    public function createContainer($containerArray) {
        $parentNode = $containerArray['Parent'];
        $childrenList = $containerArray['Children'];
        $parentNode = $this->createElement($parentNode);
        $this->encodingFormat->appendChild($parentNode);

        foreach ($childrenList as $n => $v) {
            $n = $this->createElement($n);
            $n->appendChild($this->createTextNode($v));                     
        }           
    }

    /**
     * Returns the XML, or FALSE if an error occurred.
     * @return Returns the XML (as string), or FALSE if an error occurred.
     */
    public function getXML() {
        return $this->saveXML();
    }
};

The class has a method createContainer which accepts an array with the name of Parent node and children in the form of associative array.

The calls are made as follows:

    $xml = new MXML('1.0', "iso-8859-1");

    $xml->setRoot("Cutkompress-Parameters");
    $xml->setVideoParamNode("Video-Params");
    $xml->setEncodingFormat("EncodingFormat-MPEG-4Part2");
    $xml->createContainer($this->getParameterArray());
    $xstring = $xml->getXML();

The calls go without a crash but the XML generated is not fine. It looks as follows:

    <?xml version="1.0" encoding="iso-8859-1"?> <Cutkompress-Parameters> <Video-Params> 
 <EncodingFormat-MPEG-4Part2> <Parameter/> </EncodingFormat-
 MPEG-4Part2> </Video-Params> </Cutkompress-Parameters>

Other tags are fine but the Parameter tag created from createContainer generates an incomplete XML without the parameters.

What could be the reason for this?

Upvotes: 1

Views: 69

Answers (1)

user3942918
user3942918

Reputation: 26375

You're not adding the children to the parent node, only the parent to the encodingFormat and text to the children. Add $parentNode->appendChild($n) while you're iterating through your $childrenList.

Upvotes: 1

Related Questions