Java Reference
In-Depth Information
Directory "Sketcher reading and writing XML"
Calling this method with a reference to the
Document
object that represents the sketch as an argument adds
a
<line>
child node corresponding to the
Element.Line
object. To complete this you must add the
cre-
ateEndpointElement()
method to the
Element.Line
class:
// Create XML element for the end point of a line
private org.w3c.dom.Element createEndpointElement(Document doc) {
return createPointTypeElement(doc, "endpoint",
String.valueOf(line.x2), String.valueOf(line.y2));
}
Directory "Sketcher reading and writing XML"
This method creates an XML
<endpoint>
element by calling the
createPointTypeElement()
method
that
Line
inherits from the base class.
Adding a Rectangle Node
Next you can add the method to the
Rectangle
class that creates a
<rectangle>
child element in the
Docu-
ment
object :
// Create an XML element for a rectangle
public void addElementNode(Document doc) {
org.w3c.dom.Element rectElement = doc.createElement("rectangle");
// Create the width & height attributes and attach them to the node
Attr attr = doc.createAttribute("width");
attr.setValue(String.valueOf(rectangle.width));
rectElement.setAttributeNode(attr);
attr = doc.createAttribute("height");
attr.setValue(String.valueOf(rectangle.height));
rectElement.setAttributeNode(attr);
// Create the angle attribute and attach it to the <rectangle> node
attr = doc.createAttribute("angle");
attr.setValue(String.valueOf(angle));
rectElement.setAttributeNode(attr);
// Append the <color>, <position>, and <bounds> nodes as children
rectElement.appendChild(createColorElement(doc));
rectElement.appendChild(createPositionElement(doc));
rectElement.appendChild(createBoundsElement(doc));