Jump to content

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

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.