Script DOM extension
Document object model is W3C standard and recommendation
to access XML and HTML content
What is DOM
?The document object model is a universal standard interface for accessing the contents of XML or XML documents. The functions for dynamically building and modifying the structure of these scripted documents are also included.
DOM can be used from JavaScript programs embedded in web pages, or in XUL interfaces or any XML tools that support JavaScript.
DOM is part of Ajax and as such it is important for building modern websites and web applications. Processes XML files received by XMLHttpRequest and dynamically changes the content of web pages based on data from those files. You can use it to create an XML document from form data and send it to the server.
The DOM interface is part of the PHP programming language and can also be used from a Script program.
Installing and Using the DOM
There is nothing to install under PHP 5, the DOM package is part of the base language.
With Script, you just need to include the header file in the source .sol:
include "dom.sol"
Main classes and functions
DOM has classes for building and accessing the document structure (node, nodelist), as well as classes for reading and editing data stored in the document (element, text).
Therefore, the interface is made up of these classes and methods that are defined in the Script interface:
- DOMDocument
- getElementByID () Returns the item whose ID is specified.
- getElementsByTagName () Returns a list of items that use the tag type.
- DOMElement
- setAttribute () Edit or create a tag attribute .
- DOMNode
- DOMNodeList
- item (n) Returns node n of the node created by getElementsByTagName .
- DOMText
DOMDocument methods added to PHP that are not part of the specification:
- loadHTMLile () Loads a file into DOMDocument .
- save () Saves DOMDocument in XML or HTML file .
Simple demonstration
This is a very simple demo from the PHP manual and has been converted to Script:
DOMDocument doc = DOMDocument() DOMNode root = doc.createElement('book') root = doc.appendChild(root) DOMNode title = doc.createElement('title') title = root.appendChild(title) DOMText name = doc.createTextNode('This is the title') name = title.appendChild(name) print doc.saveXML()
The goal is to create and view an XML document.
- The doc document is created first.
- The root node is created and added as a child of the dock.
-The title node is then inserted as a root child.
- The node name element is inserted as a title tag data element.
- The document is displayed by the saveXML () method.
To save it as a file, assign a variable and save its contents to a file:
text x = doc.saveXML() file f = fopen("name.xml", "w") f.write(x) f.close()
Loading
- Download Demo Archive (dom.sol header enabled).
To create a demo, enter:
solp domdemo
Previously required: PHP 5 interpreter. Script-PHP compiler.