本文整理汇总了PHP中DomDocument::loadXml方法的典型用法代码示例。如果您正苦于以下问题:PHP DomDocument::loadXml方法的具体用法?PHP DomDocument::loadXml怎么用?PHP DomDocument::loadXml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DomDocument
的用法示例。
在下文中一共展示了DomDocument::loadXml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: assertMatchesXpath
protected function assertMatchesXpath($html, $expression, $count = 1)
{
$dom = new \DomDocument('UTF-8');
try {
// Wrap in <root> node so we can load HTML with multiple tags at
// the top level
$dom->loadXml('<root>'.$html.'</root>');
} catch (\Exception $e) {
return $this->fail(sprintf(
"Failed loading HTML:\n\n%s\n\nError: %s",
$html,
$e->getMessage()
));
}
$xpath = new \DOMXPath($dom);
$nodeList = $xpath->evaluate('/root'.$expression);
if ($nodeList->length != $count) {
$dom->formatOutput = true;
$this->fail(sprintf(
"Failed asserting that \n\n%s\n\nmatches exactly %s. Matches %s in \n\n%s",
$expression,
$count == 1 ? 'once' : $count . ' times',
$nodeList->length == 1 ? 'once' : $nodeList->length . ' times',
// strip away <root> and </root>
substr($dom->saveHTML(), 6, -8)
));
}
}
示例2: parseString
/**
* Parse the given xml definition string and return a
* Console_CommandLine instance constructed with the xml data.
*
* @param string $xmlstr the xml string to parse
*
* @return object a Console_CommandLine instance
* @access public
* @static
*/
public static function parseString($xmlstr)
{
$doc = new DomDocument();
$doc->loadXml($xmlstr);
self::validate($doc);
$root = $doc->childNodes->item(0);
return self::_parseCommandNode($root, true);
}
示例3: parseString
private function parseString($xmlStr)
{
$document = new DomDocument();
try {
$document->loadXml($xmlStr);
} catch (Exception $ex) {
}
return new XmlDataType($document);
}
示例4: parseString
/**
* Parses the given xml definition string and returns a
* Console_CommandLine instance constructed with the xml data.
*
* @param string $xmlstr The xml string to parse
*
* @return Console_CommandLine A parser instance
*/
public static function parseString($xmlstr)
{
$doc = new DomDocument();
$doc->loadXml($xmlstr);
self::validate($doc);
$nodes = $doc->getElementsByTagName('command');
$root = $nodes->item(0);
return self::_parseCommandNode($root, true);
}
示例5: prettyPrint
public function prettyPrint($code, $lang)
{
switch ($lang) {
case 'json':
return json_encode(json_decode($code), JSON_PRETTY_PRINT);
case 'xml':
$xml = new \DomDocument('1.0');
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->loadXml($code);
return $xml->saveXml();
default:
return $code;
}
}
示例6: 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;
}
示例7: parse
public function parse($content, __Configuration &$configuration)
{
libxml_use_internal_errors(true);
$dom = new DomDocument("1.0");
$dom->loadXml($content);
if ($dom->documentElement != null) {
$this->_parseDomNode($dom->documentElement, $configuration);
} else {
$xml_content_array = preg_split('/[\\n\\r]+/', $content);
$parse_errors = libxml_get_errors();
$error_descriptions_array = array();
foreach ($parse_errors as $_parse_error) {
$error_descriptions_array[] = $this->_getReadableXMLErrorDescription($_parse_error, $xml_content_array);
}
$error_description = join("\n", $error_descriptions_array);
libxml_clear_errors();
throw new __ConfigurationException($error_description);
}
}
示例8: parseResponse
/**
* Parse the response XML into an associative array of values.
*
* @param string response XML
*/
protected function parseResponse($xml)
{
$dom = new \DomDocument();
try {
// Munge errors into exceptions
set_error_handler('\\Pway\\Response::domErrorHandler');
$dom->loadXml($xml);
restore_error_handler();
} catch (\DomException $e) {
$this->error = self::ERROR_XML;
$this->errorMessage = $e->getMessage();
}
if ($this->error) {
return false;
}
foreach ($dom->firstChild->childNodes as $node) {
$this->responseData[$node->nodeName] = $node->nodeValue;
}
}
示例9: fetchAttributes
/**
* Fetches the necessary attribute information for
* the defined media item.
*
* @return bool
*/
protected function fetchAttributes()
{
if (ini_get('allow_url_fopen')) {
$youtubeXml = @file_get_contents($this->apiUrl . '/' . $this->mediaId);
if (isset($http_response_header) && strpos($http_response_header[0], '200') === false) {
throw new ExternalMediaDriver_InvalidID('YouTube ID "' . $this->mediaId . '" does not exist');
}
$dom = new DomDocument();
$dom->loadXml($youtubeXml);
$xPath = new DomXPath($dom);
// Gather all details
$group = $xPath->query('//media:group')->item(0);
$this->attributes['title'] = $xPath->query('media:title', $group)->item(0)->nodeValue;
$this->attributes['description'] = $xPath->query('media:description', $group)->item(0)->nodeValue;
$this->attributes['videoUrl'] = $xPath->query('media:content/@url[starts-with(., "http")]', $group)->item(0)->value;
$this->attributes['thumbUrl'] = $xPath->query('media:thumbnail[position()=1]/@url', $group)->item(0)->value;
return true;
} else {
return false;
}
}
示例10: getOsmChangeXml
/**
* Generate and return the OsmChange XML required to record the changes
* made to the object in question.
*
* @return string
* @link http://wiki.openstreetmap.org/wiki/OsmChange
*/
public function getOsmChangeXml()
{
$type = $this->getType();
if ($this->dirty) {
$version = $this->getVersion();
$version++;
$domd = new DomDocument();
$domd->loadXml($this->getXml());
$xpath = new DomXPath($domd);
$nodelist = $xpath->query("//{$type}");
$nodelist->item(0)->setAttribute('action', $this->action);
$nodelist->item(0)->setAttribute('id', $this->getId());
if (!is_null($this->changesetId)) {
$nodelist->item(0)->setAttribute('changeset', $this->changesetId);
}
$tags = $xpath->query("//{$type}/tag");
$set = array();
for ($i = 0; $i < $tags->length; $i++) {
$key = $tags->item($i)->getAttribute('k');
$val = $tags->item($i)->getAttribute('v');
$set[$key] = $val;
}
$diff = array_diff($this->getTags(), $set);
// Remove existing tags
for ($i = 0; $i < $tags->length; $i++) {
$rkey = $tags->item($i)->getAttribute('k');
if (isset($diff[$rkey])) {
$nodelist->item(0)->removeChild($tags->item($i));
}
}
foreach ($diff as $key => $value) {
$new = $domd->createElement('tag');
$new->setAttribute('k', $key);
$new->setAttribute('v', $value);
$nodelist->item(0)->appendChild($new);
}
$xml = $domd->saveXml($nodelist->item(0));
$xml = "<{$this->action}>{$xml}</{$this->action}>";
return $this->osmChangeXml($xml);
} elseif ($this->action == 'delete') {
$xml = null;
$domd = new DomDocument();
$domd->loadXml($this->getXml());
$xpath = new DomXPath($domd);
$n = $xpath->query("//{$type}");
$version = $this->getVersion();
$version++;
if (!is_null($this->changesetId)) {
$n->item(0)->setAttribute('changeset', $this->changesetId);
}
$n->item(0)->setAttribute('action', 'delete');
$xml = $domd->saveXml($n->item(0));
return $this->osmChangeXml("<delete>{$xml}</delete>");
}
}
示例11: saveXML
function saveXML($simplexml)
{
global $config;
$domDoc = new DomDocument('1.0', 'utf-8');
$domDoc->formatOutput = true;
$domDoc->preserveWhiteSpace = false;
$domDoc->loadXml($simplexml->asXml());
return file_put_contents($config['xml_tv'], $domDoc->saveXml());
}
示例12: osmChangeXml
/**
* Amend osmChangeXml with specific updates pertinent to this Way object.
*
* @param string $xml OSM Change XML as generated by getOsmChangeXml
*
* @return string
* @see getOsmChangeXml
* @link http://wiki.openstreetmap.org/wiki/OsmChange
*/
public function osmChangeXml($xml)
{
if ($this->dirtyNodes) {
$domd = new DomDocument();
$domd->loadXml($xml);
$xpath = new DomXPath($domd);
$nodelist = $xpath->query('//' . $this->action . '/way');
$nd = $xpath->query("//{$this->action}/way/nd");
// Remove nodes if appropriate.
for ($i = 0; $i < $nd->length; $i++) {
$ref = $nd->item($i)->getAttribute('ref');
if (array_search($ref, $this->nodes) === false) {
$nodelist->item(0)->removeChild($nd->item($i));
}
}
// Add new nodes.
foreach ($this->nodesNew as $new) {
$el = $domd->createElement('nd');
$el->setAttribute('ref', $new);
$nodelist->item(0)->appendChild($el);
}
// Remove blank lines in XML - minimise bandwidth usage.
return preg_replace("/(^[\r\n]*|[\r\n]+)[\\s\t]*[\r\n]+/", '', $domd->saveXml($nodelist->item(0)));
return $domd->saveXml($nodelist->item(0));
} else {
return $xml;
}
}
示例13: sendRequest
/**
* Performs a Plesk API request, returns raw API response text
*
* @param string $packet
*
* @return string
* @throws ApiRequestException
*/
private function sendRequest($packet)
{
$domdoc = new \DomDocument('1.0', 'UTF-8');
if ($domdoc->loadXml($packet) === false) {
$this->error = 'Failed to load payload';
return false;
}
$body = $domdoc->saveHTML();
return $this->http->sendRequest($body);
}
示例14: fromString
public static function fromString($xmlString)
{
$xml = new \DomDocument('1.0', 'UTF-8');
$xml->loadXml($xmlString);
return new self($xml);
}
示例15: getDom
/**
* Sets up a domdocument for the functions.
*
* @return DomDocument
* The domdocument to modify
*/
protected function getDom()
{
if (isset($this->datastream->content) && $this->autoCommit) {
// @todo Proper exception handling.
$document = new DomDocument();
$document->preserveWhiteSpace = FALSE;
$document->loadXml($this->datastream->content);
} else {
if (!is_null($this->domCache) && !$this->autoCommit) {
$document = $this->domCache;
} else {
$document = new DomDocument("1.0", "UTF-8");
$rootelement = $document->createElement('rdf:RDF');
$document->appendChild($rootelement);
}
}
// Setup the default namespace aliases.
foreach ($this->namespaces as $alias => $uri) {
// if we use setAttributeNS here we drop the rdf: from about which
// breaks things, so we do this, then the hack below.
$document->documentElement->setAttribute("xmlns:{$alias}", $uri);
}
// this is a hack, but it makes sure namespaces are properly registered
$document_namespaces = new DomDocument();
$document_namespaces->preserveWhiteSpace = FALSE;
$document_namespaces->loadXml($document->saveXML());
return $document_namespaces;
}