Friday, 3 July 2015

Set The Attribute Value Of The Php Dom

The PHP DOM extension allows PHP scripts to harvest data from other documents

The PHP scripting language allows web developers to write data to external documents. The developer codes the PHP script to place the data into an external document in a standardized format where it can be retrieved by other scripts. The PHP DOM extension represents one of the tools used by PHP developers to write this data. Specfically, the PHP DOM writes documents in the XML format. When the PHP DOM writes a piece of data to an XML, it can also set the attribute value for that data.



3. Establish a child element. Place the child element under the root element by once again using the createElement and appendChild methods. The sample code creates a child element named "cities" and places it under the "Texas" root element.

$city = $dom->createElement("City");


4. Write your data. Place your piece of data inside of the child element using the PHP DOM's createTextNode method. This code creates a piece of data named "Austin" and places the data inside of the city item.

$austin = $dom->createTextNode("Austin");


5. Assign the attribute to your data. You may set the attribute value using the createAttribute method. For example, this code assigns the attribute of population to the "city" element.

$population = $dom->createAttribute("Population");


6. Put a value inside of the attribute. You should assign a value to the attribute with the createTextNode method. In the example, the population value of 790,390 has been assigned to the "population" attribute.

$populationNumber = $dom->createTextNode("790390");


7. Save your data in XML format and print it out using the PHP echo command.

echo $dom->saveXML();

The sample XML document should appear as follows:


Tags: child element, piece data, your data, attribute value, code creates, createTextNode method