Java by API/org.w3c.dom/Node — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:15, 31 мая 2010
Содержание
- 1 Node: appendChild(Node newChild)
- 2 Node.CDATA_SECTION_NODE
- 3 Node.COMMENT_NODE
- 4 Node.DOCUMENT_FRAGMENT_NODE
- 5 Node.DOCUMENT_NODE
- 6 Node.DOCUMENT_TYPE_NODE
- 7 Node.ELEMENT_NODE
- 8 Node.ENTITY_NODE
- 9 Node.ENTITY_REFERENCE_NODE
- 10 Node: getChildNodes()
- 11 Node: getFirstChild()
- 12 Node: getNextSibling()
- 13 Node: getNodeName()
- 14 Node: getNodeType()
- 15 Node: getNodeValue()
- 16 Node: hasAttributes()
- 17 Node.NOTATION_NODE
- 18 Node.PROCESSING_INSTRUCTION_NODE
- 19 Node.TEXT_NODE
Node: appendChild(Node newChild)
/*
<?xml version="1.0" encoding="UTF-8"?>
<book id="javanut4">
<chapter>
<title>Chapter 1</title>
<para/>
</chapter>
<chapter>
<title>Chapter 2</title>
<para/>
</chapter>
<chapter>
<title>Chapter 3</title>
<para/>
</chapter>
</book>
*/
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class MainClass {
public static void main(String[] args) throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
Element book = document.createElement("book");
book.setAttribute("id", "javanut4");
document.appendChild(book);
for(int i = 1; i <= 3; i++) {
Element chapter = document.createElement("chapter");
Element title = document.createElement("title");
title.appendChild(document.createTextNode("Chapter " + i));
chapter.appendChild(title);
chapter.appendChild(document.createElement("para"));
book.appendChild(chapter);
}
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
}
}
Node.CDATA_SECTION_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.COMMENT_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.DOCUMENT_FRAGMENT_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.DOCUMENT_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.DOCUMENT_TYPE_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.ELEMENT_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.ENTITY_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.ENTITY_REFERENCE_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node: getChildNodes()
/*
DOCTYPE node:
<!ELEMENT address (buildingnumber,street,city,state,zip)>
<!ATTLIST address xmlns CDATA #IMPLIED>
<!ELEMENT buildingnumber (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
Document body contents are:
Node: address
Node Type: Element
Child Nodes of address are:
Node: buildingnumber
Node Type: Element
Child Nodes of buildingnumber are:
Node: #text
Node Type: Text
Content is: 29
Node: street
Node Type: Element
Child Nodes of street are:
Node: #text
Node Type: Text
Content is: South Street
Node: city
Node Type: Element
Child Nodes of city are:
Node: #text
Node Type: Text
Content is: Vancouver
Node: state
Node Type: Element
Child Nodes of state are:
Node: #text
Node Type: Text
Content is: BC
Node: zip
Node Type: Element
Child Nodes of zip are:
Node: #text
Node Type: Text
Content is: V6V 4U7
*/
import static org.w3c.dom.Node.ATTRIBUTE_NODE;
import static org.w3c.dom.Node.CDATA_SECTION_NODE;
import static org.w3c.dom.Node.ruMENT_NODE;
import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.ENTITY_NODE;
import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE;
import static org.w3c.dom.Node.NOTATION_NODE;
import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
import static org.w3c.dom.Node.TEXT_NODE;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class MainClass {
public static void main(String args[]) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true); // Set namespace aware
builderFactory.setValidating(true); // and validating parser feaures
builderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder(); // Create the parser
} catch(ParserConfigurationException e) {
e.printStackTrace();
}
Document xmlDoc = null;
try {
xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch(SAXException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
DocumentType doctype = xmlDoc.getDoctype();
if(doctype == null) {
System.out.println("DOCTYPE is null");
} else {
System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
}
System.out.println("\nDocument body contents are:");
listNodes(xmlDoc.getDocumentElement(),""); // Root element & children
}
static void listNodes(Node node, String indent) {
String nodeName = node.getNodeName();
System.out.println(indent+" Node: " + nodeName);
short type = node.getNodeType();
System.out.println(indent+" Node Type: " + nodeType(type));
if(type == TEXT_NODE){
System.out.println(indent+" Content is: "+((Text)node).getWholeText());
}
NodeList list = node.getChildNodes();
if(list.getLength() > 0) {
System.out.println(indent+" Child Nodes of "+nodeName+" are:");
for(int i = 0 ; i<list.getLength() ; i++) {
listNodes(list.item(i),indent+" ");
}
}
}
static String nodeType(short type) {
switch(type) {
case ELEMENT_NODE: return "Element";
case DOCUMENT_TYPE_NODE: return "Document type";
case ENTITY_NODE: return "Entity";
case ENTITY_REFERENCE_NODE: return "Entity reference";
case NOTATION_NODE: return "Notation";
case TEXT_NODE: return "Text";
case COMMENT_NODE: return "Comment";
case CDATA_SECTION_NODE: return "CDATA Section";
case ATTRIBUTE_NODE: return "Attribute";
case PROCESSING_INSTRUCTION_NODE: return "Attribute";
}
return "Unidentified";
}
static String xmlString ="<?xml version=\"1.0\"?>" +
" <!DOCTYPE address" +
" [" +
" <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
" <!ATTLIST address xmlns CDATA #IMPLIED>" +
" <!ELEMENT buildingnumber (#PCDATA)>" +
" <!ELEMENT street (#PCDATA)>" +
" <!ELEMENT city (#PCDATA)>" +
" <!ELEMENT state (#PCDATA)>" +
" <!ELEMENT zip (#PCDATA)>" +
" ]>" +
"" +
" <address>" +
" <buildingnumber> 29 </buildingnumber>" +
" <street> South Street</street>" +
" <city>Vancouver</city>" +
"" +
" <state>BC</state>" +
" <zip>V6V 4U7</zip>" +
" </address>";
}
Node: getFirstChild()
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class MainClass {
public static void main(String[] args) throws IOException, ParserConfigurationException,
org.xml.sax.SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true); // Convert CDATA to Text nodes
factory.setNamespaceAware(false); // No namespaces: this is default
factory.setValidating(false); // Don"t validate DTD: also default
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(new File(args[0]));
NodeList sections = document.getElementsByTagName("sect1");
int numSections = sections.getLength();
for (int i = 0; i < numSections; i++) {
Element section = (Element) sections.item(i); // A <sect1>
Node title = section.getFirstChild();
while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
title = title.getNextSibling();
if (title != null)
System.out.println(title.getFirstChild().getNodeValue());
}
}
}
Node: getNextSibling()
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class MainClass {
public static void main(String[] args) throws IOException, ParserConfigurationException,
org.xml.sax.SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true); // Convert CDATA to Text nodes
factory.setNamespaceAware(false); // No namespaces: this is default
factory.setValidating(false); // Don"t validate DTD: also default
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(new File(args[0]));
NodeList sections = document.getElementsByTagName("sect1");
int numSections = sections.getLength();
for (int i = 0; i < numSections; i++) {
Element section = (Element) sections.item(i); // A <sect1>
Node title = section.getFirstChild();
while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
title = title.getNextSibling();
if (title != null)
System.out.println(title.getFirstChild().getNodeValue());
}
}
}
Node: getNodeName()
/*
DOCTYPE node:
<!ELEMENT address (buildingnumber,street,city,state,zip)>
<!ATTLIST address xmlns CDATA #IMPLIED>
<!ELEMENT buildingnumber (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
Document body contents are:
Node: address
Node Type: Element
Child Nodes of address are:
Node: buildingnumber
Node Type: Element
Child Nodes of buildingnumber are:
Node: #text
Node Type: Text
Content is: 29
Node: street
Node Type: Element
Child Nodes of street are:
Node: #text
Node Type: Text
Content is: South Street
Node: city
Node Type: Element
Child Nodes of city are:
Node: #text
Node Type: Text
Content is: Vancouver
Node: state
Node Type: Element
Child Nodes of state are:
Node: #text
Node Type: Text
Content is: BC
Node: zip
Node Type: Element
Child Nodes of zip are:
Node: #text
Node Type: Text
Content is: V6V 4U7
*/
import static org.w3c.dom.Node.ATTRIBUTE_NODE;
import static org.w3c.dom.Node.CDATA_SECTION_NODE;
import static org.w3c.dom.Node.ruMENT_NODE;
import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.ENTITY_NODE;
import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE;
import static org.w3c.dom.Node.NOTATION_NODE;
import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
import static org.w3c.dom.Node.TEXT_NODE;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class MainClass {
public static void main(String args[]) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true); // Set namespace aware
builderFactory.setValidating(true); // and validating parser feaures
builderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder(); // Create the parser
} catch(ParserConfigurationException e) {
e.printStackTrace();
}
Document xmlDoc = null;
try {
xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch(SAXException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
DocumentType doctype = xmlDoc.getDoctype();
if(doctype == null) {
System.out.println("DOCTYPE is null");
} else {
System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
}
System.out.println("\nDocument body contents are:");
listNodes(xmlDoc.getDocumentElement(),""); // Root element & children
}
static void listNodes(Node node, String indent) {
String nodeName = node.getNodeName();
System.out.println(indent+" Node: " + nodeName);
short type = node.getNodeType();
System.out.println(indent+" Node Type: " + nodeType(type));
if(type == TEXT_NODE){
System.out.println(indent+" Content is: "+((Text)node).getWholeText());
}
NodeList list = node.getChildNodes();
if(list.getLength() > 0) {
System.out.println(indent+" Child Nodes of "+nodeName+" are:");
for(int i = 0 ; i<list.getLength() ; i++) {
listNodes(list.item(i),indent+" ");
}
}
}
static String nodeType(short type) {
switch(type) {
case ELEMENT_NODE: return "Element";
case DOCUMENT_TYPE_NODE: return "Document type";
case ENTITY_NODE: return "Entity";
case ENTITY_REFERENCE_NODE: return "Entity reference";
case NOTATION_NODE: return "Notation";
case TEXT_NODE: return "Text";
case COMMENT_NODE: return "Comment";
case CDATA_SECTION_NODE: return "CDATA Section";
case ATTRIBUTE_NODE: return "Attribute";
case PROCESSING_INSTRUCTION_NODE: return "Attribute";
}
return "Unidentified";
}
static String xmlString ="<?xml version=\"1.0\"?>" +
" <!DOCTYPE address" +
" [" +
" <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
" <!ATTLIST address xmlns CDATA #IMPLIED>" +
" <!ELEMENT buildingnumber (#PCDATA)>" +
" <!ELEMENT street (#PCDATA)>" +
" <!ELEMENT city (#PCDATA)>" +
" <!ELEMENT state (#PCDATA)>" +
" <!ELEMENT zip (#PCDATA)>" +
" ]>" +
"" +
" <address>" +
" <buildingnumber> 29 </buildingnumber>" +
" <street> South Street</street>" +
" <city>Vancouver</city>" +
"" +
" <state>BC</state>" +
" <zip>V6V 4U7</zip>" +
" </address>";
}
Node: getNodeType()
/*
DOCTYPE node:
<!ELEMENT address (buildingnumber,street,city,state,zip)>
<!ATTLIST address xmlns CDATA #IMPLIED>
<!ELEMENT buildingnumber (#PCDATA)>
<!ELEMENT street (#PCDATA)>
<!ELEMENT city (#PCDATA)>
<!ELEMENT state (#PCDATA)>
<!ELEMENT zip (#PCDATA)>
Document body contents are:
Node: address
Node Type: Element
Child Nodes of address are:
Node: buildingnumber
Node Type: Element
Child Nodes of buildingnumber are:
Node: #text
Node Type: Text
Content is: 29
Node: street
Node Type: Element
Child Nodes of street are:
Node: #text
Node Type: Text
Content is: South Street
Node: city
Node Type: Element
Child Nodes of city are:
Node: #text
Node Type: Text
Content is: Vancouver
Node: state
Node Type: Element
Child Nodes of state are:
Node: #text
Node Type: Text
Content is: BC
Node: zip
Node Type: Element
Child Nodes of zip are:
Node: #text
Node Type: Text
Content is: V6V 4U7
*/
import static org.w3c.dom.Node.ATTRIBUTE_NODE;
import static org.w3c.dom.Node.CDATA_SECTION_NODE;
import static org.w3c.dom.Node.ruMENT_NODE;
import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.ENTITY_NODE;
import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE;
import static org.w3c.dom.Node.NOTATION_NODE;
import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
import static org.w3c.dom.Node.TEXT_NODE;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class MainClass {
public static void main(String args[]) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true); // Set namespace aware
builderFactory.setValidating(true); // and validating parser feaures
builderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder(); // Create the parser
} catch(ParserConfigurationException e) {
e.printStackTrace();
}
Document xmlDoc = null;
try {
xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch(SAXException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
DocumentType doctype = xmlDoc.getDoctype();
if(doctype == null) {
System.out.println("DOCTYPE is null");
} else {
System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
}
System.out.println("\nDocument body contents are:");
listNodes(xmlDoc.getDocumentElement(),""); // Root element & children
}
static void listNodes(Node node, String indent) {
String nodeName = node.getNodeName();
System.out.println(indent+" Node: " + nodeName);
short type = node.getNodeType();
System.out.println(indent+" Node Type: " + nodeType(type));
if(type == TEXT_NODE){
System.out.println(indent+" Content is: "+((Text)node).getWholeText());
}
NodeList list = node.getChildNodes();
if(list.getLength() > 0) {
System.out.println(indent+" Child Nodes of "+nodeName+" are:");
for(int i = 0 ; i<list.getLength() ; i++) {
listNodes(list.item(i),indent+" ");
}
}
}
static String nodeType(short type) {
switch(type) {
case ELEMENT_NODE: return "Element";
case DOCUMENT_TYPE_NODE: return "Document type";
case ENTITY_NODE: return "Entity";
case ENTITY_REFERENCE_NODE: return "Entity reference";
case NOTATION_NODE: return "Notation";
case TEXT_NODE: return "Text";
case COMMENT_NODE: return "Comment";
case CDATA_SECTION_NODE: return "CDATA Section";
case ATTRIBUTE_NODE: return "Attribute";
case PROCESSING_INSTRUCTION_NODE: return "Attribute";
}
return "Unidentified";
}
static String xmlString ="<?xml version=\"1.0\"?>" +
" <!DOCTYPE address" +
" [" +
" <!ELEMENT address (buildingnumber, street, city, state, zip)>" +
" <!ATTLIST address xmlns CDATA #IMPLIED>" +
" <!ELEMENT buildingnumber (#PCDATA)>" +
" <!ELEMENT street (#PCDATA)>" +
" <!ELEMENT city (#PCDATA)>" +
" <!ELEMENT state (#PCDATA)>" +
" <!ELEMENT zip (#PCDATA)>" +
" ]>" +
"" +
" <address>" +
" <buildingnumber> 29 </buildingnumber>" +
" <street> South Street</street>" +
" <city>Vancouver</city>" +
"" +
" <state>BC</state>" +
" <zip>V6V 4U7</zip>" +
" </address>";
}
Node: getNodeValue()
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class MainClass {
public static void main(String[] args) throws IOException, ParserConfigurationException,
org.xml.sax.SAXException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setCoalescing(true); // Convert CDATA to Text nodes
factory.setNamespaceAware(false); // No namespaces: this is default
factory.setValidating(false); // Don"t validate DTD: also default
DocumentBuilder parser = factory.newDocumentBuilder();
Document document = parser.parse(new File(args[0]));
NodeList sections = document.getElementsByTagName("sect1");
int numSections = sections.getLength();
for (int i = 0; i < numSections; i++) {
Element section = (Element) sections.item(i); // A <sect1>
Node title = section.getFirstChild();
while (title != null && title.getNodeType() != Node.ELEMENT_NODE)
title = title.getNextSibling();
if (title != null)
System.out.println(title.getFirstChild().getNodeValue());
}
}
}
Node: hasAttributes()
/*
DOCTYPE node:
<!ELEMENT circle (position)>
<!ATTLIST circle radius CDATA #REQUIRED>
<!ELEMENT position EMPTY>
<!ATTLIST position x CDATA #REQUIRED>
<!ATTLIST position y CDATA #REQUIRED>
Document body contents are:
Node: circle
Node Type: Element
Element Attributes are:
radius = 15
Child Nodes of circle are:
Node: position
Node Type: Element
Element Attributes are:
x = 30
y = 50
*/
import static org.w3c.dom.Node.ATTRIBUTE_NODE;
import static org.w3c.dom.Node.CDATA_SECTION_NODE;
import static org.w3c.dom.Node.ruMENT_NODE;
import static org.w3c.dom.Node.DOCUMENT_TYPE_NODE;
import static org.w3c.dom.Node.ELEMENT_NODE;
import static org.w3c.dom.Node.ENTITY_NODE;
import static org.w3c.dom.Node.ENTITY_REFERENCE_NODE;
import static org.w3c.dom.Node.NOTATION_NODE;
import static org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE;
import static org.w3c.dom.Node.TEXT_NODE;
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentType;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class MainClass {
public static void main(String args[]) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setValidating(true); // and validating parser feaures
builderFactory.setIgnoringElementContentWhitespace(true);
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder(); // Create the parser
} catch(ParserConfigurationException e) {
e.printStackTrace();
}
Document xmlDoc = null;
try {
xmlDoc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch(SAXException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
DocumentType doctype = xmlDoc.getDoctype();
if(doctype == null) {
System.out.println("DOCTYPE is null");
} else {
System.out.println("DOCTYPE node:\n" + doctype.getInternalSubset());
}
System.out.println("\nDocument body contents are:");
listNodes(xmlDoc.getDocumentElement(),""); // Root element & children
}
static void listNodes(Node node, String indent) {
String nodeName = node.getNodeName();
System.out.println(indent+" Node: "+nodeName);
short type =node.getNodeType();
System.out.println(indent+" Node Type: " + nodeType(type));
if(type == TEXT_NODE){
System.out.println(indent+" Content is: "+((Text)node).getWholeText());
} else if(node.hasAttributes()) {
System.out.println(indent+" Element Attributes are:");
NamedNodeMap attrs = node.getAttributes();
for(int i = 0 ; i<attrs.getLength() ; i++) {
Attr attribute = (Attr)attrs.item(i);
System.out.println(indent+ " " + attribute.getName()+" = "+attribute.getValue());
}
}
NodeList list = node.getChildNodes();
if(list.getLength() > 0) {
System.out.println(indent+" Child Nodes of "+nodeName+" are:");
for(int i = 0 ; i<list.getLength() ; i++) {
listNodes(list.item(i),indent+" ");
}
}
}
static String nodeType(short type) {
switch(type) {
case ELEMENT_NODE: return "Element";
case DOCUMENT_TYPE_NODE: return "Document type";
case ENTITY_NODE: return "Entity";
case ENTITY_REFERENCE_NODE: return "Entity reference";
case NOTATION_NODE: return "Notation";
case TEXT_NODE: return "Text";
case COMMENT_NODE: return "Comment";
case CDATA_SECTION_NODE: return "CDATA Section";
case ATTRIBUTE_NODE: return "Attribute";
case PROCESSING_INSTRUCTION_NODE: return "Attribute";
}
return "Unidentified";
}
static String xmlString ="<?xml version=\"1.0\"?>" +
"<!DOCTYPE circle" +
"[" +
" <!ELEMENT circle (position)>" +
" <!ATTLIST circle" +
" radius CDATA #REQUIRED" +
" >" +
" <!ELEMENT position EMPTY>" +
" <!ATTLIST position" +
" x CDATA #REQUIRED" +
" y CDATA #REQUIRED" +
" >" +
"]>" +
"<circle radius=\"15\">" +
" <position x=\"30\" y=\"50\"/>" +
"</circle>";
}
Node.NOTATION_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.PROCESSING_INSTRUCTION_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}
Node.TEXT_NODE
import java.io.IOException;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
static public void main(String[] arg) {
boolean validate = false;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(validate);
dbf.setNamespaceAware(true);
dbf.setIgnoringElementContentWhitespace(true);
Document doc = null;
try {
DocumentBuilder builder = dbf.newDocumentBuilder();
doc = builder.parse(new InputSource(new StringReader(xmlString)));
} catch (SAXException e) {
System.exit(1);
} catch (ParserConfigurationException e) {
System.err.println(e);
System.exit(1);
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
TreeDumper td = new TreeDumper();
td.dump(doc);
}
static String xmlString = "<PHONEBOOK>" +
" <PERSON>" +
" <NAME >Joe Wang</NAME>" +
" <EMAIL property=\"working\">joe@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON> " +
"<NAME>Karol</NAME>" +
" <EMAIL>karol@yourserver.ru</EMAIL>" +
" <TELEPHONE>306-999-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" <PERSON>" +
" <NAME>Green</NAME>" +
" <EMAIL>green@yourserver.ru</EMAIL>" +
" <TELEPHONE>202-414-9999</TELEPHONE>" +
" <WEB>www.jexp.ru</WEB>" +
" </PERSON>" +
" </PHONEBOOK>";
}
class TreeDumper {
public void dump(Document doc) {
dumpLoop((Node)doc,"");
}
private void dumpLoop(Node node,String indent) {
switch(node.getNodeType()) {
case Node.CDATA_SECTION_NODE:
System.out.println(indent + "CDATA_SECTION_NODE");
break;
case Node.ruMENT_NODE:
System.out.println(indent + "COMMENT_NODE");
break;
case Node.DOCUMENT_FRAGMENT_NODE:
System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
break;
case Node.DOCUMENT_NODE:
System.out.println(indent + "DOCUMENT_NODE");
break;
case Node.DOCUMENT_TYPE_NODE:
System.out.println(indent + "DOCUMENT_TYPE_NODE");
break;
case Node.ELEMENT_NODE:
System.out.println(indent + "ELEMENT_NODE");
break;
case Node.ENTITY_NODE:
System.out.println(indent + "ENTITY_NODE");
break;
case Node.ENTITY_REFERENCE_NODE:
System.out.println(indent + "ENTITY_REFERENCE_NODE");
break;
case Node.NOTATION_NODE:
System.out.println(indent + "NOTATION_NODE");
break;
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
break;
case Node.TEXT_NODE:
System.out.println(indent + "TEXT_NODE");
break;
default:
System.out.println(indent + "Unknown node");
break;
}
NodeList list = node.getChildNodes();
for(int i=0; i<list.getLength(); i++)
dumpLoop(list.item(i),indent + " ");
}
}