本文整理汇总了PHP中SimpleXMLElement::getDocNamespaces方法的典型用法代码示例。如果您正苦于以下问题:PHP SimpleXMLElement::getDocNamespaces方法的具体用法?PHP SimpleXMLElement::getDocNamespaces怎么用?PHP SimpleXMLElement::getDocNamespaces使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SimpleXMLElement
的用法示例。
在下文中一共展示了SimpleXMLElement::getDocNamespaces方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fromAtom
private static function fromAtom(SimpleXMLElement $xml)
{
if (!in_array('http://www.w3.org/2005/Atom', $xml->getDocNamespaces(), TRUE) && !in_array('http://purl.org/atom/ns#', $xml->getDocNamespaces(), TRUE)) {
throw new FeedException('Invalid feed.');
}
// generate 'timestamp' tag
foreach ($xml->entry as $entry) {
$entry->timestamp = strtotime($entry->updated);
}
$feed = new self();
$feed->xml = $xml;
return $feed;
}
示例2: parseBody
/**
* Parses additional exception information from the response body
*
* @param \SimpleXMLElement $body The response body as XML
* @param array $data The current set of exception data
*/
protected function parseBody(\SimpleXMLElement $body, array &$data)
{
$data['parsed'] = $body;
$namespaces = $body->getDocNamespaces();
if (isset($namespaces[''])) {
// Account for the default namespace being defined and PHP not being able to handle it :(
$body->registerXPathNamespace('ns', $namespaces['']);
$prefix = 'ns:';
} else {
$prefix = '';
}
if ($tempXml = $body->xpath("//{$prefix}Code[1]")) {
$data['code'] = (string) $tempXml[0];
}
if ($tempXml = $body->xpath("//{$prefix}Message[1]")) {
$data['message'] = (string) $tempXml[0];
}
$tempXml = $body->xpath("//{$prefix}RequestId[1]");
if (empty($tempXml)) {
$tempXml = $body->xpath("//{$prefix}RequestID[1]");
}
if (isset($tempXml[0])) {
$data['request_id'] = (string) $tempXml[0];
}
}
示例3: __construct
/**
* @param string $wsdl
*/
public function __construct($wsdl)
{
$this->wsdl = $wsdl;
$xml = new \SimpleXMLElement(\file_get_contents($wsdl));
$this->namespaces = $xml->getDocNamespaces();
$this->tns = isset($xml['targetNamespace']) ? (string) $xml['targetNamespace'] : null;
}
示例4: parse
/**
* Converts XML into an array, respecting namespaces, attributes, and text values.
*
* @return array
*/
public function parse()
{
$namespaces = $this->xml->getDocNamespaces();
$namespaces[''] = null;
//add base (empty) namespace
$attributes = $this->getAttributes($namespaces);
//get child nodes from all namespaces
$tags = array();
foreach ($namespaces as $prefix => $namespace) {
foreach ($this->xml->children($namespace) as $childXml) {
$new_parser = new XmlParser($childXml, $this->options);
$child = $new_parser->parse();
list($childTag, $childProperties) = each($child);
//add namespace prefix, if any
if ($prefix) {
$childTag = $prefix . $this->namespaceSeparator . $childTag;
}
if (!isset($tags[$childTag])) {
$alwaysArray = $this->options['alwaysArray'];
$autoArray = $this->options['autoArray'];
$tags[$childTag] = $childProperties;
if (in_array($childTag, $alwaysArray) || !$autoArray) {
$tags[$childTag] = [$childProperties];
}
} elseif ($this->isIntegerIndexedArray($tags[$childTag])) {
$tags[$childTag][] = $childProperties;
} else {
//key exists so convert to integer indexed array with previous value in position 0
$tags[$childTag] = array($tags[$childTag], $childProperties);
}
}
}
//get text content of node
$textContent = array();
$plainText = trim((string) $this->xml);
if ($plainText !== '') {
$textContent[$this->options['textContent']] = $plainText;
}
//stick it all together
$properties = $plainText;
if (!$this->options['autoText'] || $attributes || $tags || $plainText === '') {
$properties = array_merge($attributes, $tags, $textContent);
}
//return node as array
return array($this->xml->getName() => $properties);
}
示例5: parseType
/**
* @param \SimpleXMLElement $node
*
* @return \Bundle\WebServiceBundle\ServiceDefinition\Type
*/
protected function parseType(\SimpleXMLElement $node)
{
$namespaces = $node->getDocNamespaces(true);
$qname = explode(':', $node['xml-type'], 2);
$xmlType = sprintf('{%s}%s', $namespaces[$qname[0]], $qname[1]);
$type = new Type((string) $node['php-type'], $xmlType, (string) $node['converter']);
return $type;
}
示例6: xmlToArray
/**
* Create plain PHP associative array from XML.
*
* Example usage:
* $xmlNode = simplexml_load_file('example.xml');
* $arrayData = xmlToArray($xmlNode);
* echo json_encode($arrayData);
*
* @param SimpleXMLElement $xml The root node
* @param array $options Associative array of options
* @return array
* @link http://outlandishideas.co.uk/blog/2012/08/xml-to-json/ More info
* @author Tamlyn Rhodes <http://tamlyn.org>
* @license http://creativecommons.org/publicdomain/mark/1.0/ Public Domain
*/
function xmlToArray($xml, $options = array())
{
$defaults = array('namespaceSeparator' => ':', 'attributePrefix' => '', 'alwaysArray' => array(), 'autoArray' => true, 'textContent' => '$', 'autoText' => true, 'keySearch' => '@', 'keyReplace' => '');
$options = array_merge($defaults, $options);
$namespaces = $xml->getDocNamespaces();
$namespaces[''] = null;
//add base (empty) namespace
//get attributes from all namespaces
$attributesArray = array();
foreach ($namespaces as $prefix => $namespace) {
foreach ($xml->attributes($namespace) as $attributeName => $attribute) {
//replace characters in attribute name
if ($options['keySearch']) {
$attributeName = str_replace($options['keySearch'], $options['keyReplace'], $attributeName);
}
$attributeKey = $options['attributePrefix'] . ($prefix ? $prefix . $options['namespaceSeparator'] : '') . $attributeName;
$attributesArray[$attributeKey] = (string) $attribute;
}
}
//get child nodes from all namespaces
$tagsArray = array();
foreach ($namespaces as $prefix => $namespace) {
foreach ($xml->children($namespace) as $childXml) {
//recurse into child nodes
$childArray = xmlToArray($childXml, $options);
list($childTagName, $childProperties) = each($childArray);
//replace characters in tag name
if ($options['keySearch']) {
$childTagName = str_replace($options['keySearch'], $options['keyReplace'], $childTagName);
}
//add namespace prefix, if any
if ($prefix) {
$childTagName = $prefix . $options['namespaceSeparator'] . $childTagName;
}
if (!isset($tagsArray[$childTagName])) {
//only entry with this key
//test if tags of this type should always be arrays, no matter the element count
$tagsArray[$childTagName] = in_array($childTagName, $options['alwaysArray']) || !$options['autoArray'] ? array($childProperties) : $childProperties;
} elseif (is_array($tagsArray[$childTagName]) && array_keys($tagsArray[$childTagName]) === range(0, count($tagsArray[$childTagName]) - 1)) {
//key already exists and is integer indexed array
$tagsArray[$childTagName][] = $childProperties;
} else {
//key exists so convert to integer indexed array with previous value in position 0
$tagsArray[$childTagName] = array($tagsArray[$childTagName], $childProperties);
}
}
}
//get text content of node
$textContentArray = array();
$plainText = trim((string) $xml);
if ($plainText !== '') {
$textContentArray[$options['textContent']] = $plainText;
}
//stick it all together
$propertiesArray = !$options['autoText'] || $attributesArray || $tagsArray || $plainText === '' ? array_merge($attributesArray, $tagsArray, $textContentArray) : $plainText;
//return node as array
return array($xml->getName() => $propertiesArray);
}
示例7: registerXPathNamespaces
/**
* Registers found doc namespaces to xpath for being able to access
* elements via xpath
*
* @param \SimpleXMLElement $doc
*/
protected function registerXPathNamespaces(\SimpleXMLElement $doc)
{
foreach ($doc->getDocNamespaces() as $strPrefix => $strNamespace) {
if (strlen($strPrefix) == 0) {
$strPrefix = $strNamespace;
}
$doc->registerXPathNamespace($strPrefix, $strNamespace);
}
}
示例8: __doRequest
/**
* Ацкий костыль. В ответе частенько пеперпутаны неймспейсы, которые пытаемся тут поправить
*
* @param <type> $request
* @param <type> $location
* @param <type> $action
* @param <type> $version
* @param <type> $one_way
* @return <type>
*/
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
$req = parent::__doRequest($request, $location, $action, $version, $one_way);
$xml = new \SimpleXMLElement($req);
if (isset($nss[self::NS_SOAPLITE]) && isset($nss[self::NS_API])) {
$nss = \array_flip($xml->getDocNamespaces(true));
$req = \str_replace($nss[self::NS_SOAPLITE] . ':', $nss[self::NS_API] . ':', $req);
}
return $req;
}
示例9: action_block_content_rssblock
public function action_block_content_rssblock($block)
{
$items = array();
$cachename = array('rssblock', md5($block->feed_url));
if (Cache::expired($cachename)) {
// CronTab::add_single_cron('single_update_rrs_blocks', 'rrsblocks_update', HabariDateTime::date_create());
}
$feed = Cache::get($cachename);
try {
$xml = new SimpleXMLElement($feed);
$dns = $xml->getDocNamespaces();
$itemcount = 0;
foreach ($xml->channel->item as $xitem) {
$item = new StdClass();
foreach ($xitem->children() as $child) {
$item->{$child->getName()} = (string) $child;
}
foreach ($dns as $ns => $nsurl) {
foreach ($xitem->children($nsurl) as $child) {
$item->{$ns . '__' . $child->getName()} = (string) $child;
foreach ($child->attributes() as $name => $value) {
$item->{$ns . '__' . $child->getName() . '__' . $name} = (string) $value;
}
}
}
$items[] = $item;
$itemcount++;
if ($block->item_limit > 0 && $itemcount >= $block->item_limit) {
break;
}
}
foreach ($xml->item as $xitem) {
$item = new StdClass();
foreach ($xitem->children() as $child) {
$item->{$child->getName()} = (string) $child;
}
foreach ($dns as $ns => $nsurl) {
foreach ($xitem->children($nsurl) as $child) {
$item->{$ns . '__' . $child->getName()} = (string) $child;
foreach ($child->attributes() as $name => $value) {
$item->{$ns . '__' . $child->getName() . '__' . $name} = (string) $value;
}
}
}
$items[] = $item;
$itemcount++;
if ($block->item_limit > 0 && $itemcount >= $block->item_limit) {
break;
}
}
} catch (Exception $e) {
}
$block->items = $items;
$block->markup_id = Utils::slugify($block->title);
}
示例10: loadAtom
/**
* Loads Atom channel.
* @param string Atom feed URL
* @param string optional user name
* @param string optional password
* @return Feed
* @throws FeedException
*/
public static function loadAtom($url, $user = NULL, $pass = NULL)
{
$xml = new SimpleXMLElement(self::httpRequest($url, $user, $pass), LIBXML_NOWARNING | LIBXML_NOERROR);
if (!in_array('http://www.w3.org/2005/Atom', $xml->getDocNamespaces(), TRUE)) {
throw new FeedException('Invalid channel.');
}
// generate 'timestamp' tag
foreach ($xml->entry as $entry) {
$entry->timestamp = strtotime($entry->updated);
}
$feed = new self();
$feed->xml = $xml;
return $feed;
}
示例11: __doRequest
/**
* {@inheritdoc}
*
* @internal
*/
public function __doRequest($request, $location, $action, $version, $oneWay = null)
{
$this->requestId = substr(str_replace(' ', '', microtime()), 2);
$response = parent::__doRequest($request, $location, $action, $version, $oneWay);
if (!empty($response)) {
$xml = new \SimpleXMLElement($response);
$nss = array_flip($xml->getDocNamespaces(true));
$invalidNs = 'http://namespaces.soaplite.com/perl';
if (isset($nss[$invalidNs]) && isset($nss['API'])) {
$response = str_replace($nss[$invalidNs] . ':', $nss['API'] . ':', $response);
}
}
return $response;
}
示例12: test_root
/**
* @test
* @group sitemap
*/
public function test_root()
{
// Base Sitemap
$sitemap = new Sitemap();
// Create basic Mobile Sitemap
$instance = new Sitemap_URL(new Sitemap_Geo());
$instance->set_loc('http://google.com');
$sitemap->add($instance);
// Load the end XML
$xml = new SimpleXMLElement($sitemap->render());
// Namespaces.
$namespaces = $xml->getDocNamespaces();
$this->assertSame(TRUE, isset($namespaces['geo']));
$this->assertSame('http://www.google.com/geo/schemas/sitemap/1.0', $namespaces['geo']);
}
示例13: parser
public function parser()
{
$error = 'El archivo no tiene un formato xml correcto';
try {
$sxe = new \SimpleXMLElement($this->text);
} catch (\ErrorException $e) {
throw new \ErrorException($error);
}
$namespaces = $sxe->getDocNamespaces(true);
$error = 'El archivo no esta en formato CFDI';
if (!array_key_exists('cfdi', $namespaces)) {
throw new \ErrorException($error);
}
$this->finished = true;
$this->parsed = $this->xmlAttr($sxe, $sxe->getName(), $namespaces, 'cfdi');
}
示例14: array
/**
* Transforms XML data to array.
*
* @param string $xml XML data.
* @param string[] $namespaces List of namespaces to include in parsing or empty to include all namespaces.
* @return array
*/
function ky_xml_to_array($xml, $namespaces = null)
{
$iter = 0;
$arr = array();
if (is_string($xml)) {
$xml = new SimpleXMLElement($xml);
}
if (!$xml instanceof SimpleXMLElement) {
return $arr;
}
if ($namespaces === null) {
$namespaces = $xml->getDocNamespaces(true);
}
foreach ($xml->attributes() as $attributeName => $attributeValue) {
$arr["_attributes"][$attributeName] = trim($attributeValue);
}
foreach ($namespaces as $namespace_prefix => $namespace_name) {
foreach ($xml->attributes($namespace_prefix, true) as $attributeName => $attributeValue) {
$arr["_attributes"][$namespace_prefix . ':' . $attributeName] = trim($attributeValue);
}
}
$has_children = false;
foreach ($xml->children() as $element) {
/** @var $element SimpleXMLElement */
$has_children = true;
$elementName = $element->getName();
if ($element->children()) {
$arr[$elementName][] = ky_xml_to_array($element, $namespaces);
} else {
$shouldCreateArray = array_key_exists($elementName, $arr) && !is_array($arr[$elementName]);
if ($shouldCreateArray) {
$arr[$elementName] = array($arr[$elementName]);
}
$shouldAddValueToArray = array_key_exists($elementName, $arr) && is_array($arr[$elementName]);
if ($shouldAddValueToArray) {
$arr[$elementName][] = trim($element[0]);
} else {
$arr[$elementName] = trim($element[0]);
}
}
$iter++;
}
if (!$has_children) {
$arr['_contents'] = trim($xml[0]);
}
return $arr;
}
示例15: __construct
public function __construct($xml = null)
{
if ($xml) {
// first, lets find the cmis namespace to use
$sx = new SimpleXMLElement($xml);
$namespaces = $sx->getDocNamespaces();
$ns = isset($namespaces['cmis']) ? $namespaces['cmis'] : 'http://docs.oasis-open.org/ns/cmis/core/200901';
// go through the XML and pull out the things we're interested in
Zend_Feed::registerNamespace('cmis', $ns);
// 'http://www.cmis.org/2008/05');
$this->rawXml = $xml;
$feed = Zend_Feed::importString($this->rawXml);
foreach ($feed as $item) {
$this->loadFromFeed($item);
}
}
}