WebReference.com - Part 4 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (1/5) | WebReference

WebReference.com - Part 4 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (1/5)

current pageTo page 2To page 3To page 4To page 5
[next]

Professional PHP4 XML, Chapter 10: Putting It Together

Writing XML

[The following is the conclusion of our series of excerpts from chapter 10 of the Wrox Press title, Professional PHP4 XML. Source code for the examples discussed can be downloaded at the Wrox Web site (free e-mail registration required).]

Writing XML is a problem where an XML file has to be generated from a non-XML source (or it will be a transformation). For example, we may have some data in a database, a file, or objects, and we have to write an XML document. What we do with the XML document later is beyond the scope of the problem. Following are some instances of this problem:

  • We have data in a relational database and want to produce an XML representation of the data
  • We have data in a plaintext file that describes news and want to produce an XML RSS file
  • We have some PHP objects and want to produce an XML file describing object properties
  • We have a file in a proprietary format and want to convert it to XML for processing

We won't cover in this problem parsing the non-XML data, which is usually harder than writing the XML document.

Example

In this example we have the following text file describing a to-do list for some people:

Jim Smith, Go to the library and pick up books
Sidney, Make 13 copies of memo no:19
Kelly, Distribute memo no:5 by email
Sidney, Arrange meeting with foo managers
Kelly, Write a draft of the technical document for project X
Jim Smith, Distribute books to team members
Sidney, Record book loans in HR software

Our task is to generate a representation such as the following:

<to-do>
  <person>
    <name>Jim Smith</name>
    <tasks>
      <task>Go to the library and pick up books</task>
      <task>Distribute books to team members</task>
    </tasks>
  </person>
  
  <person>
    <name>Sidney</name>
    <tasks>
      <task>Make 13 copies of memo no:19</task>
      <task>Arrange meeting with foo&foo managers</task>
      <task>Record book loans in HR software</task>
    </tasks>
  </person>
  
  <person>
    <name>Kelly</name>
    <tasks>
      <task>Distribute memo no:5 by email</task>
      <task>Write a draft of the technical document for project X</task>
    </tasks>
  </person>
</to-do>

We can use the following tools to write XML documents:


current pageTo page 2To page 3To page 4To page 5
[next]

Created: September 3, 2002
Revised: September 3, 2002

URL: https://webreference.com/programming/php/php4xml/chap10/4/