注明:其中某些注释部分为读写文件(与之对应的是仅仅操作字符串),或者是可替换方式,在JDOM和DOM4J中XPATH需要用到jaxen.jar
一、DOM操作方式
public static void getDoc() throws ParserConfigurationException, SAXException, IOException
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//Document doc = db.parse(new File("student.xml"));
String xml = "";//xml string
Document doc = db.parse(new InputSource(new StringReader(xml)));
Element element = doc.getDocumentElement();
parseDoc(element);
}
public static void parseDoc(Node element)
{
System.out.print("<");
System.out.print(element.getNodeName());
NamedNodeMap al = element.getAttributes();
for(int i = 0 ; i < al.getLength();i++)
{
Node node = al.item(i);
System.out.print(" ");
System.out.print(node.getNodeName());
System.out.print("='");
System.out.print(node.getNodeValue());
System.out.print("'");
}
System.out.print(">");
NodeList nl = element.getChildNodes();
for(int i = 0 ; i < nl.getLength();i++)
{
Node node = nl.item(i);
if(node.getNodeType() == Node.ELEMENT_NODE)
{
parseDoc(node);
}
else if(node.getNodeType() == Node.TEXT_NODE)
{
System.out.print(node.getNodeValue());
}
}
System.out.print("</");
System.out.print(element.getNodeName());
System.out.print(">");
}
二、SAX操作方式
/**
* @param args
* @throws SAXException
* @throws ParserConfigurationException
* @throws IOException
*/
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException
{
SAXParserFactory saxFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxFactory.newSAXParser();
// saxParser.parse(new File("student.xml"), new Handler());
String xml = "";//xml String
InputSource is=new InputSource(new StringReader(xml));
saxParser.parse(is, new Handler());
}
}
class Handler extends DefaultHandler
{
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException
{
System.out.print("<");
System.out.print(qName);
for(int i = 0 ; i < attributes.getLength();i++)
{
String name = attributes.getQName(i);
String value = attributes.getValue(i);
System.out.print(" "+name + "='" + value+"'");
}
System.out.print(">");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException
{
System.out.print(new String(ch,start,length));
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException
{
System.out.print("</");
System.out.print(qName);
System.out.print(">");
}
三、JDOM操作方式
/**
* @param args
* @throws IOException
* @throws FileNotFoundException
* @throws JDOMException
*/
public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException
{
Document doc = new Document();
doc = doc.addContent(new Element("学生名册"));
Element element = doc.getRootElement();
element.addContent(new Element("学生").setAttribute(
new Attribute("学号", "1")).addContent(
new Element("姓名").setText("张三")).addContent(
new Element("性别").setText("男")).addContent(
new Element("年龄").setText("20")));
XMLOutputter out = new XMLOutputter();
//out.output(doc, new FileOutputStream("student.xml"));
StringWriter sw = new StringWriter();
out.output(doc, sw);
String xml =sw.toString();
SAXBuilder saxBuilder = new SAXBuilder();
//Document doc1= saxBuilder.build(new File("student.xml"));
Document doc1 = saxBuilder.build(new StringReader(xml));
Element root = doc1.getRootElement();
print(root);
/**
* XPath,需要添加jaxen.jar包
*/
List<Element> elements = XPath.selectNodes(root,"//姓名");
System.out.println(elements);
}
public static void print(Element element)
{
System.out.println(element);
List<Element> elements = element.getChildren();
for(Element e : elements)
{
print(e);
}
}
四、Dom4j操作方式
/**
* @param args
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws IOException, DocumentException
{
Element element = DocumentHelper.createElement("学生名册");
Document doc = DocumentHelper.createDocument(element);
Element stu_element = element.addElement("学生");
stu_element.addAttribute("学号", "1");
Element name_element = stu_element.addElement("姓名");
name_element.setText("张三");
Element sex_element = stu_element.addElement("性别");
sex_element.setText("男");
Element age_element = stu_element.addElement("年龄");
age_element.setText("20");
OutputFormat of = new OutputFormat(" ", true);
StringWriter sw = new StringWriter();
XMLWriter xmlwriter = new XMLWriter(sw,of);
xmlwriter.write(doc);
String xml = sw.toString();
System.out.println(xml);
// XMLWriter xmlwriter = new XMLWriter(new FileOutputStream("dom4j_student.xml"),of);
// xmlwriter.write(doc);
SAXReader saxReader = new SAXReader();
Document doc1 = saxReader.read(new StringReader(xml));
//Document doc1 = saxReader.read(new FileInputStream("dom4j_student.xml"));
print(doc1.getRootElement());
/**
* XPath,需要添加jaxen.jar包
*/
List<? extends Node> elements = doc.selectNodes("//姓名");
// XPath xpath = new DefaultXPath("//姓名");
// List<? extends Node> elements = xpath.selectNodes(element);
System.out.println(elements);
}
public static void print(Element element)
{
System.out.println(element);
List<Element> elements = element.elements();
for(Element e : elements)
{
print(e);
}
}