XML and PHP Simplified: The DOM / Page 2 | WebReference

XML and PHP Simplified: The DOM / Page 2


[prev]

XML and PHP Simplified: The DOM [con't]

Next we look at the simplxml_load_string() function:

simplexml_load_string: takes a string as an argument; the string should be well-formed XML. Converts the string to an object.

To demonstrate let's take a look at the following code:

The above code does the following; first, it defines an XML formatted string:

Then it simply calls the simplexml_load_string() function to convert the string into an object and then prints its contents:

$obj = simplexml_load_string($str);
print_r($str); 

It shows the following results when executed:

How are you? [email protected] #hometamas@/goagoseb.com Have a nice day

The next function is the simple_import_dom():

simplexml_import_dom: takes a node from a DOM document and turns it into a simplexml node. Here's some sample code for this function:

When executing this code it returns the following:

simon //goagoseb

simplexml_element->asXML: returns a well-formed XML string from a simpleXML object. Lets take a look at the code:

Notice that certain elements and attributes are missing from the XML document. Now when we run the code we get:

simplexml_element->attributes: provides the attributes and values defined within a well-formed string of XML.

When executed:

age="32" employment="programmer"

simplexml_element->children: method provides the child elements of an element.

When executed:

As with the DOM, you can also change node values with simpleXML. To demonstrate, let's take our mailmessage XML string that we used earlier and change the subject from 'How are you' to 'Hey':

When you run this code, the XML string changes the subject, as visible from the source HTML:

So how does this work? Well first, we define the XML string that we want to use:

Now we load the xmlstring. Once loaded, we can use the document parts just as we would an array:

$xmlstr = simplexml_load_string ($str);

Because we know that mailmessage is a element in the document we can manipulate it as much as we want or in anyway we want. In this case we simply change the value of subject:

$xmlstr->mailmessage[0]->subject = 'Hey';

Then we show the contents of the string:

echo "
";
print_r($xmlstr);
echo "";

Now that we know how to manipulate a XML document through the DOM,SimpleXML and ordinary PHP functions, let's built a program that will use all of this knowledge and that we can later use as a reference point for any future projects that involve XML. The program is called XML Editor. It will, as the name suggests, enable us to create XML documents and manipulate various parts. So let's see what we will require for the program:

  • Provide a list of XML documents that is available. For this we need to store all our XML documents in one folder so that the program can easily fetch and store
  • We need to be able to create new well formed XML documents
  • We need to be able to change an element of an XML document

We have enough knowledge to implement these requirements by using either the DOM or the simplexml extensions.

The program

Create a new php document and save it as main.php. Add the following code:

What this code does is to create a list of all the files in the docs folder. This folder contains only XML files; these files will then be listed in the list/menu dropdown box. From here, the user will be able to select the XML file that he or she wants to work with. The code is very easy to understand. In the next article, we continue to build on this program.

Original: August 26, 2009


[prev]