Home > Articles > Web Services > XML

XML Reference Guide

📄 Contents

  1. XML Reference Guide
  2. Overview
  3. What Is XML?
  4. Informit Articles and Sample Chapters
  5. Books and e-Books
  6. Official Documentation
  7. Table of Contents
  8. The Document Object Model
  9. Informit Articles and Sample Chapters
  10. Books and e-Books
  11. Official Documentation
  12. DOM and Java
  13. Informit Articles and Sample Chapters
  14. Books and e-Books
  15. Implementations
  16. DOM and JavaScript
  17. Using a Repeater
  18. Repeaters and XML
  19. Repeater Resources
  20. DOM and .NET
  21. Informit Articles and Sample Chapters
  22. Books and e-Books
  23. Documentation and Downloads
  24. DOM and C++
  25. DOM and C++ Resources
  26. DOM and Perl
  27. DOM and Perl Resources
  28. DOM and PHP
  29. DOM and PHP Resources
  30. DOM Level 3
  31. DOM Level 3 Core
  32. DOM Level 3 Load and Save
  33. DOM Level 3 XPath
  34. DOM Level 3 Validation
  35. Informit Articles and Sample Chapters
  36. Books and e-Books
  37. Documentation and Implementations
  38. The Simple API for XML (SAX)
  39. Informit Articles and Sample Chapters
  40. Books and e-Books
  41. Official Documentation
  42. SAX and Java
  43. Informit Articles and Sample Chapters
  44. Books and e-Books
  45. SAX and .NET
  46. Informit Articles and Sample Chapters
  47. SAX and Perl
  48. SAX and Perl Resources
  49. SAX and PHP
  50. SAX and PHP Resources
  51. Validation
  52. Informit Articles and Sample Chapters
  53. Books and e-Books
  54. Official Documentation
  55. Document Type Definitions (DTDs)
  56. Informit Articles and Sample Chapters
  57. Books and e-Books
  58. Official Documentation
  59. XML Schemas
  60. Informit Articles and Sample Chapters
  61. Books and e-Books
  62. Official Documentation
  63. RELAX NG
  64. Informit Articles and Sample Chapters
  65. Books and e-Books
  66. Official Documentation
  67. Schematron
  68. Official Documentation and Implementations
  69. Validation in Applications
  70. Informit Articles and Sample Chapters
  71. Books and e-Books
  72. XSL Transformations (XSLT)
  73. Informit Articles and Sample Chapters
  74. Books and e-Books
  75. Official Documentation
  76. XSLT in Java
  77. Java in XSLT Resources
  78. XSLT and RSS in .NET
  79. XSLT and RSS in .NET Resources
  80. XSL-FO
  81. Informit Articles and Sample Chapters
  82. Books and e-Books
  83. Official Documentation
  84. XPath
  85. Informit Articles and Sample Chapters
  86. Books and e-Books
  87. Official Documentation
  88. XML Base
  89. Informit Articles and Sample Chapters
  90. Official Documentation
  91. XHTML
  92. Informit Articles and Sample Chapters
  93. Books and e-Books
  94. Official Documentation
  95. XHTML 2.0
  96. Documentation
  97. Cascading Style Sheets
  98. Informit Articles and Sample Chapters
  99. Books and e-Books
  100. Official Documentation
  101. XUL
  102. XUL References
  103. XML Events
  104. XML Events Resources
  105. XML Data Binding
  106. Informit Articles and Sample Chapters
  107. Books and e-Books
  108. Specifications
  109. Implementations
  110. XML and Databases
  111. Informit Articles and Sample Chapters
  112. Books and e-Books
  113. Online Resources
  114. Official Documentation
  115. SQL Server and FOR XML
  116. Informit Articles and Sample Chapters
  117. Books and e-Books
  118. Documentation and Implementations
  119. Service Oriented Architecture
  120. Web Services
  121. Informit Articles and Sample Chapters
  122. Books and e-Books
  123. Official Documentation
  124. Creating a Perl Web Service Client
  125. SOAP::Lite
  126. Amazon Web Services
  127. Creating the Movable Type Plug-in
  128. Perl, Amazon, and Movable Type Resources
  129. Apache Axis2
  130. REST
  131. REST Resources
  132. SOAP
  133. Informit Articles and Sample Chapters
  134. Books and e-Books
  135. Official Documentation
  136. SOAP and Java
  137. Informit Articles and Sample Chapters
  138. Books and e-Books
  139. Official Documentation
  140. WSDL
  141. Informit Articles and Sample Chapters
  142. Books and e-Books
  143. Official Documentation
  144. UDDI
  145. UDDI Resources
  146. XML-RPC
  147. XML-RPC in PHP
  148. Informit Articles and Sample Chapters
  149. Books and e-Books
  150. Official Documentation
  151. Ajax
  152. Asynchronous Javascript
  153. Client-side XSLT
  154. SAJAX and PHP
  155. Ajax Resources
  156. JSON
  157. Ruby on Rails
  158. Creating Objects
  159. Ruby Basics: Arrays and Other Sundry Bits
  160. Ruby Basics: Iterators and Persistence
  161. Starting on the Rails
  162. Rails and Databases
  163. Rails: Ajax and Partials
  164. Rails Resources
  165. Web Services Security
  166. Web Services Security Resources
  167. SAML
  168. Informit Articles and Sample Chapters
  169. Books and e-Books
  170. Specification and Implementation
  171. XML Digital Signatures
  172. XML Digital Signatures Resources
  173. XML Key Management Services
  174. Resources for XML Key Management Services
  175. Internationalization
  176. Resources
  177. Grid Computing
  178. Grid Resources
  179. Web Services Resource Framework
  180. Web Services Resource Framework Resources
  181. WS-Addressing
  182. WS-Addressing Resources
  183. WS-Notifications
  184. New Languages: XML in Use
  185. Informit Articles and Sample Chapters
  186. Books and e-Books
  187. Official Documentation
  188. Google Web Toolkit
  189. GWT Basic Interactivity
  190. Google Sitemaps
  191. Google Sitemaps Resources
  192. Accessibility
  193. Web Accessibility
  194. XML Accessibility
  195. Accessibility Resources
  196. The Semantic Web
  197. Defining a New Ontology
  198. OWL: Web Ontology Language
  199. Semantic Web Resources
  200. Google Base
  201. Microformats
  202. StructuredBlogging
  203. Live Clipboard
  204. WML
  205. XHTML-MP
  206. WML Resources
  207. Google Web Services
  208. Google Web Services API
  209. Google Web Services Resources
  210. The Yahoo! Web Services Interface
  211. Yahoo! Web Services and PHP
  212. Yahoo! Web Services Resources
  213. eBay REST API
  214. WordML
  215. WordML Part 2: Lists
  216. WordML Part 3: Tables
  217. WordML Resources
  218. DocBook
  219. Articles
  220. Books and e-Books
  221. Official Documentation and Implementations
  222. XML Query
  223. Informit Articles and Sample Chapters
  224. Books and e-Books
  225. Official Documentation
  226. XForms
  227. Informit Articles and Sample Chapters
  228. Books and e-Books
  229. Official Documentation
  230. Resource Description Framework (RDF)
  231. Informit Articles and Sample Chapters
  232. Books and e-Books
  233. Official Documentation
  234. Topic Maps
  235. Informit Articles and Sample Chapters
  236. Books and e-Books
  237. Official Documentation, Implementations, and Other Resources
  238. Rich Site Summary (RSS)
  239. Informit Articles and Sample Chapters
  240. Books and e-Books
  241. Official Documentation
  242. Simple Sharing Extensions (SSE)
  243. Atom
  244. Podcasting
  245. Podcasting Resources
  246. Scalable Vector Graphics (SVG)
  247. Informit Articles and Sample Chapters
  248. Books and e-Books
  249. Official Documentation
  250. OPML
  251. OPML Resources
  252. Summary
  253. Projects
  254. JavaScript TimeTracker: JSON and PHP
  255. The Javascript Timetracker
  256. Refactoring to Javascript Objects
  257. Creating the Yahoo! Widget
  258. Web Mashup
  259. Google Maps
  260. Indeed Mashup
  261. Mashup Part 3: Putting It All Together
  262. Additional Resources
  263. Frequently Asked Questions About XML
  264. What's XML, and why should I use it?
  265. What's a well-formed document?
  266. What's the difference between XML and HTML?
  267. What's the difference between HTML and XHTML?
  268. Can I use XML in a browser?
  269. Should I use elements or attributes for my document?
  270. What's a namespace?
  271. Where can I get an XML parser?
  272. What's the difference between a well-formed document and a valid document?
  273. What's a validating parser?
  274. Should I use DOM or SAX for my application?
  275. How can I stop a SAX parser before it has parsed the entire document?
  276. 2005 Predictions
  277. 2006 Predictions
  278. Nick's Book Picks

When you have a lot of things going on in your life, it's easy to wonder: where does the time go? I've tried various time tracking applications, but I need something that's not just simple, but also fast. It has to be available all the time (without getting in my way) and I have to be able to click it and move on rather than filling out forms.

So last week's entry on JavaScript Object Notation (JSON) got me thinking: had technology finally advanced to the point where I could make a time tracker I would actually use? I think the answer is "yes", and I'm willing to spend a couple of entries proving it.

Here's what we're going to do: taking the concept of JSON one step further, we're going to take advantage of a fact even many seasoned web developers don't realize: JavaScript is a real Object Oriented language. Oh, sure, you knew you could use predefined objects such as document and even Image, but did you know you can create your own classes and objects, complete with methods? (Well, where did you think those JavaScript libraries came from?) In other words, you can write a real, honest-to-goodness application for your browser.

And that's just what we're going to do.

Then we're going to take it one step further. JavaScript is now available outside the browser, in environments such as OpenLaszlo and Yahoo! Widgets (formerly Konfabulator). We're going to take our object-oriented JavaScript application and turn it into a widget, increasing our graphical options and providing "always on" capability.

Overall, the application is pretty straightforward: we have a list of projects that are retrieved over the Web via JSON. They're displayed in the window, and when you click on one, it starts accruing time. To change projects, just click the new project. Time data is then sent back to the server to save it.

Will start by creating the basic application, which will give you a good grounding in using existing objects (such as those you receive via JSON). It will also give you a chance to create a simple "Web services interface" for your application, as we'll be sending information back and forth to and from PHP pages. We'll then add functionality and refactor the application to use custom classes and objects, and finally use the application as the basis for a widget.

We'll start by creating the basic service interface.

The skeleton application

The first step is to create any necessary database tables.

The actual database structure is pretty straightforward. Eventually, I would expand this to work for multiple users, but for now, all we need is a table to track the actual projects. I'm using MySQL for this project, but you can use whatever you like. (Change the code accordingly, of course.) Create the projects table:

CREATE TABLE `projects` (
  `id` int(11) NOT NULL auto_increment,
  `project_name` varchar(50) NOT NULL default '',
  `total_time` int(11) NOT NULL default '0',
  `time_this_period` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

As in our previous example, we are tracking the name of the project, total time spent on it, and the time spent during the current period. For example, a system might work on the basis of a weekly timesheet, but track the total time spent on a project over several weeks.

You'll also want to create a couple of projects to start with:

INSERT INTO `projects` VALUES (1, 'InformIT column', 0, 0);
INSERT INTO `projects` VALUES (3, 'Web Service Proposals', 0, 0);
INSERT INTO `projects` VALUES (4, 'Personal', 0, 0);
INSERT INTO `projects` VALUES (5, 'Miscellaneous', 0, 0);
INSERT INTO `projects` VALUES (6, 'Cleaning desk', 0, 0);

The contents of these projects is, of course, up to you. Ultimately, we will make it possible to add new projects through the interface, but for now you'll have to do it using SQL.

Once we have the database settled, the next step is to retrieve the information from it and turn it into an object in JSON form. We'll start by creating a PHP page called timesheet.php that pulls the information out of the database:

<?php

$link = mysql_connect('localhost', 'myuser', 'mypassword')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('workflow') or die('Could not select database');

$query = 'SELECT * FROM projects';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "<br />";
   echo $line["id"];
   echo "<br />";
   echo $line["project_name"];
   echo "<br />";
   echo $line["total_time"];
   echo "<br />";
   echo $line["time_this_period"];
}
?>

This is standard PHP database code; we make a connection to the database, execute a query, and loop through each of the results, extracting the columns from the associative array returned by mySQL_fetch_array.

Ultimately, we want to send the data to the browser as an object using JSON, so the next step is to convert it to PHP object.

The desired object structure is an object that has two members: the user (hardcoded for now) and an array of "project" objects, which have a member for each column in the database table. In other words, we want a JSON of:

{"user":"Nick","projects":[
{"id":"1","project_name":"InformIT type column","total_time":"0","time_this_period":"0"},
{"id":"3","project_name":"Web Service Proposals","total_time":"0","time_this_period":"0"},
{"id":"4","project_name":"Personal","total_time":"0","time_this_period":"0"},
{"id":"5","project_name":"Miscellaneous","total_time":"0","time_this_period":"0"},
{"id":"6","project_name":"Cleaning desk","total_time":"0","time_this_period":"0"}
]}

Start by creating the PHP objects:

<?php

$link = mysql_connect('localhost', 'myuser', 'mypassword')
   or die('Could not connect: ' . mysql_error());
mysql_select_db('workflow') or die('Could not select database');

$query = 'SELECT * FROM projects';
$projects = array();
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   $thisProject->id = $line["id"];
   $thisProject->project_name = $line["project_name"];
   $thisProject->total_time = $line["total_time"];
   $thisProject->time_this_period = $line["time_this_period"];
   array_push($projects, $thisProject);

}

$timeSheet->user = "Nick";
$timeSheet->projects = $projects;

?>

We're not adding any methods, using inheritance, and so on, so we don't need to create classes for these objects; we're solely interested in their structure and data. Once we have the objects, we can handle the actual conversion to JSON in one of two ways. The first is to build the text using "brute force", but why do that when JSON libraries are available for virtually any language? In fact, PHP has at least two. In a completely arbitrary choice, I'm using JSON-PHP, but you can also use PHP-JSON, or any other language/library combination listed at JSON.org.

To use it, add it to the page:

<?php

require_once("JSON.phps");

$link = mysql_connect('localhost', 'myuser', 'mypassword')
...
$timeSheet->projects = $projects;

$json = new Services_JSON();
$returnString = $json->encode($timeSheet);

echo "var timesheet = ".$returnString;

?>

If you point your browser to this page (http://www.nicholaschase.com/timesheet/timesheet.php, for example) you'll see output similar to:

var timesheet =
{"user":"Nick","projects":[{"id":"1","project_name":"InformIT
column","total_time":"0","time_this_period":"0"},{"id":"3","project_name":"Web
Service Proposals","total_time":"0","time_this_period":"0"},{"id":"4","project_name":"Personal","total_time":"0","time_this_period":"0"},{"id":"5","project_name":"Miscellaneous","total_time":"0","time_this_period":"0"},{"id":"6","project_name":"Cleaning
desk","total_time":"0","time_this_period":"0"}]}

Notice that this is a JavaScript command assigning the output to the timesheet variable. This is because we'll add it directly to the page using a script element. Make a new file called timesheet.html and add the following:

<html>

<head>
   <title>Timesheet Tracker</title>

   <script type="text/javascript" src="https://www.nicholaschase.com/jsondemo/timesheet.php"></script>

</head>
<body>

   <h1>TimeTracker</h1>

   <script type="text/javascript">
       document.write ("Timesheet user is "+
                              timesheet.user+"<br />");
       document.write ("The second project is "+
               timesheet.projects[1].project_name+".");
   </script>

</body>

</html>

Notice that we were able to access the object as an object from the JavaScript without having to do anything to it; because of the way the browser receives the data, it's interpreted as an object.

Once we have the object, we can access its members as we would with any JavaScript object, via the "dot" notation. Here we retrieve the user member and display it, and then retrieve the projects as an array, reference a single project object, and display its name.

We can also access these members directly to change their values:

<html>
<head>
   <title>Timesheet Tracker</title>

   <script type="text/javascript" src="https://www.nicholaschase.com/jsondemo/timesheet.php"></script>

</head>
<body>

   <h1>TimeTracker</h1>

   <script type="text/javascript">

       timesheet.projects[1].total_time = 120;

       document.write ("Timesheet user is "+
                              timesheet.user+"<br />");
       document.write ("The second project is "+
               timesheet.projects[1].project_name+".");
       document.write ("The second total time "+
                 timesheet.projects[1].total_time+".");
   </script>


</body>
</html>

Ultimately, will want to send the altered information back to the database, so let's look at how we create a JSON string and send it to the server:

<html>
<head>
   <title>Timesheet Tracker</title>

   <script type="text/javascript" src="https://www.nicholaschase.com/jsondemo/timesheet.php"></script>

   <script type="text/javascript" src="./json.js" ></script>

   <script type="text/javascript">

       function prepareSaveForm(){
           var returnObjectStr = JSON.stringify(timesheet);
           //saveForm.elements["timesheet"].value = URLencode(returnObjectStr);
           document.getElementById("timesheetValue").setAttribute("value", 
                                             URLencode(returnObjectStr));
       }

       function URLencode(sStr) {
           return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
       }

   </script>

</head>
<body>

   <h1>TimeTracker</h1>

   <form name="saveForm" id="saveForm" action="savetimesheet.php" 
                    onsubmit="prepareSaveForm();return true;" method="post">
       <input type="hidden" name="timesheet" id="timesheetValue" value="" />

       <input type="submit" value="Save Time Information" />
   </form>

</body>
</html>

Starting at the bottom of the pitch, in the body, we're using the easiest possible way of sending data from the browser to a server: a form. The form itself has only two elements. The first is the actual data being sent to the server, which will add in a moment, and the second is the submit button.

When the user submits the form, the browser executes the prepareSaveForm() function, and then submits the data to savetimesheet.php. We'll use the post method because we'll be making changes to the database, and that's a no-no with the get method.

The prepareSaveForm() method uses the JSON library as before, but this time we're using the stringify() method to turn a modified timesheet object into a JSON string.

In order to pass the string in a form, we need to URL encod it, which we'll do with the URLencode() function, courtesy of Real Gagnon.

Finally, we add the value to the form. You can either do this the traditional way, referencing the name of the form in the element, or you can use the DOM and access the element using getElementById().

Now that we're sending the data, we'd better retrieve it. Create another PHP page, savetimesheet.php, and add code to display the received information:

<?php

echo $_POST["timesheet"];
echo "<br />";
echo urldecode($_POST['timesheet']);

?>

Now we can use the library to convert the string back into an object:

<?php

  require_once("JSON.phps");
  $json = new Services_JSON();

  $timesheet = $json->decode(urldecode($_POST["timesheet"]));

?>

Finally, we save the data back to the database:

<?php
 require_once("JSON.phps");
 $json = new Services_JSON();

 $timesheet = $json->decode(urldecode($_POST["timesheet"]));

 $link = mysql_connect('localhost', 'myuser', 'mypassword')
   or die('Could not connect: ' . mysql_error()); 
 mysql_select_db('workflow') or die('Could not select database');


 $projects = $timesheet->projects;

 for ($i=0; $i<count($projects); $i++){
   $thisProject = $projects[$i];
   $id = $thisProject->id;
   $project_name = $thisProject->project_name;
   $total_time = $thisProject->total_time;
   $time_this_period = $thisProject->time_this_period;

   $sql = "update projects set total_time = ".$total_time.", time_this_period = ".$time_this_period." where id = ".$id;
   $result = mysql_query($sql) or die('Query failed: ' . mysql_error());
  if ($result){
    echo "Project '".$project_name."' updated.<br />";
  } else {
    echo "Nothing updated.";
  }

 }
?>

<html><head><title>Save time information</title></head>
<body>
<p>Time information saved.  Please <a href="timesheet.html">continue</a>.</p>

</body>
</html>

You should see the changes in a query of the database.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.