Jump to content

Recommended Posts

Hi there, I was working in a XML file that I need to be updated by PHP, my problem is that using this snippet:

<?php
$xml = simplexml_load_file("myXml.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$user = $sxe->addChild("user");
$user->addChild("name", "Ed Doe");
$user->addChild("age", "34");
$sxe->asXML("myXml.xml");
?>

 

After the php has run the XML has actuallly the updated information but here comes the catch:

If I open the file in the browser like http://...myXml.xml the estructure does look great, indent spaces, breaks and all like in this form:

<user>

<name>Ed Doe</name>

<age>34</age>

</user>

 

But when I try to see it in dreamweaver or another editor, the XML updated information does appear like a single line without any breaks like:

 

<user><name>Ed Doe</name><age>34</age></user>

 

Which is really going to get really messy when more data is updated since every entry as new user just extends the long single line.

How could I add this indent spaces using PHP? Is there a way like:

$myItem ->addChild("age", "34", <br/>);

 

Hope anyone could give me a hand on this one.

Greetings and thanks in advance.

Edited by djburner
Link to comment
https://forums.phpfreaks.com/topic/272340-indent-addchild-element-in-xml/
Share on other sites

wow, I had this prob for eons!! for some time I kept working on my project, but eventually this "single line" issue bugged me.

his is a function I made to correct it. :-) enjoy

 

@filename must be full path (or relative), and $xml is the $xml data

 

modify as needed

 

function saveXMLFile($filename, $xml)
{
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->formatOutput = true;
$fh = fopen($filename,'w');
fwrite($fh,$dom->saveXML());
fclose($fh);
}

 

YOUR CODE can be changed as such:

 

 

$xml = simplexml_load_file("myXml.xml");

$sxe = new SimpleXMLElement($xml->asXML());

$user = $sxe->addChild("user");

$user->addChild("name", "Ed Doe");

$user->addChild("age", "34");

// $sxe->asXML("myXml.xml"); dont need this line

function saveXMLFile($xml, $sxe);

  • 2 weeks later...

@Cainj Hi man, thanks for your snippet, it does look preety good, but I have some problems with it.

 

My code looks like this:

<?php
$xml = simplexml_load_file("menu.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$disenoItem = $sxe->addChild("disenoItem");
$disenoItem->addChild("name", "Ed Doe");
$disenoItem->addChild("age", "30");
saveXMLFile($xml, $sxe);



function saveXMLFile($filename, $xml)
{
 $dom = dom_import_simplexml($xml)->ownerDocument;
 $dom->formatOutput = true;
 $fh = fopen($filename,'w');
 fwrite($fh,$dom->saveXML());
 fclose($fh);
}

?>

 

What am I doing wrong, actually the FTP trows this error: TypeError: items[x][0] is undefined[]

Thanks if you can check how I used your snippet in order to spot out the problem.

 

I only delete the world "function" from your snippet right here:

// $sxe->asXML("myXml.xml"); dont need this line
function saveXMLFile($xml, $sxe);
//I changed the above sentence into this:
saveXMLFile($xml, $sxe);
//Cause I think It was an error in your snippet, am I right?

 

Greetings and thanks in advance for any futher help on this issue.

djburner, look at the arguments you are passing in to the function and how the function uses them. In particular, the $filename argument, which should be a string containing the path to the file that should be written.

 

Also, there is no need to use fopen() and friends to write the file. Instead use $dom->save($filename).

Hi @Badger, I've change the snippet as I think you told me to do, but now is not doing anything. I know how to work with Javascript pretty well, but in PHP I'm just a total rookie, sorry if I do something really off the line.

<?php
$xml = simplexml_load_file("menu.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$disenoItem = $sxe->addChild("disenoItem");
$disenoItem->addChild("name", "Ed Doe");
$disenoItem->addChild("age", "30");
saveXMLFile($xml, $sxe);

function saveXMLFile($filename, $xml)
{
 $dom = dom_import_simplexml($xml)->ownerDocument;
 $dom->formatOutput = true;
 $dom->save(menu.xml); // I used both (menu.xml) as well ($filename) but neither seems to work
}

?>

 

Hope you can see what is wrong in this approach.

Thank you fro your time and help guys.

Greetings.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.