Accessing an XML document using DOM API objects
A description of the editor-specific implementations or extensions of DOM methods and objects, with examples.
The document is made up out of elements, attributes and other objects. These are represented in the Content Editor DOM API by different types of objects:
You can recognize these objects from other Document Object Model (DOM) structures. Content Editor implements the W3C DOM standard, and in addition provides some extra methods. For example the makeValid method can be used on transactions so that Content Editor will extend a node's structure until it is valid. Content Editor will add elements and attributes as needed according to the element's schema definition.
Example using selectSingleNode
var doc= Editor.getActiveDocument();
var element = doc.
selectSingleNode("//title[ancestor::section][1]");
For loop example
var children = node.
selectNodes("*");
for(i=0;i< children.getLength();i++)
{
var child = children.item(i);
}
Extended functionality example
<section> element should always have a <title>. This code creates and adds a new section prior to the second section in the document. Without the call to makeValid, Content Editor would revert the action and nothing would change in your document.. MakeValid inserts what's need to make the new structure valid. In this case, it adds the <title>, and adds an empty textNode to the title in order to make the new section valid. Remember, the makeValid method works on transactions so either exectute it as part of the execute method of a command (toolbar-button) where it's automatically in a transaction or wrap it in a transaction as in this example.
Editor.runInTransaction(function() {
var doc= Editor.getActiveDocument();
var section = doc.createElement("section");
var oldSection = doc.selectSingleNode('//section[2]');
var parent = oldSection.getParentNode();
//insert before oldSection
parent.insertBefore(section, oldSection);
//make valid according to the document's schema (node has to be in the document)
parent.makeValid();
});