本文整理汇总了PHP中DOMDocument::hasChildNodes方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::hasChildNodes方法的具体用法?PHP DOMDocument::hasChildNodes怎么用?PHP DOMDocument::hasChildNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMDocument
的用法示例。
在下文中一共展示了DOMDocument::hasChildNodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getAny
/**
* Get any value
* @uses \DOMDocument::loadXML()
* @uses \DOMDocument::hasChildNodes()
* @uses \DOMDocument::saveXML()
* @uses \DOMNode::item()
* @uses \Ews\StructType\EwsConfigurationRequestDetailsType::setAny()
* @param bool $asString true: returns XML string, false: returns \DOMDocument
* @return \DOMDocument|null
*/
public function getAny($asString = true)
{
if (!empty($this->any) && !$this->any instanceof \DOMDocument) {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
if ($dom->loadXML($this->any)) {
$this->setAny($dom);
}
unset($dom);
}
return $asString && $this->any instanceof \DOMDocument && $this->any->hasChildNodes() ? $this->any->saveXML($this->any->childNodes->item(0)) : $this->any;
}
示例2: initRootElement
/**
* Find valid root node (not a comment, at least a DOMElement node)
* @throws \InvalidArgumentException
*/
protected function initRootElement()
{
if ($this->domDocument->hasChildNodes()) {
foreach ($this->domDocument->childNodes as $node) {
if ($node instanceof \DOMElement) {
$this->rootElement = $this->getElementHandler($node, $this);
break;
}
}
} else {
throw new \InvalidArgumentException('Document seems to be invalid', __LINE__);
}
}
示例3: convertXmlToArray
/**
* @param \DOMDocument|\DOMElement $dom
* @return array
*/
private function convertXmlToArray($dom)
{
$result = array();
if ($dom->hasChildNodes()) {
$children = $dom->childNodes;
if (1 === $children->length) {
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE) {
$result['_value'] = $child->nodeValue;
return urldecode(count($result) == 1 ? $result['_value'] : $result);
}
}
$groups = array();
foreach ($children as $child) {
if (!isset($result[$child->nodeName])) {
$result[$child->nodeName] = $this->convertXmlToArray($child);
} else {
if (!isset($groups[$child->nodeName])) {
$result[$child->nodeName] = array($result[$child->nodeName]);
$groups[$child->nodeName] = 1;
}
$result[$child->nodeName][] = $this->convertXmlToArray($child);
}
}
}
return $result;
}
示例4: convert
/**
* Convert xml node to array or string
*
* @param \DOMDocument $node
* @return array|string
*/
public function convert($node)
{
$result = [];
if ($node->nodeType == XML_TEXT_NODE) {
$result = $node->nodeValue;
} else {
if ($node->hasChildNodes()) {
$children = $node->childNodes;
for ($i = 0; $i < $children->length; $i++) {
$child = $children->item($i);
if ($child->nodeName != '#text') {
$result[$child->nodeName][] = $this->convert($child);
} else {
if ($child->nodeName == '#text') {
$text = $this->convert($child);
if (trim($text) != '') {
$result[$child->nodeName] = $this->convert($child);
}
}
}
}
}
if ($node->hasAttributes()) {
$attributes = $node->attributes;
if (!is_null($attributes)) {
foreach ($attributes as $attribute) {
$result[$attribute->name] = $attribute->value;
}
}
}
}
return $result;
}
示例5: parse
/**
* Parse dom document
*
* @param \DOMDocument $dom
* @return array
*/
public function parse(\DOMDocument $dom)
{
$result = [];
if ($dom->hasChildNodes()) {
foreach ($dom->childNodes as $child) {
if (XML_COMMENT_NODE == $child->nodeType) {
$result['comment'] = $child->nodeValue;
} elseif (XML_ELEMENT_NODE == $child->nodeType && 'config' == $child->nodeName) {
$result = array_merge($result, $this->_parseNode($child));
}
}
}
return $result;
}
示例6: getHTMLNode
/**
* Get the html node for this element
* @param AppBuilderAPIDOMDocument $xml
* @param array $data
* @return DOMElement
*/
public function getHTMLNode($xml, &$data)
{
$node = parent::getHTMLNode($xml, $data);
$node->appendChild($htmlNode = $xml->createElement('div', 'html'));
$html = $this->html;
if ($html != '') {
$fragDoc = new DOMDocument();
@$fragDoc->loadHTML('<?xml version="1.0" encoding="utf-8" standalone="yes"?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/ loose.dtd"><html><head><title></title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>' . $html . '</body></html>');
if ($fragDoc->hasChildNodes()) {
$body = $fragDoc->getElementsByTagName('body')->item(0);
if ($body->hasChildNodes()) {
for ($i = 0; $i < $body->childNodes->length; $i++) {
$iNode = $body->childNodes->item($i);
$importedNode = $xml->importNode($iNode, true);
$htmlNode->appendChild($importedNode);
}
$this->checkNode($xml, $htmlNode);
}
}
}
return $node;
}
示例7: getHTMLNodeInner
/**
* Get the html node for this element
*
* @param \DOMElement $node
* @param \AppShed\Remote\XML\DOMDocument $xml
* @param \AppShed\Remote\HTML\Settings $settings
*/
protected function getHTMLNodeInner($node, $xml, $settings)
{
$node->appendChild($htmlNode = $xml->createElement('div', 'html'));
$html = $this->html;
if ($html != '') {
$fragDoc = new \DOMDocument();
$libxml_previous_state = libxml_use_internal_errors(true);
$fragDoc->loadHTML('<?xml version="1.0" encoding="utf-8" standalone="yes"?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/ loose.dtd"><html><head><title></title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>' . $html . '</body></html>');
libxml_clear_errors();
libxml_use_internal_errors($libxml_previous_state);
if ($fragDoc->hasChildNodes()) {
$body = $fragDoc->getElementsByTagName('body')->item(0);
if ($body->hasChildNodes()) {
for ($i = 0; $i < $body->childNodes->length; $i++) {
$iNode = $body->childNodes->item($i);
$importedNode = $xml->importNode($iNode, true);
$htmlNode->appendChild($importedNode);
}
$this->checkNode($xml, $htmlNode);
}
}
}
}
示例8: getArrayFromXml
/**
* Convert opml xml node into array for import
* http://www.php.net/manual/en/class.domdocument.php#101014
*
* @param DOMDocument $node Node to convert into array
*
* @return array Array corresponding to the given node
*/
public static function getArrayFromXml($node)
{
$array = false;
if ($node->hasAttributes()) {
foreach ($node->attributes as $attr) {
$array[$attr->nodeName] = $attr->nodeValue;
}
}
if ($node->hasChildNodes()) {
if ($node->childNodes->length == 1) {
$array[$node->firstChild->nodeName] = $node->firstChild->nodeValue;
} else {
foreach ($node->childNodes as $childNode) {
if ($childNode->nodeType != XML_TEXT_NODE) {
$array[$childNode->nodeName][] = Opml::getArrayFromXml($childNode);
}
}
}
}
return $array;
}
示例9: testFunctionnal
/**
* Test if the breadcrumb is functionnal.
*
* @cover BreadcrumbExtension::getTemplateName()
* @cover BreadcrumbExtension::breadcrumbRender()
* @cover BreadcrumbExtension::breadcrumbDisplay()
*/
public function testFunctionnal()
{
$service = new BreadcrumbService();
$extension = new BreadcrumbExtension($this->environment, $service);
// Test default
$template = $extension->getTemplateName();
$this->assertEquals('bs_breadcrumb/breadcrumb.html.twig', $template);
$this->assertTrue($this->filesystemLoader->exists($template));
ob_start();
$extension->breadcrumbDisplay();
$display = ob_get_contents();
ob_end_clean();
$html = $extension->breadcrumbRender();
$this->assertTrue(is_string($html));
$this->assertGreaterThan(20, strlen($html));
$this->assertEquals($display, $html);
$dom = new \DOMDocument();
$dom->loadXML($html);
$this->assertTrue($dom->hasChildNodes());
$container = $dom->firstChild;
$this->assertEquals('div', $container->nodeName);
$this->assertEquals('container-fluid', $container->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals(1, $container->childNodes->length);
$row = $container->firstChild;
$this->assertEquals('div', $row->nodeName);
$this->assertEquals('row', $row->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals(1, $row->childNodes->length);
$breadcrumb = $row->firstChild;
$this->assertEquals('ol', $breadcrumb->nodeName);
$this->assertEquals('breadcrumb', $breadcrumb->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals(0, $breadcrumb->childNodes->length);
// Test content
$data_test = array(array('Page 1 ' . uniqid(), '/', 'glyphicon-home'), array('Page 2 ' . uniqid(), 'bootstrap-bundle-test-route', null), array('Page 3 ' . uniqid(), null, null));
foreach ($data_test as $attrs) {
$link = new BreadcrumbLink($attrs[0], $attrs[1], $attrs[2]);
$service->pushLink($link);
}
$html = $extension->breadcrumbRender();
$dom->loadXML($html);
$breadcrumb = $dom->firstChild->firstChild->firstChild;
$this->assertEquals('ol', $breadcrumb->nodeName);
$this->assertEquals('breadcrumb', $breadcrumb->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals(3, $breadcrumb->childNodes->length);
foreach ($breadcrumb->childNodes as $k => $node) {
$this->assertEquals($data_test[$k][0], $node->nodeValue);
}
$this->assertEquals('a', $breadcrumb->childNodes->item(0)->firstChild->nodeName);
$this->assertEquals('a', $breadcrumb->childNodes->item(1)->firstChild->nodeName);
$this->assertEquals('#text', $breadcrumb->childNodes->item(2)->firstChild->nodeName);
$this->assertEquals($data_test[0][1], $breadcrumb->childNodes->item(0)->firstChild->attributes->getNamedItem('href')->nodeValue);
$this->assertEquals($data_test[1][1], $breadcrumb->childNodes->item(1)->firstChild->attributes->getNamedItem('href')->nodeValue);
$this->assertEquals('i', $breadcrumb->childNodes->item(0)->firstChild->firstChild->nodeName);
$this->assertEquals('#text', $breadcrumb->childNodes->item(1)->firstChild->firstChild->nodeName);
$this->assertEquals('glyphicon ' . $data_test[0][2], $breadcrumb->childNodes->item(0)->firstChild->firstChild->attributes->getNamedItem('class')->nodeValue);
// Test with the url generator
$service = new BreadcrumbService($this->urlGenerator);
$extension = new BreadcrumbExtension($this->environment, $service);
foreach ($data_test as $attrs) {
$link = new BreadcrumbLink($attrs[0], $attrs[1], $attrs[2]);
$service->pushLink($link);
}
$html = $extension->breadcrumbRender();
$dom->loadXML($html);
$breadcrumb = $dom->firstChild->firstChild->firstChild;
$this->assertEquals($data_test[0][1], $breadcrumb->childNodes->item(0)->firstChild->attributes->getNamedItem('href')->nodeValue);
$this->assertEquals($this->urlGenerator->generate($data_test[1][1]), $breadcrumb->childNodes->item(1)->firstChild->attributes->getNamedItem('href')->nodeValue);
}
示例10: _rollback_web_config
/**
* Rollback *all* web.config settings
* This removes the specific xpaths we added.
* Unmerging XML docs is ugly and complex. We don't have the ability to ermove "chunks"
* like in the .htaccess files. It's all or nothing.
* @return void
*/
protected function _rollback_web_config()
{
if (!file_exists($this->_web_config_path)) {
return true;
}
if (file_exists($this->_web_config_path) && !win_is_writable($this->_web_config_path)) {
return false;
}
// Get the parent doc
libxml_use_internal_errors(true);
$DOMParent = new DOMDocument();
$DOMParent->formatOutput = true;
try {
$DOMParent->load($this->_web_config_path);
} catch (Exception $e) {
return false;
}
// Remove any matching nodes in the parent
$xpath = new DOMXPath($DOMParent);
$paths = array("//*/caching/profiles/add[@duration='00:01:00']", "//*/urlCompression[@doDynamicCompression='true']", "//*/clientCache[@cacheControlMaxAge='14.00:00:00']");
foreach ($paths as $path) {
$query = $xpath->query($path);
if ($query->length > 0) {
for ($i = 0; $i < $query->length; $i++) {
$query->item($i)->parentNode->removeChild($query->item($i));
}
}
}
// Remove empty nodes
$paths = array("//configuration/system.webServer/caching/profiles", "//configuration/system.webServer/caching", "//configuration/system.webServer/staticContent", "//configuration/system.webServer", "//configuration");
foreach ($paths as $path) {
$query = $xpath->query($path);
if ($query->length > 0) {
for ($i = 0; $i < $query->length; $i++) {
$node = $query->item($i);
$nodeHasEmptyText = true;
for ($i = 0; $i < $node->childNodes->length; $i++) {
if (!$node->childNodes->item($i) instanceof DOMText || '' !== trim($node->childNodes->item($i)->nodeValue)) {
$nodeHasEmptyText = false;
break;
}
}
if (!$node->hasAttributes() && (!$node->hasChildNodes() || $nodeHasEmptyText)) {
$node->parentNode->removeChild($node);
}
}
}
}
// If there is no root element, then save a dummy web.config
if (!$DOMParent->hasChildNodes()) {
if (0 === @file_put_contents($this->_web_config_path, '<' . '?xml version="1.0"?' . ">\n<configuration><system.webServer/></configuration>")) {
return false;
}
} else {
// Save the XML to the parent doc's file
try {
$DOMParent->save($this->_web_config_path);
} catch (Exception $e) {
return false;
}
}
return true;
}
示例11: formatXmlString
/**
* Function will reformat the xml document to have correct indention.
*
* @param $strXml
* @return DOMDocument
*/
private function formatXmlString($strXml)
{
try {
$doc = new DOMDocument();
$doc->loadXML($strXml, LIBXML_NOBLANKS);
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
} catch (Exception $e) {
LogHelper::log_error('Error formatting xml: ' . $e);
}
return $doc != NULL && $doc->hasChildNodes() ? $doc->saveXML() : NULL;
}
示例12: parse
/**
* @param WebDavClient $client
* @param string $xml The multi-status response as an XML string
*
* @throws \RuntimeException
* @return self Returns the parsed multi-status response as an object
*
* @todo
* - validate the XML document using a WebDAV DTD
* - register namespaces automatically with the Xpath object
* - testing DOMDocument::loadXML throwing DOMException with the libxml settings and an erroneous XML document
*/
public static function parse(WebDavClient $client, $xml)
{
$xml = preg_replace('/\\s*[\\r\\n]\\s*/', null, $xml);
$dom = new \DOMDocument();
$dom->formatOutput = false;
$dom->preserveWhiteSpace = false;
$dom->loadXML($xml, LIBXML_NOWARNING | LIBXML_NOERROR);
// XSD validation ? Namespaces ?
$xpath = new \DOMXPath($dom);
$xpath->registerNamespace('D', 'DAV:');
if (!$dom->hasChildNodes()) {
throw new \RuntimeException();
}
if ($dom->documentElement->localName != self::TAGNAME) {
throw new \RuntimeException();
}
$result = array();
$description = $xpath->evaluate('string(D:responsedescription)', $dom->documentElement);
foreach ($xpath->query('./D:response', $dom->documentElement) as $xResponse) {
$href = $xpath->evaluate('string(D:href)', $xResponse);
$statusLine = $xpath->evaluate('string(D:status)', $xResponse);
$responseDescription = $xpath->evaluate('string(D:responsedescription)', $xResponse);
// Determines whether this response uses a combination of href+status elements
// or if it uses a list of propstat elements
$responseStatus = $statusLine ? self::parseHttpStatus($statusLine) : array();
foreach ($xpath->query('./D:propstat', $xResponse) as $xPropstat) {
$statusCode = self::parseHttpStatus($xpath->evaluate('string(D:status)', $xPropstat));
$responseStatus[$statusCode] = array();
foreach ($xpath->query('./D:prop/*', $xPropstat) as $xProperty) {
if (!isset($client->xmlNamespaces[$xProperty->namespaceURI])) {
continue;
}
if (isset($client->propertyMap[$xProperty->localName])) {
$propertyClass = $client->propertyMap[$xProperty->localName];
} else {
$propertyClass = __NAMESPACE__ . '\\Property';
}
$responseStatus[$statusCode][] = $propertyClass::fromXml($xProperty, $client->xmlNamespaces);
}
}
$result[] = new Response($href, $responseStatus, $responseDescription ? $responseDescription : null);
}
return new self($result, $description);
}
示例13: convertXMLToArray
/**
* @param \DOMDocument|string $xml $xml
* @return array
*/
public function convertXMLToArray($xml)
{
$result = array();
if ($xml->hasAttributes()) {
$attrs = $xml->attributes;
foreach ($attrs as $attr) {
$result['@attributes'][$attr->name] = $attr->value;
}
}
if ($xml->hasChildNodes()) {
$children = $xml->childNodes;
if ($children->length == 1) {
$child = $children->item(0);
if ($child->nodeType == XML_TEXT_NODE || $child->nodeType == XML_CDATA_SECTION_NODE) {
$result['_value'] = $child->nodeValue;
return count($result) == 1 ? $result['_value'] : $result;
}
}
$groups = array();
foreach ($children as $child) {
if (!isset($result[$child->nodeName])) {
$result[$child->nodeName] = $this->convertXMLToArray($child);
} else {
if (!isset($groups[$child->nodeName])) {
$result[$child->nodeName] = array($result[$child->nodeName]);
$groups[$child->nodeName] = 1;
}
$result[$child->nodeName][] = $this->convertXMLToArray($child);
}
if (empty($result[$child->nodeName])) {
unset($result[$child->nodeName]);
} elseif (is_array($result[$child->nodeName])) {
$values = array();
foreach ($result[$child->nodeName] as $key => $val) {
if (!empty($result[$child->nodeName][$key])) {
$values[] = $result[$child->nodeName][$key];
if (count($values) > 1) {
break;
}
}
}
if (empty($values)) {
unset($result[$child->nodeName]);
}
}
}
}
return $result;
}
示例14: testOutput
/**
* Test is the navbar is functionnal.
*
* @cover NavbarExtension::navbarRender()
* @cover NavbarExtension::navbarDisplay()
*/
public function testOutput()
{
$container = new NavbarService();
$extension = new NavbarExtension($this->environment, $container, $this->configuration);
// Test default
$template = $extension->getTemplateName();
$this->assertEquals('bs_navbar/navbar.html.twig', $template);
$this->assertTrue($this->filesystemLoader->exists($template));
ob_start();
$extension->navbarDisplay();
$display = ob_get_contents();
ob_end_clean();
$html = $extension->navbarRender();
$this->assertTrue(is_string($html));
$this->assertGreaterThan(20, strlen($html));
$this->assertEquals($display, $html);
$dom = new \DOMDocument();
$dom->loadXML($html);
$this->assertTrue($dom->hasChildNodes());
$navbar = $dom->firstChild;
$this->assertEquals('nav', $navbar->nodeName);
$this->assertEquals('navbar navbar-fixed-top', $navbar->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals(1, $navbar->childNodes->length);
$container = $navbar->firstChild;
$this->assertEquals('div', $container->nodeName);
$this->assertEquals('container-fluid', $container->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals(2, $container->childNodes->length);
$header = $container->firstChild;
$this->assertEquals('div', $header->nodeName);
$this->assertEquals('navbar-header', $header->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals(2, $header->childNodes->length);
$collapse = $container->childNodes->item(1);
$this->assertEquals('div', $collapse->nodeName);
$this->assertEquals('collapse navbar-collapse', $collapse->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals('neimheadh-bootstrap-navbar-collapse', $collapse->attributes->getNamedItem('id')->nodeValue);
$this->assertEquals(1, $collapse->childNodes->length);
$menus = $collapse->firstChild;
$this->assertEquals('ul', $menus->nodeName);
$this->assertEquals('nav navbar-nav navbar-right', $menus->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals('neimheadh-bootstrap-nav', $menus->attributes->getNamedItem('id')->nodeValue);
$this->assertEquals(0, $menus->childNodes->length);
$button = $header->firstChild;
$this->assertEquals('button', $button->nodeName);
$this->assertEquals('button', $button->attributes->getNamedItem('type')->nodeValue);
$this->assertEquals('navbar-toggle collapsed', $button->attributes->getNamedItem('class')->nodeValue);
$this->assertEquals('collapse', $button->attributes->getNamedItem('data-toggle')->nodeValue);
$this->assertEquals('#' . $collapse->attributes->getNamedItem('id')->nodeValue, $button->attributes->getNamedItem('data-target')->nodeValue);
$this->assertEquals('false', $button->attributes->getNamedItem('aria-expanded')->nodeValue);
$this->assertEquals(4, $button->childNodes->length);
foreach ($button->childNodes as $i => $node) {
$this->assertEquals('span', $node->nodeName);
$this->assertEquals($i == 0 ? 'sr-only' : 'icon-bar', $node->attributes->getNamedItem('class')->nodeValue);
if ($i == 0) {
$this->assertEquals('Navigation', $node->nodeValue);
}
}
// Test config
$configuration = $this->getConfigurationExample('navbar.config1.yml');
$configuration = $configuration['neimheadh_bootstrap']['navbar'];
$container = new NavbarService();
$extension = new NavbarExtension($this->environment, $container, $configuration);
// The templates will not be good
$error = false;
try {
$extension->navbarRender();
} catch (\Twig_Error_Loader $e) {
$error = true;
}
$this->assertTrue($error);
$configuration['template'] = 'bs_navbar/navbar.html.twig';
$extension->setConfiguration($configuration);
$error = false;
try {
$extension->navbarRender();
} catch (\Twig_Error_Loader $e) {
$error = true;
}
$this->assertTrue($error);
$configuration['header']['template'] = 'bs_navbar/navbar-header.html.twig';
$extension->setConfiguration($configuration);
$error = false;
try {
$extension->navbarRender();
} catch (\Twig_Error_Loader $e) {
$error = true;
}
$this->assertTrue($error);
$configuration['menu']['_list']['template'] = 'bs_navbar/navbar-menu.html.twig';
$extension->setConfiguration($configuration);
$html = $extension->navbarRender();
$dom = new \DOMDocument();
$dom->loadXML($html);
$this->assertTrue($dom->hasChildNodes());
$navbar = $dom->firstChild;
//.........这里部分代码省略.........
示例15: getDocumentString
/**
* @param $document
* @return mixed|null
*/
private function getDocumentString(DOMDocument $document)
{
$document->preserveWhiteSpace = false;
$document->formatOutput = true;
$document_str = $document != NULL && $document->hasChildNodes() ? $document->saveXML(NULL, LIBXML_NOBLANKS) : NULL;
// TODO - current 'LIBXML_NOXMLDECL' is not working.
if (isset($document_str)) {
$document_str = str_replace('<?xml version="1.0"?>', '', $document_str);
}
return $document_str;
}