Problem formatting XML using PHP

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 6412 Views - Last Post: 08 July 2011 - 01:31 AM Rate Topic: -----

#1 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Problem formatting XML using PHP

Posted 05 July 2011 - 11:24 PM

i'm working with PHP and XML, basically i have manage to make the XML file using PHP, my problem is the formatting of the code in my XML file

<?xml version="1.0" encoding="UTF-8"?>
<asset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="app-type-schema_3pp_v1.xsd">
<app externalId="external_unique_id_to_ContentProvider2"><service>S-Tu1lyk0HdR1</service><deployed>false</deployed><applanguage><value>ar</value></applanguage><language><value>ar</value></language><title lang="ar"><value></value></title><shortDescription lang="ar"><value>ar</value></shortDescription><longDescription lang="ar"><value></value></longDescription><manufacturer></manufacturer><author></author><pubdate></pubdate><version>06/2006</version><indicativePriceProposal></indicativePriceProposal><adultRating></adultRating></app></asset>



above code is ok...the only probs is when i open it in notepad or notepad++ its just in a single line without new lines...i have hard time indenting and making new line to see if i have the right inputs


my PHP code is something like this:

<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<asset xsi:noNamespaceSchemaLocation="app-type-schema_3pp_v1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</asset>
XML;
?>

<?php

if(isset($_POST['xml_add']))
{
	$val1 = $_POST['vLang'];
	$val2 = $_POST['vTitle'];
	$val3 = $_POST['vDesc1'];
	$val4 = $_POST['vDesc2'];
	$val5 = $_POST['vManu'];
	$val6 = $_POST['vAuthor'];
	$val7 = $_POST['vMonth']."/".$_POST['vYear'];
	$val8 = $_POST['vVersion'];
	$val9 = $_POST['vPrice'];
	$val10 = $_POST['vAdultRating'];
	
$sxe = new SimpleXMLElement($xmlstr);
	$app = $sxe->addChild('app','');
	$app->addAttribute('externalId','external_unique_id_to_ContentProvider2');
	$app->addChild('service','S-Tu1lyk0HdR1');
	$app->addChild('deployed','false');
	$applan = $app->addChild('applanguage','');
		$applan ->addChild('value',$val1);
	$lan = $app->addChild('language','');
		$lan ->addChild('value',$val1);
	$title = $app->addChild('title','');
		$title -> addAttribute('lang',$val1);
		$title -> addChild('value',$val2);
	$sdesc = $app->addChild('shortDescription','');
		$sdesc -> addAttribute('lang',$val1);
		$sdesc -> addChild('value',$val3);
	$ldesc = $app->addChild('longDescription','');
		$ldesc -> addAttribute('lang',$val1);
		$ldesc -> addChild('value',$val4);
	$app->addChild('manufacturer',$val5);
	$app->addChild('author',$val6);
	$app->addChild('pubdate',$val7);
	$app->addChild('version',$val8);
	$app->addChild('indicativePriceProposal',$val9);
	$app->addChild('adultRating',$val10);
	
$fp = @fopen($val2.'_asset.xml','w');
if(!$fp) {
    die('Error cannot create XML file');
}
fwrite($fp,$sxe->asXML()). "<br />";
fclose($fp);

}
?>






Hope to find solutions soon...thanks..cheers

This post has been edited by neverender2488: 05 July 2011 - 11:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem formatting XML using PHP

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Problem formatting XML using PHP

Posted 05 July 2011 - 11:33 PM

XML per se does not need newline charcters or a human-readable style, because it is intended to be read by machines to whom that issue doesn’t matter.

if you want to have line breaks in your XML, you have to insert them manually (using appendChild())
Was This Post Helpful? 1
  • +
  • -

#3 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Problem formatting XML using PHP

Posted 05 July 2011 - 11:53 PM

im using SimpleXMLElement which cannot identify the appendChild() thus im using addChild() instead... (T_T

..is there any other way?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,254
  • Joined: 08-June 10

Re: Problem formatting XML using PHP

Posted 05 July 2011 - 11:55 PM

how often do you need to read the XML yourself?
Was This Post Helpful? 0
  • +
  • -

#5 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Problem formatting XML using PHP

Posted 06 July 2011 - 12:02 AM

...if im not mistaken to your question...my php code will not read the xml...it will just write the values of my controls...only once...
Was This Post Helpful? 0
  • +
  • -

#6 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Problem formatting XML using PHP

Posted 06 July 2011 - 06:33 AM

appendChild is apart of DOMNode. Check out DOMDocument also.
Dormilich was just saying if you don't need to read it yourself then it doesn't matter how it's formatted because the machines can read it any how. It's like binary per say. A computer would write a long string of 0 and 1s without any spaces separating the bytes but it's still perfectly valid to them. However to use all we'll see is 010101000101010101010101010101 which isn't very readable to use.
Was This Post Helpful? 0
  • +
  • -

#7 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Problem formatting XML using PHP

Posted 06 July 2011 - 05:45 PM

ok ill check out DOMNod...but which is better to use DOMNod or SimpleXMLElement?

...acctually im a beginner with it, and i need to read it over to see if im doing right...
Was This Post Helpful? 0
  • +
  • -

#8 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: Problem formatting XML using PHP

Posted 06 July 2011 - 06:00 PM

SimpleXML is regarded as the "easiest" and "most friendly towards beginners," however DOMDocument has more power and strength to manipulate the DOM of XML and web pages.
Was This Post Helpful? 0
  • +
  • -

#9 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Problem formatting XML using PHP

Posted 06 July 2011 - 06:51 PM

...i get it now...whew..im really confused back then with this two methods...anyways my solution for my problem is that i wrote a php script that converts my simpleXML to Dom to beautify my code... it looks like this...i found this in some sample in the web

$xmlFile = $val2.'_asset.xml';
if( !file_exists($xmlFile) ) die('Missing file: ' . $xmlFile);
else
{
  $dom = new DOMDocument('1.0');
  $dom->preserveWhiteSpace = false;
  $dom->formatOutput = true;
  $dl = @$dom->load($xmlFile);
  if ( !$dl ) die('Error while parsing the document: ' . $xmlFile);
  $dom->save($xmlFile);
}


..my next problem is..i want to make the xml generated to be downloadable...i have some clue with headers() something like this

header("Content-disposition: attachment; filename=\"".$val2."_asset" . ".xml\"")


but it just print my entire code not my generated xml...any ideas?
Was This Post Helpful? 0
  • +
  • -

#10 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Problem formatting XML using PHP

Posted 06 July 2011 - 07:59 PM

Your $dom->save should some after you've sent the headers to the browser.
Sample Code
<?php
$dom = new DOMDocument("1.0");
$xml_file = 'config.xml';
$dom->loadXML(file_get_contents($xml_file)); //read our XML file
///...
//process XML
header("Content-type: application/xml"); //XML type
header("Content-disposition: attachment; filename=$xml_file");
header("Content-Transfer-Encoding: binary"); //transfer in binary mode
header("Content-length: " . filezise($xml_file)); //let the browser know how many bytes it's going to download
echo $dom->saveXML(); //note this comes after we've sent the headers telling the browser to download the following with an XML format
?>

Was This Post Helpful? 1
  • +
  • -

#11 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Problem formatting XML using PHP

Posted 06 July 2011 - 08:15 PM

<?php
$xmlFile = $val2.'_asset.xml';
if( !file_exists($xmlFile) ) die('Missing file: ' . $xmlFile);
else
{
  $dom = new DOMDocument('1.0');
  $dom->preserveWhiteSpace = false;
  $dom->formatOutput = true;
  $dl = @$dom->load($xmlFile);
  if ( !$dl ) die('Error while parsing the document: ' . $xmlFile);
  
  $dom->save($xmlFile);

}
?>

<?php
	$dom = new DOMDocument("1.0");
	$xml_file = $val2.'_asset.xml';
	$dom->loadXML(file_get_contents($xml_file)); //read our XML file
	///...
	//process XML
	header("Content-type: application/xml"); //XML type
	header("Content-disposition: attachment; filename=$xml_file");
	header("Content-Transfer-Encoding: binary"); //transfer in binary mode
	header("Content-length: " . filezise($xml_file)); //let the browser know how many bytes it's going to download
	echo $dom->saveXML(); //note this comes after we've sent the headers telling the browser to download the following with an XML format
?>




am i doing the right thing? because it just printing my html codes not the xml..
Was This Post Helpful? 0
  • +
  • -

#12 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Problem formatting XML using PHP

Posted 06 July 2011 - 08:38 PM

You've got two scripts in one. Mine was just an example not a copy, paste, run kinda script. Remove the @ operator from line #9. Handle your errors properly.

Did you read the comments in my code? They explain what you must do in order to get YOUR script to work.
Was This Post Helpful? 0
  • +
  • -

#13 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Problem formatting XML using PHP

Posted 07 July 2011 - 04:54 PM

<?php
$xmlFile = $val2.'_asset.xml';

if( !file_exists($xmlFile) ) die('Missing file: ' . $xmlFile.' Check if You have inputted all the fields.');
else
{
  $dom = new DOMDocument('1.0');
  $dom->preserveWhiteSpace = false;
  $dom->formatOutput = true;
  $dl = $dom->load($xmlFile);

  if ( !$dl ) die('Error while parsing the document: ' . $xmlFile);
   
 $dom->save($xmlFile);
 
	header("Content-type: application/xml");
	header("Content-disposition: attachment; filename=".$xmlFile);
	readfile($xmlFile);
}
?>


i can now display my xml code in my file, but still my html codes will be printed as well... how can i remove those html codes?
Was This Post Helpful? 0
  • +
  • -

#14 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Problem formatting XML using PHP

Posted 07 July 2011 - 05:33 PM

Read the comments in my code from line 23 - 27.
Was This Post Helpful? 0
  • +
  • -

#15 neverender2488  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 27-September 10

Re: Problem formatting XML using PHP

Posted 07 July 2011 - 05:46 PM

i have tried your code...but it done nothing, it just print html....so i put readfile($xmlFile); it print my xml, but it still include my html
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2