How to retrieve attribute of root element of DOMDocument? [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
DOMDocument::load - PHP - Getting attribute value
I use the following code:
$str = 'text';
$dom = new DOMDocument;
$dom->loadXML($str);
I want to obtain the value of root element of $str.
In this example "some_link" should be returned. In real case $str is read from file.
How to achieve this?

Try:
$dom->documentElement->getAttribute('%yourAttrName%');

Related

How to add an attribute to XML in PHP? [duplicate]

This question already has answers here:
Unable to add namespace to an attribute with PHP's SimpleXML
(3 answers)
Closed 8 years ago.
I am very new to XML so apologies if this is a simple question;
I have the following line of code in a PHP file which is used to create a resulting line of code in an XML file via a PHP form;
$Savings = $xml->createElement('Savings');
This creates the following line in my XML file;
<Savings>263.4</Savings>
How would I change this line in my XML file to look as follows;
<Savings Currency="EUR">263.4</Savings>
I have tried the following but I get no additional output;
$Savings = $xml->createElementNS('EUR', 'Savings');
Use setAttribute:
$Savings = $xml->createElement('Savings');
$Savings->setAttribute('Currency', 'EUR');

How would one get the value/text of an anchor DOMElement? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get Anchor text using DomDocument?
Getting node's text in PHP DOM
I have a script that finds all the anchor tags of a certain class in a DOMDocument. I am looking to echo the text that is contained within the <a>"....."</a> tags.
You can access DOMText node directly using XPath:
$xpath = new DOMXPath($dom_document);
$node = $xpath->query('//a/text()')->item(0);
echo $node->textContent; // text
You can use preg_match(). Here is an example:
$link = 'www.CoursesWeb.net';
if(preg_match('/\<a([^\>]*)\>(.*?)\<\/a\>/i', $link, $mc)) {
echo $mc[2]; // www.CoursesWeb.net
}

getElementByAttribute PHP DOM [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Select xml node by attribute in php
Is there any function like getElementByAttribute in PHP? If no, how do I create a workaround?
E.g.
<div class="foo">FOO!</div>
How do I match that element?
You can use XPath:
$xpath = new DOMXPath($document);
$results = $xpath->query("//*[#class='foo']");
Here's a demo.

Tags still remaining after using simplexml_load_string [duplicate]

This question already has answers here:
Getting actual value from PHP SimpleXML node [duplicate]
(4 answers)
Closed 8 years ago.
I am using simplexml_load_string for XML packets. In my scenario, the XML string I want to convert is known as k.
My problem, however, is that when I use k, tags still remain that weren't parsed (<k>, <\k>).
For example, I use
$x->k, and I get back <k>DATA I WANT HERE<\EK>.
How do I get rid of these?
What the code does: It connects to a game and logs in.
Use InnerNode to get the value without the tags:
$x->k->InnerNode
You can also do a typecast:
(string)$x->k
I tried this and seem to be getting the string.
<?php
$str = "<msg t='sys'><body action='rndK' r='-1'><k>qH~e9Gmt</k></body></msg>";
$xml = simplexml_load_string( $str );
echo $xml->body->k; // gives 'qH~e9Gmt'
?>

How to detect new line from the parsed XML with simpleload_xml_string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP SimpleXML doesn't preserve line breaks in XML attributes
I have following XML
$xmldatas = '<layer text="name
id"></layer>';
I have parse this XML with
$xml = simplexml_load_string($xmldatas);
But when I checked the $xml, the \n is been replaced with space. I want the new line remains as it is after the xml parsing.
But how can I do that ?
Thanks
I don't think that xml will accept a new line character in the tag option text.
If you are generating the xml maybe you want to do something like this?
$xmldatas = '<layer><text>name
id</text></layer>';

Categories