Jump to content

[SOLVED] PHP and XML question


corillo181

Recommended Posts

alright so i got a xml file that has the fallowing.

<?xml version="1.0" encoding="utf-8"?>
<artists>

</artists>

 

than i have a php code that add artists to this file. it looks like this.

<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/include/db.php');

$dom = new DomDocument();
$dom->load("test.xml"); 

$xp = new domxpath($dom);

$dom->formatOutput = true;

$item =  $dom->createElement("artist");
$title = $dom->createElement("name");
$link =  $dom->createElement("image");

$titletext = $dom->createTextNode("JUly el champ");
$linkpath = $dom->createTextNode("filepathtoimage.jpg");

$title->appendChild($titletext);
$link->appendChild($linkpath);

$item->appendChild($title);
$item->appendChild($link);

$item->setAttribute("id", "2");

$dom->documentElement->appendChild($item);

$dom->save('test.xml'); 

?>

 

there is no problem with the code the problem is with the xml format.

 

i want it to look

<?xml version="1.0"?>
<artists>
<artist id="2">
  <name>JUly el champ</name>
  <image>filepathtoimage.jpg</image>
</artist>
</artists>

 

but it looks like this

 

<?xml version="1.0"?>
<artists>
<artist id="2"><name>JUly el champ</name><image>filepathtoimage.jpg</image></artist></artists>

Link to comment
https://forums.phpfreaks.com/topic/67785-solved-php-and-xml-question/
Share on other sites

Does it really matter? It's just whitespace. If you want it like that, then you can use tidy though.

 

uh output \n after every dom object?  that should make the newlines.. but as far as "name" im not sure.

You do apparently not understand the code he posted.

well i guess I'll leave it as it is, i was searching and a lot of people have the same issue, bt every one says the same that there is no need for the format when you can view it on a browser that gives you the right format.

 

plus

 

$dom->formatOutput = yes;

 

only works if you create the file from scratch meaning you can't add nothing else to it.

oh so danielo if it was oyu you wouldn't pay mind to the format?

 

Not really, as long as it's valid syntax, but if you can do what corillo181 says (I didn't bother to check), then you might as well do so.

 

I just realized that I talked to you (corillo181) about you in third person... ::)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.