Get root node of XML doc using simplexml
Are you wanting to get the name of the root node?
$xml = simplexml_load_string($str);
echo $xml->getName();
SimpleXML - how to get root object name?
You should remove the extraneous questions from this question and post them separately. Keeping questions short makes it easier for readers to respond.
To answer your question
Where is my root element (config_admin) or how do i get it's name?
Your root element is returned by simplexml_load_string()
or simplexml_load_file()
. In case you don't know its name, you can get its name with getName()
. If you know its name, you should always name the variable the same name as the root element, as it makes it easier to match variables to the node they represent.
$config_admin = simplexml_load_file('config.xml');
echo $config_admin->getName();
PHP SimpleXML get document node
As far as I understand it, the root element is simply your xml
variable so to access its attributes you can simply use foreach ($xml->attributes() as $z => $y)
.
SimpleXML root node prefix php
The problem with SimpleXML is that if you don't specify the namespace of an element when adding it, it assumes the namespace of the parent node (hence the p:
). To add it to the default namespace (i.e. without a prefix) there are a couple of things you will need to change.
First is to add a default namespace declaration at the root element...
$xml = new SimpleXMLElement('<p:FatturazioneElettronica
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:p="http://microsoft.com/wsdl/types/"
xmlns="http://dummy.com" />');
I've just added is as xmlns="http://dummy.com"
near the end.
Then when adding the first element to the document, add this to the newly defined default namespace...
$FatturaElettronicaHeader = $xml->addChild('FatturaElettronicaHeader',
null, 'http://dummy.com');
Get root node attributes
What is your existing PHP code? Are you using DOMDocument? SimpleXML?
The correct Xpath expression is 'string(/records/@timestamp)'
For SimpleXML see http://php.net/manual/en/simplexmlelement.xpath.php
e.g.
<?php
$string = <<<XML
<records timestamp="1264777862">
<record></record>
<record></record>
<record></record>
<record></record>
</records>
XML;
$xml = new SimpleXMLElement($string);
$result = $xml->xpath('string(/records/@timestamp)');
while(list( , $node) = each($result)) {
echo $node,"\n";
}
?>
And for DOMXPath see http://www.php.net/manual/en/domxpath.evaluate.php
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$xpath = new DOMXPath($doc);
// our query is relative to the records node
$query = 'string(/records/@timestamp)';
$timestamp = $xpath->evaluate($query);
echo $timestamp ."\n";
?>
EDIT
See edit above. Need to cast to string in the xpath expression
SimpleXML get node value
$xml = new SimpleXMLElement($xml);
$this->xmlcode = (string) $xml->parent[0]->child1;
Adding in new XML root node
This seem to work
$units = $dom->createElement('units');
$units->appendChild($dom->documentElement);
$dom->appendChild($units);
DEMO
Unable to get root node from XML document
The $domLead already is the documentElement, not the document. You're loading the XML into a SimpleXMLElement object, this will always represent an element, never a document. So you're importing the document element into DOM.
$xmlLead = new SimpleXMLElement('<Operations username="test" password="test"/>');
$domLead = dom_import_simplexml($xmlLead);
var_dump(get_class($domLead));
Output:
string(10) "DOMElement"
If you would like to use DOM, create the DOMDocument directly and load your XML into it:
$dom = new DOMDocument();
$dom->loadXml('<Operations username="test" password="test"/>');
var_dump(get_class($dom), get_class($dom->documentElement));
Output:
string(11) "DOMDocument"
string(10) "DOMElement"
If you have your XML loaded into a DOM, you can create an DOMXpath object and fetch data:
$dom = new DOMDocument();
$dom->loadXml('<Operations username="test" password="test"/>');
$xpath = new DOMXpath($dom);
var_dump(
$xpath ->evaluate('string(/*/@username)'),
$xpath ->evaluate('string(/*/@password)')
);
Output:
string(4) "test"
string(4) "test"
Related Topics
PHP Function That Creates a Nested Ul Li
Long Integer Is Transformed When Inserted in Shorter Column, Not Truncated. Why? What Is the Formula
PHP Integer and Float Comparison Mismatch
How to Return a Proper Success/Error Message for Jquery .Ajax() Using PHP
Getting the Name of a Child Class in the Parent Class (Static Context)
Create an Order Programmatically with Line Items in Woocommerce 3+
Downloading Attachments to Directory with Imap in PHP, Randomly Works
Generate Url with Parameters from an Array
How to Manage a Single PHP5 Session on Multiple Apache Servers
Detect If Android App Has Been Installed on the Device Using a Mobile Web Page - PHP and Js
PHP Mail Special Characters Utf8
Round to Max Thousand, Hundred etc in PHP
Format Text in Excel File via PHP
What Is the Advantage of Using Try {} Catch {} Versus If {} Else {}
Laravel No Supported Encrypter Found. the Cipher And/Or Key Length Are Invalid