/** * @covers ByJG\AnyDataset\Repository\AnyDataset::XML * @covers ByJG\AnyDataset\Repository\AnyDataset::getDomObject */ public function testXML() { $this->object->appendRow(); $this->object->addField('field', 'value'); $xmlDom = \ByJG\Util\XmlUtil::createXmlDocumentFromStr('<?xml version="1.0" encoding="utf-8"?>' . '<anydataset>' . '<row>' . '<field name="field">value</field>' . '</row>' . '</anydataset>'); $xmlDomValidate = \ByJG\Util\XmlUtil::createXmlDocumentFromStr($this->object->xml()); $this->assertEquals($xmlDom, $xmlDomValidate); }
/** * * @param DOMDocument $xml * @param string $rowNode * @param string[] $colNode * @param array $registerNS * @throws DatasetException * @throws InvalidArgumentException */ public function __construct($xml, $rowNode, $colNode, $registerNS = null) { if (!is_array($colNode)) { throw new DatasetException("XmlDataset constructor: Column nodes must be an array."); } if ($xml instanceof DOMDocument) { $this->_domDocument = $xml; } else { $this->_domDocument = XmlUtil::createXmlDocumentFromStr($xml); } if (is_null($registerNS)) { $registerNS = array(); } if (!is_array($registerNS)) { throw new InvalidArgumentException('The parameter $registerNS must be an associative array'); } $this->_registerNS = $registerNS; $this->_rowNode = $rowNode; $this->_colNodes = $colNode; }
/** * Get the \DOMElement row objet * @return \DOMElement */ public function getDomObject() { if (is_null($this->_node)) { $this->_node = XmlUtil::createXmlDocumentFromStr("<row />"); $root = $this->_node->getElementsByTagName("row")->item(0); foreach ($this->_row as $key => $value) { if (!is_array($value)) { $field = XmlUtil::createChild($root, "field", $value); XmlUtil::addAttribute($field, "name", $key); } else { foreach ($value as $valueItem) { $field = XmlUtil::createChild($root, "field", $valueItem); XmlUtil::addAttribute($field, "name", $key); } } } } return $this->_node; }
/** * Returns the AnyDataset XmlDocument representive object * @return \DOMDocument XmlDocument object */ public function getDomObject() { $anyDataSet = XmlUtil::createXmlDocumentFromStr("<anydataset/>"); $nodeRoot = $anyDataSet->getElementsByTagName("anydataset")->item(0); foreach ($this->_collection as $sr) { $row = $sr->getDomObject(); $nodeRow = $row->getElementsByTagName("row")->item(0); $newRow = XmlUtil::createChild($nodeRoot, "row"); XmlUtil::addNodeFromNode($newRow, $nodeRow); } return $anyDataSet; }
<?php require "vendor/autoload.php"; $xml = \ByJG\Util\XmlUtil::createXmlDocumentFromStr('<root />'); $myNode = \ByJG\Util\XmlUtil::createChild($xml->documentElement, 'mynode'); \ByJG\Util\XmlUtil::createChild($myNode, 'subnode', 'text'); \ByJG\Util\XmlUtil::createChild($myNode, 'subnode', 'more text'); $otherNode = \ByJG\Util\XmlUtil::createChild($myNode, 'othersubnode', 'other text'); \ByJG\Util\XmlUtil::addAttribute($otherNode, 'attr', 'value'); echo $xml->saveXML(); print_r(\ByJG\Util\XmlUtil::xml2Array($xml)); $node = \ByJG\Util\XmlUtil::selectSingleNode($xml, '//subnode'); echo $node->nodeValue . "\n"; $node = \ByJG\Util\XmlUtil::selectSingleNode($myNode, '//subnode'); echo $node->nodeValue . "\n"; $nodeList = \ByJG\Util\XmlUtil::selectNodes($xml, '//subnode'); foreach ($nodeList as $node) { echo $node->nodeName; } echo "\n"; $nodeList = \ByJG\Util\XmlUtil::selectNodes($myNode, '//subnode'); foreach ($nodeList as $node) { echo $node->nodeName; } echo "\n"; \ByJG\Util\XmlUtil::addNamespaceToDocument($xml, 'my', 'http://www.example.com/mytest/'); echo $xml->saveXML() . "\n"; \ByJG\Util\XmlUtil::createChild($xml->documentElement, 'nodens', 'teste', 'http://www.example.com/mytest/'); \ByJG\Util\XmlUtil::createChild($xml->documentElement, 'my:othernodens', 'teste'); echo $xml->saveXML() . "\n"; $nodeList = \ByJG\Util\XmlUtil::selectNodes($xml, '//my:othernodens', ['my' => 'http://www.example.com/mytest/']);