simplexml addChild in a certain tag - php

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];

Related

XML Changing value of namespace property

I need to change a value of a nested namespace property within a XML document before I submit it to an EPP service.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
<command>
<info>
<host:info xmlns:host="urn:ietf:params:xml:ns:host-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:host-1.0 host-1.0.xsd">
<host:name>ns1.example.test.example.com</host:name>
</host:info>
</info>
<clTRID>NORID-14373-1207137695427775</clTRID>
</command>
</epp>
In the above XML, I need to change the host:name elements value. I am using PHP simplexml_load_string to first modify the values from the XML schema as shown below.
$xml = simplexml_load_string(file_get_contents($fn));
$xml->command->clTRID = GUID(); // This works perfectly
$xml->command->info->name = 'somename'; // Does not work :)
What is the correct way to do this?
Use the following code
Note I am echoing the data for your reference as how to use it
echo $xml->command->info->children('host', true)->info->name;
Total simple code which I tried
$xmls = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
<command>
<info>
<host:info xmlns:host="urn:ietf:params:xml:ns:host-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:host-1.0 host-1.0.xsd">
<host:name>ns1.example.test.example.com</host:name>
</host:info>
</info>
<clTRID>NORID-14373-1207137695427775</clTRID>
</command>
</epp>';
$xml = simplexml_load_string($xmls);
$xml->command->clTRID = 'something'; // This works perfectly
echo $xml->command->info->children('host', true)->info->name;
$xml->command->info->children('host', true)->info->name = 'some new name';
echo $xml->command->info->children('host', true)->info->name;

How to get the specific inner node with SimpleXml?

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();

php scripts gets a node value , if it equals specified value ; changes a node value has its same parent node

just like the title I need help in a php script gets "name" node value , if it equals specified given value ; changes "rate" node value has its same "person" parent node.
the xml file 'rate.xml' like the following
<?xml version="1.0"?>
<user>
<person>
<name>jessy</name>
<rate>2</rate>
</person>
<person>
<name>mice</name>
<rate>5</rate>
</person>
</user>
the script will be similar to this
<?php
$name = $_POST['name'];
$like = $_POST['like'];
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadXML(file_get_contents ('rate.xml'));
$xpath = new DOMXPath($doc);
$nlist = $xpath->query("//user/person/rate");
$node = $nlist->item(0);
$newval = trim($node->nodeValue)-1;
$node->nodeValue = $newval;
file_put_contents ('rate.xml', $doc->saveXML());
?>
My missing part is how to type "if" condition checks if name equals the name I passed to "POST" and how to limit the change to the "rate" node that comes after this "name" node with the "POST['name']" value.
implementation will be a great help
Kind regards :)
You can just do this
$name = "jessy";
$xml = '<?xml version="1.0"?>
<user>
<person>
<name>jessy</name>
<rate>2</rate>
</person>
<person>
<name>mice</name>
<rate>5</rate>
</person>
</user>';
$xml = new SimpleXMLElement($xml);
for($i = 0; $i < count($xml->person); $i ++) {
if ($xml->person[$i]->name == $name) {
$xml->person[$i]->rate = $xml->person[$i]->rate - 1;
}
}
echo $xml->asXML();
Output
<?xml version="1.0"?>
<user>
<person>
<name>jessy</name>
<rate>1</rate> <----- Modified rate for jessy
</person>
<person>
<name>mice</name>
<rate>5</rate>
</person>
</user>

PHP restructure recursive XML with closing tag on same line? ie <value="blah"/>

I need to restructure a very large xml source, example is at
http://www.fluffyduck.com.au/sampleXML.xml
I need to modify it for jstree however I'm not sure how to manipulate the data recursively, as loading it as xml with simpleXml only see's the first 1 user record.
<user id="41" username="bsmain" firstname="Boss" lastname="MyTest" fullname="Test Name" email="lalal#test.com" logins="1964" lastseen="11/09/2012">
</user>
to
<user id="41">
<content><name>bsmain</name></content>
</user>
The problem is some xml lines do not have a closing tag such as , but instead look like this :
<user id="61" username="underling" firstname="Under" lastname="MyTest" fullname="Test Name" email="lalal#test.com" logins="4" lastseen="08/09/2009"/>
If i modify this record and add underling jstree does not recognise it, i'm presuming the /> at the end is the same as ?
I did want to do this in XML but am thinking it may be easier, to simply somehow parse the xml file 'line by line', read in the line of data explode it perhaps,
then create a new variable storing it with modified contents such as :
<user id="61">
<content><name>bsmain</name>
</user>
and on the rows where /> exists at the end, manually insert a tag.
there has to be a smarter/faster way to achieve this.
Your best bet is to use DOMDocument for XML parsing. I have written an example that transforms attributes (excluding the id attribute) to content elements:
Code
<?php
$s =
'<users>' .
'<user id="61" username="underling" firstname="Under" lastname="MyTest" fullname="Test Name" email="lalal#test.com" logins="4" lastseen="08/09/2009"/>' .
'<user id="61" username="underling" firstname="Under" lastname="MyTest" fullname="Test Name" email="lalal#test.com" logins="4" lastseen="08/09/2009"/>' .
'<user id="8" test="testvalue"></user>' .
'</users>';
$doc = new DOMDocument();
$doc->loadXML($s);
$users = $doc->getElementsByTagName("user");
foreach ($users as $user)
{
if ($user->hasAttributes())
{
// create content node
$content = $user->appendChild($doc->createElement("content"));
// transform attributes into content elements
for ($i = 0; $i < $user->attributes->length; $i++)
{
$attr = $user->attributes->item($i);
if (strtolower($attr->name) != "id")
{
if ($user->removeAttribute($attr->name))
{
$content->appendChild($doc->createElement($attr->name, $attr->value));
$i--;
}
}
}
}
}
header("Content-Type: text/xml");
echo $doc->saveXML();
?>
Output
<users>
<user id="61">
<content>
<username>underling</username>
<firstname>Under</firstname>
<lastname>MyTest</lastname>
<fullname>Test Name</fullname>
<email>lalal#test.com</email>
<logins>4</logins>
<lastseen>08/09/2009</lastseen>
</content>
</user>
<user id="61">
<content>
<username>underling</username>
<firstname>Under</firstname>
<lastname>MyTest</lastname>
<fullname>Test Name</fullname>
<email>lalal#test.com</email>
<logins>4</logins>
<lastseen>08/09/2009</lastseen>
</content>
</user>
<user id="8">
<content>
<test>testvalue</test>
</content>
</user>
</users>

PHP XML adding new entry

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();

Categories