4 Replies - 2375 Views - Last Post: 28 November 2008 - 10:04 PM Rate Topic: -----

#1 sylviakoh24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-November 08

How to get rid of extra lines before and after SimpleXML structure

Post icon  Posted 28 November 2008 - 01:20 AM

Hi

I have the following SimpleXML code, to add a new child "STATUS" to this structure.
However, when the results are displayed in IE, there's 2 extra lines on top and below the XML structure. How do I get rid of them? Thanks
Sylvia

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<RESPONSE>
</RESPONSE>
XML;
?>

$sxe = new SimpleXMLElement($xmlstr); //create a new instance of XML

$sxe->STATUS = $GLOBALS["currentstatus"];
echo $sxe->asXML();

Is This A Good Question/Topic? 0
  • +

Replies To: How to get rid of extra lines before and after SimpleXML structure

#2 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: How to get rid of extra lines before and after SimpleXML structure

Posted 28 November 2008 - 01:52 AM

It's caused by the newlines after <<<XML and XML; Why do you want/need to use macro's for a simple string assignment?

$xmlstr = "<?xml version='1.0' standalone='yes'?><RESPONSE></RESPONSE>";



This way you do noy have newlines outside your <?php and ?> tags.
?>
Was This Post Helpful? 0
  • +
  • -

#3 sylviakoh24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-November 08

Re: How to get rid of extra lines before and after SimpleXML structure

Posted 28 November 2008 - 08:22 PM

View PostHary, on 28 Nov, 2008 - 12:52 AM, said:

It's caused by the newlines after <<<XML and XML; Why do you want/need to use macro's for a simple string assignment?

$xmlstr = "<?xml version='1.0' standalone='yes'?><RESPONSE></RESPONSE>";



This way you do noy have newlines outside your <?php and ?> tags.
?>

Was This Post Helpful? 0
  • +
  • -

#4 sylviakoh24  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 28-November 08

Re: How to get rid of extra lines before and after SimpleXML structure

Posted 28 November 2008 - 08:52 PM

Hi Hary

I am putting it in macro because it's accessible by all function in the PHP file and at the end, I will printing out the XML structure. Does this make sense? I will try out your suggestion.

Thank you so much for your help
Sylvia

View Postsylviakoh24, on 28 Nov, 2008 - 07:22 PM, said:

View PostHary, on 28 Nov, 2008 - 12:52 AM, said:

It's caused by the newlines after <<<XML and XML; Why do you want/need to use macro's for a simple string assignment?

$xmlstr = "<?xml version='1.0' standalone='yes'?><RESPONSE></RESPONSE>";



This way you do noy have newlines outside your <?php and ?> tags.
?>

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: How to get rid of extra lines before and after SimpleXML structure

Posted 28 November 2008 - 10:04 PM

Declare it as a constant in an include file:
define('XMLRESPONSE', "<?xml version='1.0' standalone='yes'?><RESPONSE></RESPONSE>");

and include this file where you need it.
You could even make it more object-oriented:
<?php
/*file: MyXml.php */
define('XMLRESPONSE', "<?xml version='1.0' standalone='yes'?><RESPONSE></RESPON\
SE>");
class MyXml
{
   public static function GetStatusResponse($status) {
      $sxe = new SimpleXMLElement(XMLRESPONSE);
      $sxe->STATUS = $status;
      return $sxe->asXML();
   }
};


And then call it:
include_once(MyXml.php);
echo MyXml::GetStatusResponse($_GLOBALS['currentstatus']);

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1