本文整理汇总了PHP中SimpleXmlElement类的典型用法代码示例。如果您正苦于以下问题:PHP SimpleXmlElement类的具体用法?PHP SimpleXmlElement怎么用?PHP SimpleXmlElement使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SimpleXmlElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addChild
/**
* Add the node to a position under the tree
*
* @param \SimpleXmlElement|Element $node
* @param Element $parent
*/
public function addChild($node, $parent = null)
{
if ($node instanceof \SimpleXmlElement) {
$name = $node->getName();
$attributes = (array) $node->attributes();
$content = trim((string) $node);
$element = new Element($name, $attributes, $content);
if (!$this->tree) {
$this->tree = $element;
} else {
if (!$parent) {
$parent = $this->tree;
}
$parent->addChild($element);
}
// Add child elements recursive
if ($node->count() > 0) {
foreach ($node as $childNode) {
$this->addChild($childNode, $element);
}
}
} else {
if ($node instanceof Element) {
if (!$this->tree) {
$this->tree = $node;
} else {
if (!$parent) {
$parent = $this->tree;
}
$parent->addChild($node);
}
}
}
}
示例2: arrayToXml
/**
* arrayToXml
*
* @param string $array Array to convert to xml
* @param string $rootNodeName Name of the root node
* @param string $xml SimpleXmlElement
* @return string $asXml String of xml built from array
*/
public static function arrayToXml($array, $rootNodeName = 'data', $xml = null, $charset = null)
{
if ($xml === null) {
$xml = new SimpleXmlElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><{$rootNodeName}/>");
}
foreach ($array as $key => $value) {
$key = preg_replace('/[^a-z]/i', '', $key);
if (is_array($value) && !empty($value)) {
$node = $xml->addChild($key);
foreach ($value as $k => $v) {
if (is_numeric($v)) {
unset($value[$k]);
$node->addAttribute($k, $v);
}
}
self::arrayToXml($value, $rootNodeName, $node, $charset);
} else {
if (is_int($key)) {
$xml->addChild($value, 'true');
} else {
$charset = $charset ? $charset : 'utf-8';
if (strcasecmp($charset, 'utf-8') !== 0 && strcasecmp($charset, 'utf8') !== 0) {
$value = iconv($charset, 'UTF-8', $value);
}
$value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
$xml->addChild($key, $value);
}
}
}
return $xml->asXML();
}
示例3: parseLinks
/**
* Parse Links
*
* Parses an XML file returned by a link resolver
* and converts it to a standardised format for display
*
* @param string $xmlstr Raw XML returned by resolver
*
* @return array Array of values
*/
public function parseLinks($xmlstr)
{
$records = [];
// array to return
try {
$xml = new \SimpleXmlElement($xmlstr);
} catch (\Exception $e) {
return $records;
}
$root = $xml->xpath("//ctx_obj_targets");
$xml = $root[0];
foreach ($xml->children() as $target) {
$record = [];
$record['title'] = (string) $target->target_public_name;
$record['href'] = (string) $target->target_url;
$record['service_type'] = (string) $target->service_type;
if (isset($target->coverage->coverage_text)) {
$coverageText =& $target->coverage->coverage_text;
$record['coverage'] = (string) $coverageText->threshold_text->coverage_statement;
if (isset($coverageText->embargo_text->embargo_statement)) {
$record['coverage'] .= ' ' . (string) $coverageText->embargo_text->embargo_statement;
$record['embargo'] = (string) $coverageText->embargo_text->embargo_statement;
}
}
if (isset($target->coverage)) {
$record['coverage_details'] = json_decode(json_encode($target->coverage), true);
}
array_push($records, $record);
}
return $records;
}
示例4: fetch
public function fetch($params)
{
$feed = null;
//debug($params);
if (isset($params['source'])) {
$handle = fopen($params['source'], "rb");
$feed = stream_get_contents($handle);
fclose($handle);
} else {
echo 'A source is required.';
}
//debug($feed);
$feed = new SimpleXmlElement($feed);
if (isset($params['limit']) and $params['limit'] > 0) {
// Not possible in SimpleXmlElement objects
}
//debug($feed); exit;
if (isset($params['raw']) and $params['raw'] === true) {
echo $feed->asXml();
}
if (isset($params['convert']) and $params['convert'] == 'json') {
$feed = $this->convertToJson($feed->asXml());
}
//debug($feed);
echo $feed;
}
示例5: addXmlIdentifier
/**
* Add to service xml description identifier by REST info
* @param SimpleXmlElement $xmlProduct
* @param array $serviceRest
*/
private function addXmlIdentifier($xmlProduct, $serviceRest) {
if($serviceRest['1c_id'] != null ) {
$xmlProduct->addChild("Ид", $serviceRest['1c_id']);
} else {
$xmlProduct->addChild("Ид", $serviceRest['servicename']);
}
}
示例6: arrayToXml
/**
* arrayToXml
*
* @param string $array Array to convert to xml
* @param string $rootNodeName Name of the root node
* @param string $xml SimpleXmlElement
* @return string $asXml String of xml built from array
*/
public static function arrayToXml($array, $rootNodeName = 'data', $xml = null)
{
if ($xml === null) {
$xml = new SimpleXmlElement("<?xml version=\"1.0\" encoding=\"utf-8\"?><{$rootNodeName}/>");
}
foreach ($array as $key => $value) {
$key = preg_replace('/[^a-z]/i', '', $key);
if (is_array($value) && !empty($value)) {
$node = $xml->addChild($key);
foreach ($value as $k => $v) {
if (is_numeric($v)) {
unset($value[$k]);
$node->addAttribute($k, $v);
}
}
self::arrayToXml($value, $rootNodeName, $node);
} else {
if (is_int($key)) {
$xml->addChild($value, 'true');
} else {
$value = htmlentities($value);
$xml->addChild($key, $value);
}
}
}
return $xml->asXML();
}
示例7: get
public function get($count = -1, $rating = null, $filter = null)
{
if ($count == 0 || !is_numeric($count)) {
$count = -1;
}
$url = sprintf(self::BACKEND_URL, $this->username) . ($filter == -1 ? '+in%3Ascraps' : '') . ($filter > 0 ? '%2F' . $filter : '');
$this->data = $this->request($url);
$xml = new SimpleXmlElement($this->data);
$ns = $xml->getNamespaces(true);
$items = null;
foreach ($xml->channel->item as $item) {
$media = $item->children($ns['media']);
if (!(empty($this->rating) || $this->rating == 'all') && $media->rating != $this->rating) {
continue;
}
if ($media->text) {
continue;
}
if ($media->text) {
continue;
}
if ($media->text) {
continue;
}
$items .= sprintf('<li><a href="%1$s" title="%2$s - %3$s"><img src="%4$s" alt="%2$s - %3$s"/></a></li>', $item->link, $media->title, $media->copyright, $media->content->attributes()->url);
--$count;
if ($count > -1 && $count == 0) {
break;
}
}
return sprintf('<ul class="da-widgets gallery">%s</ul>', $items);
}
示例8: addXmlAccountId
/**
* Add to account xml description identifier.
* @param SimpleXmlElement $account
* @param array $accountRest
*/
private function addXmlAccountId($account, $accountRest) {
$accountId = $accountRest['1c_id'];
if($accountId == null) {
$accountId = $accountRest['accountname'];
}
$account->addChild("Ид", $accountId);
}
示例9: _serializeRecurser
protected function _serializeRecurser($graphNodes, SimpleXmlElement $xmlNode)
{
// @todo find a better way to handle concurrency.. if no clone, _position in node gets messed up
if ($graphNodes instanceof Zend_Tool_Project_Structure_Node) {
$graphNodes = clone $graphNodes;
}
foreach ($graphNodes as $graphNode) {
if ($graphNode->isDeleted()) {
continue;
}
$nodeName = $graphNode->getContext()->getName();
$nodeName[0] = strtolower($nodeName[0]);
$newNode = $xmlNode->addChild($nodeName);
$reflectionClass = new ReflectionClass($graphNode->getContext());
if ($graphNode->isEnabled() == false) {
$newNode->addAttribute('enabled', 'false');
}
foreach ($graphNode->getPersistentParameters() as $paramName => $paramValue) {
$newNode->addAttribute($paramName, $paramValue);
}
if ($graphNode->hasChildren()) {
self::_serializeRecurser($graphNode, $newNode);
}
}
}
示例10: Save
public function Save()
{
//if installed goto dashboard
if ($this->getSystemSetting(OpenSms::INSTALLATION_STATUS)) {
OpenSms::redirectToAction('index', 'dashboard');
}
//var_dump($_POST);die();
// CREATE
$config = new SimpleXmlElement('<settings/>');
$config->{OpenSms::VERSION} = $this->getSystemSetting(OpenSms::VERSION);
$config->{OpenSms::SITE_NAME} = $this->getFormData(OpenSms::SITE_NAME);
$config->{OpenSms::SITE_URL} = $this->getFormData(OpenSms::SITE_URL);
$config->{OpenSms::DB_TYPE} = 'mysql';
$config->{OpenSms::DB_HOST} = $this->getFormData(OpenSms::DB_HOST);
$config->{OpenSms::DB_NAME} = $this->getFormData(OpenSms::DB_NAME);
$config->{OpenSms::DB_TABLE_PREFIX} = $this->getFormData(OpenSms::DB_TABLE_PREFIX);
$config->{OpenSms::DB_USERNAME} = $this->getFormData(OpenSms::DB_USERNAME);
$config->{OpenSms::DB_PASSWORD} = $this->getFormData(OpenSms::DB_PASSWORD);
$config->{OpenSms::DB_PASSWORD} = $this->getFormData(OpenSms::DB_PASSWORD);
$config->{OpenSms::CURRENT_THEME_KEY} = $this->getFormData(OpenSms::CURRENT_THEME_KEY);
$config->{OpenSms::OPEN_PRICE_PER_UNIT} = $this->getFormData(OpenSms::OPEN_PRICE_PER_UNIT);
$config->{OpenSms::OPEN_UNITS_PER_SMS} = $this->getFormData(OpenSms::OPEN_UNITS_PER_SMS);
$config->{OpenSms::INSTALLATION_STATUS} = false;
//unlink(OpenSms::SETTINGS_FILE_PATH);
$config->saveXML(OpenSms::SETTINGS_FILE_PATH);
$this->setNotification('Settings saved', 'settings_save');
OpenSms::redirectToAction('index');
}
示例11: parse
public function parse()
{
$sXML = new \SimpleXmlElement($this->xml);
$ns = $sXML->getNamespaces(true);
foreach ((array) $sXML->channel as $k => $v) {
if ($k == 'item') {
continue;
}
if (!is_string($v) && !is_array($v)) {
$v = (array) $v;
}
$this->meta[$k] = $v;
}
foreach ($sXML->channel->item as $item) {
$i = (array) $item;
foreach ($ns as $space => $val) {
$children = $item->children($val);
if ($children) {
foreach ($children as $c => $a) {
$i[$c] = (string) $a;
}
}
}
$this->items[] = $i;
}
}
示例12: addOrderProps
/**
* Add props to order, specified for website.
* @param SimpleXmlElement $document
* @param array $salesOrderRest
*/
protected function addOrderProps($document, $salesOrderRest)
{
$documentProps = $document->addChild("ЗначенияРеквизитов");
$documentProp = $documentProps->addChild("ЗначениеРеквизита");
$documentProp->addChild("Наименование", "Номер по 1С");
$documentProp->addChild("Значение", $salesOrderRest['fromsite']);
if ($salesOrderRest['sostatus'] == "Created") {
$documentProp = $documentProps->addChild("ЗначениеРеквизита");
$documentProp->addChild("Наименование", "Проведен");
$documentProp->addChild("Значение", "false");
}
if ($salesOrderRest['sostatus'] == "Approved") {
$documentProp = $documentProps->addChild("ЗначениеРеквизита");
$documentProp->addChild("Наименование", "Проведен");
$documentProp->addChild("Значение", "true");
}
if ($salesOrderRest['sostatus'] == "Delivered") {
$documentProp = $documentProps->addChild("ЗначениеРеквизита");
$documentProp->addChild("Наименование", "Проведен");
$documentProp->addChild("Значение", "true");
$documentProp = $documentProps->addChild("ЗначениеРеквизита");
$documentProp->addChild("Наименование", "Номер оплаты по 1С");
$documentProp->addChild("Значение", $salesOrderRest['salesorder_no']);
}
if ($salesOrderRest['sostatus'] == "Cancelled") {
$documentProp = $documentProps->addChild("ЗначениеРеквизита");
$documentProp->addChild("Наименование", "Отменен");
$documentProp->addChild("Значение", "true");
$documentProp = $documentProps->addChild("ЗначениеРеквизита");
$documentProp->addChild("Наименование", "Проведен");
$documentProp->addChild("Значение", "false");
}
}
示例13: parseXml
/**
* Parse the input SimpleXmlElement into an array
*
* @param SimpleXmlElement $node xml to parse
* @return array
*/
protected function parseXml($node)
{
$data = array();
foreach ($node->children() as $key => $subnode) {
if ($subnode->count()) {
$value = $this->parseXml($subnode);
if ($subnode->attributes()) {
foreach ($subnode->attributes() as $attrkey => $attr) {
$value['@' . $attrkey] = (string) $attr;
}
}
} else {
$value = (string) $subnode;
}
if ($key === 'item') {
if (isset($subnode['key'])) {
$data[(string) $subnode['key']] = $value;
} elseif (isset($data['item'])) {
$tmp = $data['item'];
unset($data['item']);
$data[] = $tmp;
$data[] = $value;
}
} elseif (key_exists($key, $data)) {
if (false === is_array($data[$key])) {
$data[$key] = array($data[$key]);
}
$data[$key][] = $value;
} else {
$data[$key] = $value;
}
}
return $data;
}
示例14: getPath
/**
* Gets a path to a node via SPL Stack implementation
*
* Pass in the child node and will recurse up the XML tree to print out
* the path in the tree to that node
*
* <config>
* <path>
* <to>
* <node>
* Node Value
* </node>
* </to>
* </path>
* </config>
*
* If you pass in the "node" object, this will print out
* config/path/to/node/
*
* @param SimpleXmlElement $element Child element to find path to
*
* @return string
* @access public
*/
public function getPath(SimpleXmlElement $element)
{
$this->_iterator->push($element->getName() . '/');
if (!$element->getSafeParent()) {
return $this->_iterator->pop();
}
return $this->getPath($element->getParent()) . $this->_iterator->pop();
}
示例15: getPath
/**
* Gets a path to a node via array implementation
*
* Pass in the child node and will recurse up the XML tree to print out
* the path in the tree to that node
*
* <config>
* <path>
* <to>
* <node>
* Node Value
* </node>
* </to>
* </path>
* </config>
*
* If you pass in the "node" object, this will print out
* config/path/to/node/
*
* @param SimpleXmlElement $element Child element to find path to
*
* @return string
* @access public
*/
public function getPath(SimpleXmlElement $element)
{
$this->_iterator[] = $element->getName() . '/';
if (!$element->getSafeParent()) {
return array_pop($this->_iterator);
}
return $this->getPath($element->getParent()) . array_pop($this->_iterator);
}