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

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

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

Professional PHP4 XML

Programming XML Applications

With a very broad vision we may classify programming tasks using XML into two major categories:

Storing and retrieving XML is detailed in Chapter 13.

Up till now in this book we've covered the core standards for XML processing--XPath, SAX, DOM, and XSLT. In this chapter we are going to study different XML processing problems and study different solutions to these problems using the tools we've mastered in the previous chapters.

We need to process XML data from some source since it is useful to our application. We are going to study some very common problems that arise when processing an XML document. For each problem we'll discuss different options to solve the problem and the scenarios where each solution is better than the others.

The assumption here is that we are in the middle of a large application, and maintaining it is an important issue, hence, we'll be focusing on OOP solutions most of the time. We'll also observe some patterns of OOP that can be applied to programs that process XML.

Transforming XML

When it comes to transforming XML, we have an XML file or XML data source that we need to transform. This can be achieved in many different ways, for example:

Transforming implies reading XML, peeking at the content, and writing something that may or may not be XML.

Example

To illustrate different solutions, we'll work with a very simple example. Let's use the following XML file describing some applications coded by a software company:

<?xml version="1.0" ?>
<!-- This XML file is an example -->
<apps>
<application id="1">
  <name>Editor</name>
  <author>John</author>
  <bugs>
    <bug>
      <!-- This is one bug -->
      <desc>Foo</desc>
      <sev>2</sev>
    </bug>
    <bug>
      <desc>Bar</desc>
      <sev>3</sev>
    </bug>
  </bugs>
</application>
<application id="2">
  <name>Compiler</name>
  <author>Peter</author>
  <bugs>
    <bug>
    <desc>Foo Bar</desc>
    <sev>5</sev>
    </bug>
  </bugs>
</application>
</apps>

Our transformation task will be to transform all the <name> elements to a <name><b>text</b></name> format where all the text is uppercase. After transformation, the sample file would look like this:

<?xml version="1.0" ?>
<!-- This XML file is an example -->
<apps>
<application id="1">
  <name><b>EDITOR</b></name>
  <author>John</author>
  <bugs>
    <bug>
      <!-- This is one bug -->
      <desc>Foo</desc>
      <sev>2</sev>
    </bug>
    <bug>
      <desc>Bar</desc>
      <sev>3</sev>
    </bug>
  </bugs>
</application>
<application id="2">
  <name><b>COMPILER</b></name>
  <author>Peter</author>
  <bugs>
    <bug>
    <desc>Foo Bar</desc>
    <sev>5</sev>
    </bug>
  </bugs>
</application>
</apps>

We can transform XML documents using:


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

Created: August 12, 2002
Revised: August 12, 2002

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