Chapter 8: Working with Databases using PHP and XML


 Download CD Content

You can store data in databases or as eXtensible Markup Language (XML) documents. Hypertext Preprocessor (PHP) converts data stored in the databases into XML documents and vice versa. Applications on the Internet can retrieve and update the data stored in the form of XML documents.

This chapter explains how to store XML documents in a database using PHP. This chapter describes how to export data stored in databases into an XML document and how to import data from an XML document into a database using the Simple API for XML (SAX) approach. This chapter also describes how to convert an XML document into an HTML format.

Storing XML Documents

XML is a standard language to store information on the Internet in a structured manner. XML is a subset of Standard Generalized Markup Language (SGML) and consists of tags to represent data.

An XML document stores data in a tree structure. You can query the contents of the XML document using a semi-structured language, such as XSL Transformations (XSLT). You can use XML documents to create Web applications and exchange information on the Internet.

You can store XML documents in a database and retrieve them. You can store the XML documents in a database by mapping the tree structure of the XML document to the database schemas.

When you map the tree structure of the XML document to the database schema, you design the database schemas according to the Document Type Declaration (DTD) of the XML structure. DTD defines the rules that an XML document should follow. The advantages of using database schemas to store XML documents are:

  • You can create XML documents using relational tables, without depending on the DTD.

  • You can perform various query operations on the XML documents using index structures. Indexing is of two types, position-based and path-based. Position-based indexing manipulates the elements and attributes to perform a query operations based on their position within the document. Path -based indexing performs the query operation using the path of the elements and attributes in the tree structure.

The disadvantages of using database schemas to store XML documents are:

  • The database schemas cannot be designed efficiently in accordance with DTD. Relational databases and object-oriented databases cannot represent DTD to store XML documents efficiently .

  • Maintaining the consistency of the database schemas is not possible. You need to change the structure of the database schemas every time there is a change in the structure of the XML document.

Storing XML Documents in Databases

An XML document is represented in a tree structure, which contains various nodes, such as element node, attribute node, and text node. The element node contains a text label, and can contain child nodes, such as the attribute node and the text node. The attribute node contains the name and value of the attribute. An attribute node does not have any child nodes. The text node in an XML document contains character data and represents the value for the element.

The process of storing the XML documents involves decomposing the XML tree structure into relations, which the relational tables can reuse and access.

The database relations that you can use for storing the XML document include element, attribute, text, and path. A relation stores data corresponding to the node of the tree structure. For example, the attribute relation stores the data stored in the attribute node. The various relations and their database attributes are:

  • Element Relation: Stores data about the element node using database attributes, such as docID, pathID, index, and pos. The docID represents the document identifier, and pathID represents the path identifier. The index attribute represents the order among the sibling having the same path identifier. The pos attribute specifies the position of the element node with respect to the other nodes.

  • Attribute Relation: Stores data about the attribute nodes. The database attributes in the attribute relation are docID, pathID, Attvalue, and pos. The Attvalue attribute specifies the value of an attribute.

  • Text Relation: Stores data about the text nodes. The database attributes in the text relation are docID, pathID, value, and pos.

  • Path Relation: Stores data about simple paths. The database attributes in the path relation are docID, pathID, Attvalue, and pos.

You can use query languages such as XML Query Language (XQL) to perform data manipulation, such as joins, on the XML data.

Organizing XML Documents with Entities

An XML document consists of storage units called entities, which are a collection of text and markup tags. In an XML document, entities are used to store data. The advantages of organizing an XML document with entities are:

  • Ensures consistency in representing repeating text.

  • Ensures that every entity instance is exactly the same.

  • Limits repetitive typing of same information.




Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net