我需要生成一个XML
文件。为了生成,我创建了以下类。
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();
}
};
该类有一个方法createContainer
,它接受一个名为Parent
节点的数组和以关联数组形式的子节点。
调用如下:
$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();
调用没有崩溃但生成的XML不正常。它看起来如下:
<?xml version="1.0" encoding="iso-8859-1"?> <Cutkompress-Parameters> <Video-Params>
<EncodingFormat-MPEG-4Part2> <Parameter/> </EncodingFormat-
MPEG-4Part2> </Video-Params> </Cutkompress-Parameters>
其他标记很好,但Parameter
标记创建的createContainer
标记会生成不完整的XML
,而且没有参数。
这可能是什么原因?
答案 0 :(得分:1)
您不会将子项添加到父节点,只将子项添加到encodingFormat,将文本添加到子项。在您浏览$parentNode->appendChild($n)
$childrenList.