DOM解析方式:
@Test public void DOMTest() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(XML4JAVA.class.getClassLoader().getResourceAsStream( "context.xml")); NodeList list = document.getElementsByTagName("bean"); for (int i = 0; i < list.getLength(); i++) { Element element = (Element) list.item(i); System.out.print(element.getAttribute("id") + "="); System.out.println(element.getAttribute("class")); } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void DomTest02() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(XML4JAVA.class.getClassLoader().getResourceAsStream( "context.xml")); Element root = document.getDocumentElement(); parseElement(root); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void parseElement(Element element) { String tagName = element.getNodeName(); NodeList children = element.getChildNodes(); NamedNodeMap map = element.getAttributes(); System.out.print("<" + tagName); if (null != map) { for (int i = 0; i < map.getLength(); i++) { Attr attr = (Attr) map.item(i); System.out.print(" " + attr.getName() + "=\"" + attr.getValue() + "\""); } } System.out.print(">"); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); short nodeType = node.getNodeType(); if (nodeType == Node.ELEMENT_NODE) { parseElement((Element) node); } else if (nodeType == Node.TEXT_NODE) { System.out.print(node.getNodeValue()); } else if (nodeType == Node.COMMENT_NODE) { System.out.print("<!--"); Comment comment = (Comment) node; System.out.print(comment.getData()); System.out.print("-->"); } } System.out.print("</" + tagName + ">"); }
SAX解析方式:
@Test public void testSax() { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); InputStream is = XML4JAVA.class.getClassLoader().getResourceAsStream("context.xml"); DefaultHandler dh = new BeanHandler(); parser.parse(is, dh); if (dh instanceof BeanHandler) { Map<String, String> map = ((BeanHandler) dh).getMap(); for (String key : map.keySet()) { System.out.println(key + ":" + map.get(key)); } } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } class BeanHandler extends DefaultHandler { private String key; private String value; private Map<String, String> map = new HashMap<String, String>(); public Map<String, String> getMap() { return map; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("bean".equals(qName)) { for (int i = 0; i < attributes.getLength(); i++) { String k = attributes.getQName(i); String v = attributes.getValue(i); if (k.equals("id")) { key = v; } else if (k.equals("class")) { value = v; } } map.put(key, value); } } }
DOM4J解析方式:
package org.yzr.utils; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; public class DOM4J { @Test public void testCreate() { try { Document document = DocumentHelper.createDocument(); Element root = DocumentHelper.createElement("root"); document.setRootElement(root); root.addAttribute("id", "1"); Element first = root.addElement("first"); first.addAttribute("id", "2"); first.setText("第一个"); Element second = root.addElement("second"); second.addAttribute("id", "3"); second.setText("第二个"); OutputFormat format = new OutputFormat("\t", true, "UTF-8"); XMLWriter writer = new XMLWriter(new FileWriter("dom4j.xml"), format); writer.write(document); writer.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testRead() { SAXReader reader; try { reader = new SAXReader(); Document document = reader.read(new File("dom4j.xml")); Element root = document.getRootElement(); System.out.println(root.getName()); List<Element> eles = root.elements(); for (int i = 0; i < eles.size(); i++) { Element e = eles.get(i); System.out.println(e.getName()); List<Attribute> attrs = e.attributes(); for (int j = 0; j < attrs.size(); j++) { if (attrs.get(j).getName().equals("id")) { System.out.println("id=" + attrs.get(j).getValue()); } } System.out.println(e.getText()); } } catch (Exception e) { e.printStackTrace(); } } }
JDOM解析方式:
package org.yzr.utils; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Comment; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.output.Format; import org.jdom2.output.XMLOutputter; import org.junit.Test; public class JDOM { @Test public void testCreate() { try { Document document = new Document(); Comment comment = new Comment("创建XML"); document.addContent(comment); Element root = new Element("root"); root.setAttribute(new Attribute("id", "root")); Element first = new Element("first"); first.setAttribute(new Attribute("id", "first")); first.addContent("first"); Element second = new Element("second"); second.setAttribute(new Attribute("id", "second")); second.addContent("second"); root.addContent(first); root.addContent(second); document.addContent(root); Format format = Format.getPrettyFormat(); format.setIndent("\t"); XMLOutputter out = new XMLOutputter(format); out.output(document, new FileWriter("jdom.xml")); } catch (IOException e) { e.printStackTrace(); } } @Test public void testRead() { try { SAXBuilder builder = new SAXBuilder(); Document document = builder.build(new File("jdom.xml")); Element root = document.getRootElement(); System.out.print(root.getName()); Element second = root.getChild("second"); List<Attribute> list = second.getAttributes(); for (int i = 0; i < list.size(); i++) { Attribute attribute = list.get(i); System.out.print(attribute.getName() + "=\""); System.out.print(attribute.getValue() + "\""); } second.setText("2"); XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setIndent("\t")); outputter.output(document, new FileWriter("jdom.xml")); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }