Java Reference
In-Depth Information
System.err.println("Invalid node in <curve>: " + aNode);
break;
}
}
}
Directory "Sketcher reading and writing XML"
The base class fields are set in the same way as for other objects. You create an empty GeneralPath ob-
ject before the loop that iterates over the child nodes and set the first segment as a move to the origin. For
each child node with the name "point" you add a LINE_TO segment using the values stored as attributes for
the <point> node. This re-creates the original curve representation.
Creating a Text Object from an XML Node
A <text> XML node has the angle and the maxAscent field values as attributes, and you extract those first.
Here's the code for the constructor:
// Create Text object from XML node
public Text(Node node) {
NamedNodeMap attrs = node.getAttributes();
angle = Double.valueOf(((Attr)(attrs.getNamedItem("angle"))).getValue());
maxAscent =
Integer.valueOf(((Attr)(attrs.getNamedItem("maxascent"))).getValue());
NodeList childNodes = node.getChildNodes();
Node aNode = null;
for(int i = 0 ; i < childNodes.getLength() ; ++i) {
aNode = childNodes.item(i);
switch(aNode.getNodeName()) {
case "position":
setPositionFromXML(aNode);
break;
case "color":
setColorFromXML(aNode);
break;
case "bounds":
setBoundsFromXML(aNode);
break;
case "font":
setFontFromXML(aNode);
break;
case "string":
text = aNode.getTextContent();
break;
default:
System.err.println("Invalid node in <text>: " + aNode);
break;
}
Search WWH ::




Custom Search