//A Document Containing a Single Person Node
/*
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE folks [
<!ELEMENT folks (person)*>
<!ELEMENT person (name, phone)>
<!ELEMENT name (#PCDATA | bold)*>
<!ELEMENT phone (#PCDATA)>
]>
<folks>
<person>
<name>Frank Fangston</name>
<phone>555-3247</phone>
</person>
</folks>
*/
public void addFragment(Document doc) {
Element person;
Element root = doc.getDocumentElement();
DocumentFragment fragment = doc.createDocumentFragment();
person = makePersonNode(doc,"Fred","555-4927");
fragment.appendChild(person);
person = makePersonNode(doc,"Sam","555-9832");
fragment.appendChild(person);
root.appendChild(fragment);
}
private Element makePersonNode(Document doc,String name,String phone) {
Element nameNode = doc.createElement("name");
Text nametextNode = doc.createTextNode(name);
nameNode.appendChild(nametextNode);
Element phoneNode = doc.createElement("phone");
Text phonetextNode = doc.createTextNode(phone);
phoneNode.appendChild(phonetextNode);
Element personNode = doc.createElement("person");
personNode.appendChild(nameNode);
personNode.appendChild(phoneNode);
return(personNode);
}
|