package xml; /** *定义XML文档建立与解析的接口 **/ public interface XmlDocument { /** * 建立XML文档 * @param fileName文件全路径名称 */ public void createXml(String fileName); /** * 解析XML文档 * @param fileName文件全路径名称 */ public void parserXml(String fileName); }
package xml; public class Dom_xml implements XmlDocument { private Document document; private static String fileName="d:/Dom_xml.xml"; public void init() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); this.document = builder.newDocument(); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } } public void createXml(String fileName) { this.init(); Element root = this.document.createElement("employees"); this.document.appendChild(root); Element employee = this.document.createElement("employee"); Element name = this.document.createElement("name"); name.appendChild(this.document.createTextNode("杨过")); employee.appendChild(name); Element sex = this.document.createElement("sex"); sex.appendChild(this.document.createTextNode("男")); employee.appendChild(sex); Element age = this.document.createElement("age"); age.appendChild(this.document.createTextNode("26")); employee.appendChild(age); root.appendChild(employee); Element employee1 = this.document.createElement("employee"); Element name1 = this.document.createElement("name"); name1.appendChild(this.document.createTextNode("小龙女")); employee1.appendChild(name1); Element sex1 = this.document.createElement("sex"); sex1.appendChild(this.document.createTextNode("女")); employee1.appendChild(sex1); Element age1 = this.document.createElement("age"); age1.appendChild(this.document.createTextNode("28")); employee1.appendChild(age1); root.appendChild(employee1); TransformerFactory tf = TransformerFactory.newInstance(); try { Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); PrintWriter pw = new PrintWriter(new FileOutputStream(fileName)); StreamResult result = new StreamResult(pw); transformer.transform(source, result); System.out.println("生成XML文件成功:"+fileName); } catch (TransformerConfigurationException e) { System.out.println(e.getMessage()); } catch (IllegalArgumentException e) { System.out.println(e.getMessage()); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (TransformerException e) { System.out.println(e.getMessage()); } } public void parserXml(String fileName) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(fileName); NodeList employees = document.getChildNodes(); for (int i = 0; i < employees.getLength(); i++) { //employees节点 Node employee = employees.item(i); //employees节点下的子节点 NodeList employeeInfo = employee.getChildNodes(); for (int j = 0; j < employeeInfo.getLength(); j++) { //employee节点 Node node = employeeInfo.item(j); //employee节点下的子节点 NodeList employeeMeta = node.getChildNodes(); for (int k = 0; k < employeeMeta.getLength(); k++) { System.out.println(k+": "+employeeMeta.item(k).getNodeName() + ":" + employeeMeta.item(k).getTextContent()); } } } System.out.println("解析完毕!"); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } } public static void main(String[] arg){ Dom_xml dom_xml=new Dom_xml(); dom_xml.createXml(fileName); dom_xml.parserXml(fileName); } }
package xml; public class Sam_xml implements XmlDocument { private static String fileName="d:/Sam_xml.xml"; public void createXml(String fileName) { } public void parserXml(String fileName) { SAXParserFactory saxfac = SAXParserFactory.newInstance(); try { SAXParser saxparser = saxfac.newSAXParser(); InputStream is = new FileInputStream(fileName); saxparser.parse(is, new MySAXHandler()); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] arg){ Sam_xml sam_xml=new Sam_xml(); sam_xml.createXml(fileName); sam_xml.parserXml(fileName); } } class MySAXHandler extends DefaultHandler { boolean hasAttribute = false; Attributes attributes = null; public void startDocument() throws SAXException { System.out.println("parsing start"); } public void endDocument() throws SAXException { System.out.println("parsing end"); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equals("employees")) { return; } if (qName.equals("employee")) { System.out.println(qName); } if (attributes.getLength() > 0) { this.attributes = attributes; this.hasAttribute = true; } } public void endElement(String uri, String localName, String qName) throws SAXException { if (hasAttribute && (attributes != null)) { for (int i = 0; i < attributes.getLength(); i++) { System.out.println(attributes.getQName(0) + attributes.getValue(0)); } } } public void characters(char[] ch, int start, int length) throws SAXException { System.out.println(new String(ch, start, length)); } }
package xml; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class Jdom_xml { private Document document; private static String fileName="d:/Jdom_xml.xml"; /** * 生产xml * @param fileName */ public void createXml(String fileName) { Element root; root = new Element("employees"); this.document = new Document(root); Element employee = new Element("employee"); root.addContent(employee); Element name = new Element("name"); name.setText("杨过"); employee.addContent(name); Element sex = new Element("sex"); sex.setText("男"); employee.addContent(sex); Element age = new Element("age"); age.setText("26"); employee.addContent(age); Element employee1 = new Element("employee"); root.addContent(employee1); Element name1 = new Element("name"); name1.setText("小龙女"); employee1.addContent(name1); Element sex1 = new Element("sex"); sex1.setText("女"); employee1.addContent(sex1); Element age1 = new Element("age"); age1.setText("28"); employee1.addContent(age1); //设置xml编码 XMLOutputter XMLOut = new XMLOutputter(); Format format=XMLOut.getFormat(); format.setEncoding("gb2312"); XMLOut.setFormat(format); try { XMLOut.output(document, new FileOutputStream(fileName)); System.out.println("生产xml成功:"+fileName); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 解析xml * @param fileName */ @SuppressWarnings("rawtypes") public void parserXml(String fileName) { SAXBuilder builder = new SAXBuilder(false); try { Document document = builder.build(fileName); Element employees = document.getRootElement(); List employeeList = employees.getChildren("employee"); for (int i = 0; i < employeeList.size(); i++) { Element employee = (Element) employeeList.get(i); List employeeInfo = employee.getChildren(); for (int j = 0; j < employeeInfo.size(); j++) { System.out.println(((Element) employeeInfo.get(j)).getName() + ":" + ((Element) employeeInfo.get(j)).getValue()); } } } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] arg){ Jdom_xml jdom_xml=new Jdom_xml(); jdom_xml.createXml(fileName); jdom_xml.parserXml(fileName); } }
package xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Dom4j_xml implements XmlDocument { private Document document; private static String fileName="d:/Dom4j_xml.xml"; /** * 生产xml */ public void createXml(String fileName) { this.document = DocumentHelper.createDocument(); Element employees = document.addElement("employees"); Element employee = employees.addElement("employee"); Element name = employee.addElement("name"); name.setText("杨过"); Element sex = employee.addElement("sex"); sex.setText("男"); Element age = employee.addElement("age"); age.setText("26"); Element employee1 = employees.addElement("employee"); Element name1 = employee1.addElement("name"); name1.setText("小龙女"); Element sex1 = employee1.addElement("sex"); sex1.setText("女"); Element age1 = employee1.addElement("age"); age1.setText("28"); try { //设置XML编码 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gb2312"); //使用字节流 XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File(fileName))); xmlWriter.write(document); xmlWriter.flush(); xmlWriter.close(); System.out.println("生成xml成功:"+fileName); } catch (IOException e) { System.out.println(e.getMessage()); } } /** * 解析xml */ @SuppressWarnings("rawtypes") public void parserXml(String fileName) { File inputXml = new File(fileName); SAXReader saxReader = new SAXReader(); try { Document document = saxReader.read(inputXml); Element employees = document.getRootElement(); for (Iterator i = employees.elementIterator(); i.hasNext();) { Element employee = (Element) i.next(); for (Iterator j = employee.elementIterator(); j.hasNext();) { Element node = (Element) j.next(); System.out.println(node.getName() + ":" + node.getText()); } } } catch (DocumentException e) { System.out.println(e.getMessage()); } } public static void main(String[] arg){ Dom4j_xml dom4j_xml=new Dom4j_xml(); dom4j_xml.createXml(fileName); dom4j_xml.parserXml(fileName); } }
参考:
http://developer.51cto.com/art/200903/117512.htm