Java Reference
In-Depth Information
Adding a Text Node
A text node is a little different and involves quite a lot of code. As well as the usual <color> and
<position> child nodes, we also have to append a <font> node to define the font and a <string>
node. The <font> node has three attributes that define the font name, the font style, and the point size.
The <string> node has the text well as a <bounds> element that has two attributes defining the
width and height of the text. Here's the code:
public void addElementNode(Document doc) {
org.w3c.dom.Element textElement = doc.createElement("text");
// Create the angle attribute and attach it to the <text> node
Attr attr = doc.createAttribute("angle");
attr.setValue(String.valueOf(angle));
textElement.setAttributeNode(attr);
// Append the <color> and <position> nodes as children
textElement.appendChild(createColorElement(doc));
textElement.appendChild(createPositionElement(doc));
// Create and apppend the <font> node
org.w3c.dom.Element fontElement = doc.createElement("font");
attr = doc.createAttribute("fontname");
attr.setValue(font.getName());
fontElement.setAttributeNode(attr);
attr = doc.createAttribute("fontstyle");
String style = null;
int styleCode = font.getStyle();
if(styleCode == Font.PLAIN)
style = "plain";
else if(styleCode == Font.BOLD)
style = "bold";
else if(styleCode == Font.ITALIC)
style = "italic";
else if(styleCode == Font.ITALIC+Font.BOLD)
style = "bold-italic";
assert style != null;
attr.setValue(style);
fontElement.setAttributeNode(attr);
attr = doc.createAttribute("pointsize");
attr.setValue(String.valueOf(font.getSize()));
fontElement.setAttributeNode(attr);
textElement.appendChild(fontElement);
// Create the <string> node
org.w3c.dom.Element string = doc.createElement("string");
// Create the <bounds> node and its attributes
org.w3c.dom.Element bounds = doc.createElement("bounds");
attr = doc.createAttribute("width");
Search WWH ::




Custom Search