本文整理汇总了PHP中DomDocument::importNode方法的典型用法代码示例。如果您正苦于以下问题:PHP DomDocument::importNode方法的具体用法?PHP DomDocument::importNode怎么用?PHP DomDocument::importNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DomDocument
的用法示例。
在下文中一共展示了DomDocument::importNode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: DomDocument
function props_to_xml()
{
# make the source xml
# make doc and root
$xml = new DomDocument();
$root = $xml->createElement('request');
$root->setAttribute('controller', params('controller'));
$root->setAttribute('action', params('action'));
$root = $xml->appendChild($root);
# unpack the props into xml
foreach ($this->props as $k => $v) {
# if it will become xml, do that, otherwise make a dumb tag
if (is_object($v) && method_exists($v, 'to_xml')) {
$obj_xml = $v->to_xml(array(), true, true);
$obj_xml = $xml->importNode($obj_xml->documentElement, true);
$root->appendChild($obj_xml);
} else {
$node = $xml->createElement($k);
if (strpos($v, '<') !== false || strpos($v, '>') !== false || strpos($v, '&') !== false) {
$cdata = $xml->createCDATASection($v);
} else {
$cdata = $xml->createTextNode($v);
}
$node->appendChild($cdata);
$node = $root->appendChild($node);
}
}
return $xml;
}
示例2: renderDocument
/**
* Renders the gathered nodes into a XML document.
*
* @param string $xml Text to be imported to a DomDocument.
*
* @return XmlManipulator
*/
public function renderDocument($xml)
{
$this->document = $this->initDomDocument($xml);
foreach ($this->nodes as $nodeXml) {
$mergeDoc = $this->initDomDocument($nodeXml);
$importNode = $mergeDoc->getElementsByTagNameNS('http://symfony.com/schema/dic/constraint-mapping', 'class')->item(0);
$importNode = $this->document->importNode($importNode, true);
$this->document->documentElement->appendChild($importNode);
}
return $this;
}
示例3: response
static function response($status, $status_message, $domdocument = null)
{
$dom = new DomDocument('1.0', 'UTF-8');
$results = $dom->appendChild($dom->createElement('response'));
$results->appendChild($dom->createElement('status', $status));
$results->appendChild($dom->createElement('status_message', $status_message));
if (is_null($domdocument)) {
$domdocument = new DomDocument('1.0', 'UTF-8');
$domdocument->appendChild($domdocument->createElement('data'));
}
if ($domdocument->documentElement->nodeName === 'data') {
$newdom = $domdocument;
} else {
$newdom = new DOMDocument('1.0', 'UTF-8');
$data = $newdom->appendChild($newdom->createElement('data'));
foreach ($domdocument->documentElement->childNodes as $domElement) {
$domNode = $newdom->importNode($domElement, true);
$data->appendChild($domNode);
}
}
$import = $dom->importNode($newdom->documentElement, TRUE);
$results->appendChild($import);
$dom->formatOutput = true;
echo $dom->saveXML();
}
示例4: truncatehtml
public function truncatehtml($html, $minimum)
{
$oldDocument = new \DomDocument();
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
$oldDocument->loadHTML('<div>' . $html . '</div>');
// remove DOCTYPE, HTML and BODY tags
$oldDocument->removeChild($oldDocument->firstChild);
$oldDocument->replaceChild($oldDocument->firstChild->firstChild->firstChild, $oldDocument->firstChild);
$currentLength = 0;
// displayed text length (without markup)
$newDocument = new \DomDocument();
foreach ($oldDocument->documentElement->childNodes as $node) {
if ($node->nodeType != 3) {
// not text node
$imported = $newDocument->importNode($node, true);
$newDocument->appendChild($imported);
// copy original node to output document
$currentLength += strlen(html_entity_decode($imported->nodeValue));
if ($currentLength >= $minimum) {
// check if the minimum is reached
break;
}
}
}
$output = $newDocument->saveHTML();
return html_entity_decode($output);
}
示例5: injectText
protected function injectText(DOMNode $parent, DOMNode $sibling, $string)
{
$textNode = new DOMText();
$textNode->nodeValue = $string;
$textNode = $this->templateDocument->importNode($textNode);
$parent->insertBefore($textNode, $sibling);
}
示例6: replaceDomNode
/**
* This method replace the <abbr> tag with a new html tag.
* @param DomDocument $parentDom
* @param array $wikiInfo
* @param string $text
* @param DomElement $oldNode
*/
private function replaceDomNode($parentDom, $wikiInfo, $text, $oldNode)
{
$wikiInfo = (array) $wikiInfo;
$urlPattern = stripslashes(get_option('wikiUrlPattern'));
$newUrl = str_replace('$articleurl', $wikiInfo['wikiurl'], $urlPattern);
$newUrl = str_replace('$title', $wikiInfo['title'], $newUrl);
$newUrl = str_replace('$text', $text, $newUrl);
$newDom = new DOMDocument();
$newDom->loadHTML($newUrl);
$node = $newDom->getElementsByTagName("a")->item(0);
$oldNode->parentNode->replaceChild($parentDom->importNode($node, true), $oldNode);
}
示例7: getXmlForConfig
/**
* Returns a SimpleXMLElement for the given config.
*
* @see Zend_Build_Resource_Interface
* @param Zend_Build_Resource_Interface Resource to convert to XML
* @return string String in valid XML 1.0 format
*/
public static function getXmlForConfig(Zend_Config $config)
{
// First create the empty XML element
$xml = self::_arrayToXml($config->toArray(), new SimpleXMLElement('<' . self::CONFIG_XML_ROOT_ELEMENT . '/>'), true);
// Format output for readable XML and save to $filename
$dom = new DomDocument('1.0');
$domnode = dom_import_simplexml($xml);
$domnode = $dom->importNode($domnode, true);
$domnode = $dom->appendChild($domnode);
$dom->formatOutput = true;
return $dom->saveXML();
}
示例8: SimpleXml_Merge
/**
* Merge two SimpleXMLElements into a single object
* @param SimpleXMLElement $xml1
* @param SimpleXMLElement $xml2
*/
public static function SimpleXml_Merge(SimpleXMLElement &$xml1, SimpleXMLElement $xml2)
{
$dom1 = new DomDocument();
$dom2 = new DomDocument();
$dom1->loadXML($xml1->asXML());
$dom2->loadXML($xml2->asXML());
$xpath = new domXPath($dom2);
$xpathQuery = $xpath->query('/*/*');
for ($i = 0; $i < $xpathQuery->length; $i++) {
$dom1->documentElement->appendChild($dom1->importNode($xpathQuery->item($i), true));
}
$xml1 = simplexml_import_dom($dom1);
}
示例9: unmarshalRoot
public static function unmarshalRoot($string)
{
$dom = new \DomDocument();
$dom->loadXml($string);
$xpath = new \DomXPath($dom);
$query = "child::*";
$childs = $xpath->query($query);
$binding = array();
foreach ($childs as $child) {
$legko = new Bind();
$lDom = new \DomDocument();
$lDom->appendChild($lDom->importNode($child, true));
$binding = $legko->unmarshal($lDom->saveXml());
}
return $binding;
}
示例10: simplexml_mergeLand
function simplexml_mergeLand(SimpleXMLElement &$xml1, SimpleXMLElement $xml2)
{
// convert SimpleXML objects into DOM ones
$dom1 = new DomDocument();
$dom2 = new DomDocument();
$dom1->loadXML($xml1->asXML());
$dom2->loadXML($xml2->asXML());
// pull all child elements of second XML
$xpath = new domXPath($dom2);
$xpathQuery = $xpath->query('/propertyList/land');
for ($i = 0; $i < $xpathQuery->length; $i++) {
// and pump them into first one
$dom1->documentElement->appendChild($dom1->importNode($xpathQuery->item($i), true));
}
// for($i = 0; $i < $xpathQuery->length; $i++)
$xml1 = simplexml_import_dom($dom1);
}
示例11: matches
public function matches($schemaFile)
{
foreach ($this->xml as $id => $dom) {
$configElement = $dom->getElementsByTagName('config');
if (1 !== $configElement->length) {
throw new \InvalidArgumentException(sprintf('Can only test a file if it contains 1 <config> element, %d given', $configElement->length));
}
$configDom = new \DomDocument();
$configDom->appendChild($configDom->importNode($configElement->item(0), true));
libxml_use_internal_errors(true);
if (!$configDom->schemaValidate($schemaFile)) {
$this->errors = libxml_get_errors();
$this->failingElement = $id;
return false;
}
}
return true;
}
示例12: filterTables
public function filterTables()
{
$this->filter = $this->query->query(sprintf("//value[@struct-name='db.mysql.Table']"));
if ($this->filter instanceof DOMNodeList and $this->filter->length > 0) {
$f = $this->filter;
for ($i = 0; $i < $f->length; $i++) {
$owner = $f->item($i)->getAttribute("id");
$x = new DOMXPath($this->reader);
$res = $x->query("//value[@id='{$owner}']/value[@key='columns']");
$y = new DOMXPath($this->reader);
$fks = $y->query("//value[@id='{$owner}']/value[@key='foreignKeys']");
$node = $f->item($i);
$node->setAttribute("key", "tables");
if ($node) {
$node = $f->item($i);
$cols = $this->writer->createElement("value");
$cols->setAttribute("key", "columns");
for ($j = 0; $j < $res->length; $j++) {
$cols->appendChild($this->writer->importNode($res->item($j), true));
}
$this->writer->documentElement->appendChild($this->writer->importNode($node, true));
$this->writer->documentElement->childNodes->item($i)->appendChild($cols);
$fk = $this->writer->createElement("value");
$fk->setAttribute("key", "foreignkeys");
for ($j = 0; $j < $fks->length; $j++) {
$fk->appendChild($this->writer->importNode($fks->item($j), true));
}
$this->writer->documentElement->childNodes->item($i)->appendChild($fk);
}
}
$res = $this->query->query("//value[@key='catalog']/value[@key='schemata']/value[@struct-name='db.mysql.Schema']/value[@key='comment']/text()");
if ($res->length > 0) {
$json = preg_replace('/([{,])(\\s*)([^"]+?)\\s*:/', '$1"$3":', trim((string) $res->item(0)->nodeValue));
$obj = json_decode($json, true);
$control = $this->writer->createElement("application");
$control->setAttribute("key", "install");
foreach ($obj as $key => $val) {
$control->setAttribute($key, $val);
}
$this->writer->documentElement->lastChild->appendChild($control);
}
}
return $this;
}
示例13: parse_xhtmlish_content
/**
* given some content that might be html or xhtml, try to coerce it to xhtml and return it.
*
* @param string $content some html or xhtmlish content
*
* @return xhtml content or false for unmodified text content
*/
public static function parse_xhtmlish_content($content, $viewid = null)
{
$dom = new DomDocument();
$topel = $dom->createElement('tmp');
$tmp = new DomDocument();
if (strpos($content, '<') === false && strpos($content, '>') === false) {
return false;
}
if (@$tmp->loadXML('<div>' . $content . '</div>')) {
$topel->setAttribute('type', 'xhtml');
$content = $dom->importNode($tmp->documentElement, true);
$content->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$topel->appendChild($content);
// if that fails, it could still be html
// DomDocument::loadHTML() parses the input as iso-8859-1 if no
// encoding is declared. Since we are only loading a HTML fragment
// there is no encoding declared which results in garbled output
// since the content is actually in utf-8. To work around this
// we force the encoding by appending an xml declaration.
// see http://php.net/manual/de/domdocument.loadhtml.php#95251
} else {
if (@$tmp->loadHTML('<?xml encoding="UTF-8"><div>' . $content . '</div>')) {
$xpath = new DOMXpath($tmp);
$elements = $xpath->query('/html/body/div');
if ($elements->length != 1) {
if ($viewid) {
log_warn("Leap2a export: invalid html found in view {$viewid}");
}
if ($elements->length < 1) {
return false;
}
}
$ourelement = $elements->item(0);
$content = $dom->importNode($ourelement, true);
$content->setAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$topel->appendChild($content);
} else {
return false;
// wtf is it then?
}
}
$dom->appendChild($topel->firstChild);
return $dom->saveXML($dom->documentElement);
}
示例14: getRowsToProcess
public function getRowsToProcess($filesToProcess)
{
# Get some more detailed error information from libxml
libxml_use_internal_errors(true);
$log = null;
try {
# Logger for this processor
$writer = new Zend_Log_Writer_Stream(Mage::getBaseDir('log') . DS . 'order_status_import_processor_xml.log');
$log = new Zend_Log($writer);
} catch (Exception $e) {
# Do nothing.. :|
}
# Updates to process, later the result
$updatesInFilesToProcess = array();
# Get mapping model
$this->mappingModel = Mage::getModel('orderstatusimport/processor_mapping_fields');
$this->mappingModel->setDataPath('orderstatusimport/processor_xml/import_mapping');
# Load mapping
$this->mapping = $this->mappingModel->getMappingConfig();
# Load configuration:
$config = array('IMPORT_DATA_XPATH' => Mage::getStoreConfig('orderstatusimport/processor_xml/xpath_data'), 'IMPORT_SHIPPED_ONE_FIELD' => Mage::getStoreConfigFlag('orderstatusimport/processor_xml/shipped_one_field'), 'IMPORT_NESTED_ITEMS' => Mage::getStoreConfigFlag('orderstatusimport/processor_xml/node_nested_items'), 'IMPORT_NESTED_ITEM_XPATH' => Mage::getStoreConfig('orderstatusimport/processor_xml/node_nested_path'), 'IMPORT_NESTED_TRACKINGS' => Mage::getStoreConfigFlag('orderstatusimport/processor_xml/node_nested_trackings'), 'IMPORT_NESTED_TRACKING_XPATH' => Mage::getStoreConfig('orderstatusimport/processor_xml/node_nested_tracking_path'));
if ($this->mapping->getOrderNumber() == null) {
Mage::throwException('Please configure the XML processor in the configuration section of the Tracking Number Import Module. The order number field may not be empty and must be mapped.');
}
if ($config['IMPORT_DATA_XPATH'] == '') {
Mage::throwException('Please configure the XML Processor in the configuration section of the Tracking Number Import Module. The Data XPath field may not be empty.');
}
foreach ($filesToProcess as $importFile) {
$data = $importFile['data'];
$filename = $importFile['filename'];
$type = $importFile['type'];
unset($importFile['data']);
// Remove UTF8 BOM
$bom = pack('H*', 'EFBBBF');
$data = preg_replace("/^{$bom}/", '', $data);
$updatesToProcess = array();
// Prepare data - replace namespace
$data = str_replace('xmlns=', 'ns=', $data);
// http://www.php.net/manual/en/simplexmlelement.xpath.php#96153
$data = str_replace('xmlns:', 'ns:', $data);
// http://www.php.net/manual/en/simplexmlelement.xpath.php#96153
try {
$xmlDOM = new DOMDocument();
$xmlDOM->loadXML($data);
} catch (Exception $e) {
$errors = "Could not load XML File '" . $filename . "' from '" . $type . "':\n" . $e->getMessage();
foreach (libxml_get_errors() as $error) {
$errors .= "\t" . $error->message;
}
if ($log instanceof Zend_Log) {
$log->info($errors);
}
continue;
# Process next file..
}
if (!$xmlDOM) {
$errors = "Could not load XML File '" . $filename . "' from '" . $type . "':\n" . $e->getMessage();
foreach (libxml_get_errors() as $error) {
$errors .= "\t" . $error->message;
}
if ($log instanceof Zend_Log) {
$log->info($errors);
}
continue;
# Process next file..
}
$domXPath = new DOMXPath($xmlDOM);
$updates = $domXPath->query($config['IMPORT_DATA_XPATH']);
foreach ($updates as $update) {
// Init "sub dom"
$updateDOM = new DomDocument();
$updateDOM->appendChild($updateDOM->importNode($update, true));
$updateXPath = new DOMXPath($updateDOM);
$this->update = $updateXPath;
$orderNumber = $this->getFieldData('order_number');
if (empty($orderNumber)) {
continue;
}
$carrierCode = $this->getFieldData('carrier_code');
$carrierName = $this->getFieldData('carrier_name');
$trackingNumber = $this->getFieldData('tracking_number');
$status = $this->getFieldData('order_status');
$orderComment = $this->getFieldData('order_status_history_comment');
$skuToShip = strtolower($this->getFieldData('sku'));
$qtyToShip = $this->getFieldData('qty');
$customData1 = $this->getFieldData('custom1');
$customData2 = $this->getFieldData('custom2');
if (!isset($updatesToProcess[$orderNumber])) {
$updatesToProcess[$orderNumber] = array("STATUS" => $status, "ORDER_COMMENT" => $orderComment, "CUSTOM_DATA1" => $customData1, "CUSTOM_DATA2" => $customData2);
$updatesToProcess[$orderNumber]['tracks'] = array();
if ($config['IMPORT_NESTED_TRACKINGS']) {
// Tracking data is nested..
$tracks = $updateXPath->query($config['IMPORT_NESTED_TRACKING_XPATH']);
foreach ($tracks as $track) {
$this->track = $track;
$carrierCode = $this->getFieldData('carrier_code', 'track');
$carrierName = $this->getFieldData('carrier_name', 'track');
$trackingNumber = $this->getFieldData('tracking_number', 'track');
if ($trackingNumber !== '') {
$trackingNumber = str_replace(array("/", ",", "|"), ";", $trackingNumber);
//.........这里部分代码省略.........
示例15: appendDomToElement
/**
*
* gets a dom to append and an element and another dom to append into them.
* @param DomDocument $domToAppend
* @param DOMElement $element
* @param DomDocument $elementsDom
* @throws Exception
*/
public static function appendDomToElement(DomDocument $domToAppend, DOMElement &$element, DomDocument $elementsDom)
{
if ($domToAppend->documentElement != NULL) {
$importedNode = $elementsDom->importNode($domToAppend->documentElement, true);
//Add him to the output reference elements
$element->appendChild($importedNode);
} else {
//DO nothing because the Dom is empty
// throw new Exception("The dom to append document element was null : " . $domToAppend);
}
}