InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Generating XML as a String in PHP
PHP

Generating XML as a String in PHP

InfinityCoder December 6, 2016

You want to generate XML. For instance, you want to provide an XML version of your data for another program to parse.

Loop through your data and print it out surrounded by the correct XML tags:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
header('Content-Type: text/xml');
print '<?xml version="1.0"?>' . "\n";
print "<shows>\n";
 
$shows = array(array('name' => 'Modern Family',
                     'channel' => 'ABC',
                     'start' => '9:00 PM',
                     'duration' => '30'),
               array('name' => 'Law & Order: SVU',
                     'channel' => 'NBC',
                     'start' => '9:00 PM',
                     'duration' => '60'));
foreach ($shows as $show) {
   print " <show>\n";
   foreach($show as $tag => $data) {
       print " <$tag>" . htmlspecialchars($data) . "</$tag>\n";
   }
   print " </show>\n";
}
 
print "</shows>\n";

Printing out XML manually mostly involves lots of foreach loops as you iterate through arrays. However, there are a few tricky details.

First, you need to call header() to set the correct Content-Type header for the document. Because you’re sending XML instead of HTML, it should be text/xml.
Next, depending on your settings for the short_open_tag configuration directive, trying to print the XML declaration may accidentally turn on PHP processing.

Because the <? of <?xml version=”1.0″?> is the short PHP open tag, to print the declaration to the browser you need to either disable the directive or print the line from within PHP.

We do the latter in the Solution. Last, entities must be escaped. For example, the & in the show Law & Order needs to be &amp;. Call htmlspecialchars() to escape your data.
The output from the example in the Solution is shown in Example 12-1.
Example 12-1. Tonight’s TV listings

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0"?>
<shows>
    <show>
        <name>Modern Family</name>
        <channel>ABC</channel>
        <start>9:00 PM</start>
        <duration>30</duration>
    </show>
    <show>
        <name>Law &amp; Order: SVU</name>
        <channel>NBC</channel>
        <start>9:00 PM</start>
        <duration>60</duration>
     </show>
</shows>

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Defining Object Stringification in PHP
You want to control how PHP displays an object when …

Defining Object Stringification in PHP

Initializing an Array to a Range of Integers in PHP
You want to assign a series of consecutive integers to …

Initializing an Array to a Range of Integers in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap