Solved

XML indentation issue with simpleXML and DOM in PHP

Posted on 2011-03-07
6
1,324 Views
Last Modified: 2012-05-11
Hi there,

I've been working on an XML management script in PHP.  You can basically view the contents of an XML file, add items or delete items.  I'm as happy as a clam right now in all honesty.  There is one nagging problem i'm having though...any new items added to the file are displayed as one long line of markup and are not nicely indented like other areas of the file.  I found a fix on StackOverflow...you basically convert the simpleXML object to a DOM object, then set a value and save the file.  It's not working for me though.

Anyways, here's the XML file :
<advertisements>
	<item name="burger">
		<image>burger.jpg</image>
		<price>5.99</price>
		<metric>1 pound</metric>
		<description>Integer tellus nibh, volutpat id sodales vitae, facilisis vel dolor. Etiam accumsan orci quis risus aliquam vitae scelerisque nulla volutpat. Maecenas placerat purus in augue blandit id consequat tellus pellentesque. Pellentesque congue laoreet turpis at lobortis.</description>
	</item>
</advertisements>

Open in new window


..and here is the portion of my code which adds a new <item> element to the file :

$items = array();
		  $items [] = array(
		  'name' => trim($_POST['item_name']),
		  'image' => basename( $_FILES['uploadedfile']['name']),
		  'price' => trim($_POST['price']),
		  'metric' => trim($_POST['metric']),
		  'description' => trim($_POST['description'])
		  );
		  $xml = new SimpleXMLElement('XML/gt.xml', NULL, TRUE);
		  foreach( $items as $item )  {
			$item_xml = $xml->addChild('item');
			$item_xml->addAttribute("name", $item['name']);
			$item_xml->addChild('image', $item['image']);
			$item_xml->addChild('price', $item['price']);
			$item_xml->addChild('metric', $item['metric']);
			$item_xml->addChild('description', $item['description']);
		  }
		  
		  $dom = dom_import_simplexml($xml)->ownerDocument;
		  $dom->formatOutput = true;
		  $dom->save('XML/gt.xml');
		  header("Location: index.php");

Open in new window


The last 3 lines where I created use the $dom variable is the workaround I found on StackOverflow but the indentation is not taking hold for new items.  Any help would be much appreciated!
0
Comment
Question by:duder78
  • 4
  • 2
6 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35060627
Can you post the Stack's link ?
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 35060780
I've managed to get a little closer to what you want:

$items = array();
		  $items [] = array(
		  'name' => trim($_POST['item_name']),
		  'image' => basename( $_FILES['uploadedfile']['name']),
		  'price' => trim($_POST['price']),
		  'metric' => trim($_POST['metric']),
		  'description' => trim($_POST['description'])
		  );
		  $xml = new SimpleXMLElement('XML/gt.xml', NULL, TRUE);
		  foreach( $items as $item )  {
			$item_xml = $xml->addChild('item');
			$item_xml->addAttribute("name", $item['name']);
			$item_xml->addChild('image', $item['image']);
			$item_xml->addChild('price', $item['price']);
			$item_xml->addChild('metric', $item['metric']);
			$item_xml->addChild('description', $item['description']);
		  }
		  
		  $doc = new DOMDocument('1.0');
        $doc->preserveWhiteSpace = false;
        $doc->loadXML($xml->asXML());
        $doc->formatOutput = true;
        $doc->save('XML/gt.xml');

Open in new window

0
 

Author Comment

by:duder78
ID: 35060795
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35060867
I think the code #35060780  works.
0
 

Author Closing Comment

by:duder78
ID: 35061002
That's perfect man,  just what I needed.  Thank you very much!
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 35061071
Cheers !
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now