- 1). Declare the XML header in your PHP script. In your text editor, create a string called "$header" and enter the following PHP code:
$header = "<?xml version="1.0" encoding="ISO-8859-1"?>";
This is the XML declaration and defines the version of XML and the encoding being used. - 2). Create a root node. If you had a string in PHP called "rootnode," convert it to XML by simply putting angled brackets around its value and creating an open and close tag. The root node is the main part of the "tree" structure. All other nodes are said to be "children" of this node. In your text editor, enter the following:
$rootnode = "<root>";
$endroot = "</root>";
This will set variables to open and close the root node. - 3). Create a child node. The child node will hold information. In your text editor, enter the following code:
$childnode = "<childnode>Information to be stored</childnode>";
This code creates a child node called "childnode." The text between the two childnode tags is the information we are storing or sending. - 4). Output the information. Since XML is stored as a tree structure, first output the root node and then the children. In your editor, enter the following PHP code:
echo $header;
echo $rootnode;
echo $childnode;
echo $endroot;
SHARE