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
Share on other sites

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.

Link to comment
Share on other sites

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... ::)

Link to comment
Share on other sites

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.