my xml is structured as follow:
<?xml version="1.0" ?>
<user>
<name>
foo
</name>
<token>
jfhsjfhksdjfhsjkfhksjfsdk
</token>
<connection>
<host>
localhost
</host>
<username>
root
</username>
<dbName>
Test
</dbName>
<dbPass>
123456789
</dbPass>
</connection>
</user>
<user>
... same structure...
</user>
I made this code that iterate through all xml node:
function getConString($node)
{
$item = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "con");
$nodes = new SimpleXMLElement($item);
$result = $nodes[0];
foreach($result as $item => $value)
{
if($item == "token")
{
return $value->__toString();
}
}
}
what I'm trying to achieve is that when $node is equal to:
jfhsjfhksdjfhsjkfhksjfsdk
the connection node is returned as array, how I can achieve this?
If the XML you're trying to parse is what you've posted here, it's invalid since
XML documents must contain one root element that is the parent of all other elements:
http://www.w3schools.com/xml/xml_syntax.asp
(and yours doesn't, and parsing such string fails with Exception: String could not be parsed as XML in ...).
So your XML should be:
<?xml version="1.0" ?>
<users>
<user>
<name>
foo
</name>
<token>
jfhsjfhksdjfhsjkfhksjfsdk
</token>
<connection>
<host>
localhost
</host>
<username>
root
</username>
<dbName>
Test
</dbName>
<dbPass>
123456789
</dbPass>
</connection>
</user>
<user>
... same structure...
</user>
</users>
And you don't need to iterate through the collection
// $nodes is SimpleXMLElement
$user = $nodes->user[0];
if($user->token)
return $user->token->__toString();
Related
My XML is as follows
<?xml version="1.0"?>
<Customers>
<customer>
<custID>
1001
</custID>
<fname>
Lama
</fname>
<lname>
Lai
</lname>
<email>
test#hotmail.com
</email>
<password>
qwer
</password>
</customer>
</Customers>
My php code is as follows:
$xmldoc = new DOMDocument();
$xmldoc->load ("../../data/customer.xml");
$customer = $xmldoc->getElementsByTagName("customer");
and the part to save the detail is as follows:
$customerNew = $xmldoc->createElement("customer");
//$customersTAB = $xmldoc->Customers();
//$customerTAB->appendChild($customerNew);
$customerNewFN = $xmldoc->createElement("fname");
$customerNewFNNode = $xmldoc->createTextNode($firstname);
$customerNewFNNode = $customerNewFN->appendChild($customerNewFNNode);
$customerNewFN = $customerNew->appendChild($customerNewFN);
$customerNewLN = $xmldoc->createElement("lname");
$customerNewLNNode = $xmldoc->createTextNode($lastname);
$customerNewLNNode = $customerNewLN->appendChild($customerNewLNNode);
$customerNewLN = $customerNew->appendChild($customerNewLN);
$customerNewEmail = $xmldoc->createElement("email");
$customerNewEmailNode = $xmldoc->createTextNode($email);
$customerNewEmailNode = $customerNewEmail->appendChild($customerNewEmailNode);
$customerNewEmail = $customerNew->appendChild($customerNewEmail);
$customerNewPass = $xmldoc->createElement("password");
$customerNewPassNode = $xmldoc->createTextNode($password);
$customerNewPassNode = $customerNewPass->appendChild($customerNewPassNode);
$customerNewPass = $customerNew->appendChild($customerNewPass);
$customerNew = $xmldoc->fir appendChild($customerNew);
$xmldoc->save('../../data/sample.xml');
I'm trying to create a completely new customer node INSIDe the Customers node. I can't seem to do that. I've pretty sure I've got the inner nodes properly mapped out but I can seemed to append as another child of Customers node.
The overall result should look like this:
<?xml version="1.0"?>
<Customers>
<customer>
<custID>
1001
</custID>
<fname>
Lama
</fname>
<lname>
Lai
</lname>
<email>
test#hotmail.com
</email>
<password>
qwer
</password>
</customer>
<customer>
<custID>
1002
</custID>
<fname>
Lama2
</fname>
<lname>
Lai2
</lname>
<email>
test2#hotmail.com
</email>
<password>
qwer2
</password>
</customer>
</Customers>
What you could do is first get the <customer> elements and then get the first one to be able to get the parent.
$customer = $xmldoc->getElementsByTagName("customer");
The variable $customer is a DOMNodeList
You can get the first item from that list using $customer->item(0), then get the parentNode (which will be <Customers> and then use insertBefore to insert your new element:
$customer->item(0)->parentNode->insertBefore($customerNew);
Demo
I want to add a child for a cetrain user.
$file = simplexml_load_file('gebruikers.xml');
$user = $file->$_SESSION["S_voornaam"];
$usercord = $user->addChild('coordinaat');
$usercord->addChild('1',$_SESSION["S_coordinaat"]);
file_put_contents('gebruikers.xml', $file->asXML());
This is my xml file.
<?xml version="1.0"?>
<gebruikers>
<user>
<voornaam>admin</voornaam>
<achternaam>web</achternaam>
<wachtwoord>1234</wachtwoord>
</user>
<user>
<voornaam>jef</voornaam>
<achternaam>gys</achternaam>
<wachtwoord>1234</wachtwoord>
</user>
</gebruikers>
Example:
When the $_SESSION["S_voornaam"] = admin
and $_SESSION["S_coordinaat"] = (51.2241558, 4.41293399999995)
This is the result i want.
<?xml version="1.0"?>
<gebruikers>
<user>
<voornaam>admin</voornaam>
<achternaam>web</achternaam>
<wachtwoord>1234</wachtwoord>
<coordinaat>
<1>(51.2241558, 4.41293399999995)</1>
</coordinaat>
</user>
<user>
<voornaam>jef</voornaam>
<achternaam>gys</achternaam>
<wachtwoord>1234</wachtwoord>
</user>
</gebruikers>
Example;
$user = $file->xpath('//user[voornaam = "name"]')[0];
I am very new to Zend Framework. And i have tried to get the XML value but cant make it work.
XML:
<?xml version="1.0" encoding="UTF-8"?>
<result count="2">
<blocks>
<listing>
<title>Title 1</title>
<id>1</id>
</listing>
<listing>
<title>Title 2</title>
<id>2</id>
</listing>
</blocks>
</result>
PHP (to find all the title):
$dom = new Zend_Dom_Query();
$dom->setDocumentXml($result);
$results = $dom->queryXpath('/result/blocks/listing/title');
//$dom->queryXpath('/*/*/listing'); no luck
//$dom->queryXpath('///listing'); no luck
foreach($results as $k)
{
Zend_Debug::dump($k->getAttribute('title')); // empty
echo $k->getDocument(); // shows none
}
Any help?
Using queryXpath('/result/blocks/listing/title') your $k already is the DOMElement that represents the <tile>...</title> elements.
You can retrieve the value via $k->nodeValue. For a DOMElement that's the concatenation of all text nodes in the descendant axis.
foreach($results as $k)
{
Zend_Debug::dump($k->nodeValue); // empty
}
title is a node - not an attribute - attributes are placed inside the tag :)
I need to get <name> and <URL> tag's value where subtype="mytype".How can do it in PHP?
I want document name and test.pdf path in my result.
<?xml version="1.0" encoding="UTF-8"?>
<test>
<required>
<item type="binary">
<name>The name</name>
<url visibility="restricted">c:/temp/test/widget.exe</url>
</item>
<item type="document" subtype="mytype">
<name>document name</name>
<url visiblity="visible">c:/temp/test.pdf</url>
</item>
</required>
</test>
Use SimpleXML and XPath, eg
$xml = simplexml_load_file('path/to/file.xml');
$items = $xml->xpath('//item[#subtype="mytype"]');
foreach ($items as $item) {
$name = (string) $item->name;
$url = (string) $item->url;
}
PHP 5.1.2+ has an extension called SimpleXML enabled by default. It's very useful for parsing well-formed XML like your example above.
First, create a SimpleXMLElement instance, passing the XML to its constructor. SimpleXML will parse the XML for you. (This is where I feel the elegance of SimpleXML lies - SimpleXMLElement is the entire library's sole class.)
$xml = new SimpleXMLElement($yourXml);
Now, you can easily traverse the XML as if it were any PHP object. Attributes are accessible as array values. Since you're looking for tags with specific attribute values, we can write a simple loop to go through the XML:
<?php
$yourXml = <<<END
<?xml version="1.0" encoding="UTF-8"?>
<test>
<required>
<item type="binary">
<name>The name</name>
<url visibility="restricted">c:/temp/test/widget.exe</url>
</item>
<item type="document" subtype="mytype">
<name>document name</name>
<url visiblity="visible">c:/temp/test.pdf</url>
</item>
</required>
</test>
END;
// Create the SimpleXMLElement
$xml = new SimpleXMLElement($yourXml);
// Store an array of results, matching names to URLs.
$results = array();
// Loop through all of the tests
foreach ($xml->required[0]->item as $item) {
if ( ! isset($item['subtype']) || $item['subtype'] != 'mytype') {
// Skip this one.
continue;
}
// Cast, because all of the stuff in the SimpleXMLElement is a SimpleXMLElement.
$results[(string)$item->name] = (string)$item->url;
}
print_r($results);
Tested to be correct in codepad.
Hope this helps!
You can use the XML Parser or SimpleXML.
How do i edit a xml files and add a new entry at end of < / user > ?
My xml(filezilla) look like
<FileZillaServer>
<Users>
<User Name="test">
</User>
/* using php to add another users on here <User Name="test2" */
</Users>
</FileZillaServer>
Thank you for help.
You can use the DOMDocument classes to manipulate an XML document.
For instance, you could use something like this :
$str = <<<XML
<FileZillaServer>
<Users>
<User Name="test">
</User>
</Users>
</FileZillaServer>
XML;
$xml = DOMDocument::loadXML($str);
$users = $xml->getElementsByTagName('Users');
$newUser = $xml->createElement('User');
$newUser->setAttribute('name', 'test2');
$users->item($users->length - 1)->appendChild($newUser);
var_dump($xml->saveXML());
Which will get you :
string '<?xml version="1.0"?>
<FileZillaServer>
<Users>
<User Name="test">
</User>
<User name="test2"/></Users>
</FileZillaServer>
' (length=147)
i.e. you :
create a new User element
you set its name attribute
and you append that new element to Users
(There are probably other ways to do that, avoiding the usage of length ; but this is what I first thought about -- quite early in the morning ^^ )
Use SimpleXML. As the name implies, it's the simplest way to deal with XML documents.
$FileZillaServer = simplexml_load_string(
'<FileZillaServer>
<Users>
<User Name="test" />
</Users>
</FileZillaServer>'
);
$User = $FileZillaServer->Users->addChild('User');
$User['Name'] = 'Test2';
echo $FileZillaServer->asXML();