Java tutorial
/** * Copyright (c) 2013-2015 www.javahih.com * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.hihframework.osplugins.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import jxl.common.Logger; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * <p> Title:</p> * <p> Description:?XML?</p> * <p> Copyright: Copyright (c) 2013 </p> * <p> Company:hihsoft.co.,ltd </p> * @author hihsoft.co.,ltd * @version 1.0 */ public class XmlParseUtil { private Logger log = Logger.getLogger(XmlParseUtil.class); //------------------------------------------------------------------ /** * ?XML * * @return */ public Document CreateDocument() { Document document = DocumentHelper.createDocument(); return document; } /** * XML * * @param document * XML * @param rootName??? * @return element? */ public Element createRootElement(Document document, String rootName) { Element element = document.addElement(rootName); return element; } /** * , ?DTD? Doctype * * @param document * XML * @param rootName * ?? * @param docType * */ public void addDocType(Document document, String rootName, String docType) { document.addDocType(rootName, null, docType); //<!DOCTYPE catalog SYSTEM "file://c:/Dtds/catalog.dtd"> } /** * ? * * @param element * ? * @param comment * */ public void addComment(Element element, String comment) { element.addComment(comment); } /** * ? * * @param parentelement???? * @param elementName???? * @return Element */ public Element addElement(Element parentelement, String elementName) { Element element = parentelement.addElement(elementName); return element; } /** * ? * * @param element??? * @param attributeName??? * @param attributeValue? */ public void addAttribute(Element element, String attributeName, String attributeValue) { element.addAttribute(attributeName, attributeValue); } /** * ? * * @param element * ?? * @param text * ? */ public void setText(Element element, String text) { element.setText(text); } /** * XML?? XML??? * * @param document * ? * @param file * ?XML */ public void writeXml(Document document, File file) { try { //? OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter output = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"), format); output.write(document); output.flush(); output.close(); } catch (IOException e) { log.info(e.getMessage()); } } /** * XML?? ? * * @param document * ? * @param file * ?XML */ public void writeXml(Document document) { try { //? OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter output = new XMLWriter(System.out, format); //XMLWriter output = new XMLWriter(new FileWriter(file)); output.write(document); output.flush(); output.close(); } catch (IOException e) { log.info(e.getMessage()); } } //------------------------------?----------------------------------- //?XML??XML public Document readXml(File file) throws MalformedURLException, DocumentException { SAXReader reader = new SAXReader(); Document document = reader.read(file); //Document document=reader.read(InputStream); //Document document=reader.read(url); return document; } /** * ?ROOT * * @param doc * ?XML * @return ROOT? */ public Element getRootElement(Document doc) { Element rootElement = doc.getRootElement(); return rootElement; } /** * ??? * * @param rootElement * * @return ?? */ public String getRootName(Element rootElement) { String rootName = rootElement.getName(); return rootName; } /** * ??? ??nameparentelementelement, * elementNamenameelement?? * * @param parentelement * ? * @param elementName * ??? * @return ? */ public Element getElement(Element parentelement, String elementName) { Element element = null; for (Iterator<?> i = parentelement.elementIterator(elementName); i.hasNext();) { element = (Element) i.next(); } return element; } /** * ?? * * @param element * * @return */ public String getElementText(Element element) { String elementText = element.getText(); return elementText; } /** * ??? * * @param element * * @param attributeName * ?? * @return */ public String getAttributeValue(Element element, String attributeName) { Attribute Attribute = element.attribute(attributeName); String attributeValue = Attribute.getValue(); return attributeValue; } /** * ? * * @param document * * @param elementName * ? * @return ? */ public Element FindElement(Document document, String elementName) { Element root = document.getRootElement(); Element element = null; for (Iterator<?> i = root.elementIterator(elementName); i.hasNext();) { element = (Element) i.next(); } return element; } /** * ?? * * @param element * * @return */ public List<Object> attributeList(Element element) { @SuppressWarnings("unchecked") List<Object> list = element.attributes(); return list; } public int getElementCount(Element element) { int i = 0; Iterator<?> iterator = element.elementIterator(); while (iterator.hasNext()) { i++; iterator.next(); } return i; } /** * ??? * * @param element * @return */ public int getAttributeCount(Element element) { int attributeCount = element.attributeCount(); return attributeCount; } /** * ?? * * @param element * * @return */ public Iterator<Object> attributeIterator(Element element) { @SuppressWarnings("unchecked") Iterator<Object> attrIterator = element.attributeIterator(); return attrIterator; } /** * ?? * * @param element * * @return */ public HashMap<String, String> attributeMap(Element element) { HashMap<String, String> attributeHah = new HashMap<String, String>(); Iterator<?> elemIterator = element.attributeIterator(); while (elemIterator.hasNext()) { Attribute attribute = (Attribute) elemIterator.next(); String attributeName = attribute.getName(); String attributeValue = attribute.getValue(); attributeHah.put(attributeName, attributeValue); } return attributeHah; } /** * ??? * * @param element * * @return ? */ public Iterator<Object> elementIterator(Element element) { @SuppressWarnings("unchecked") Iterator<Object> elemIterator = element.elementIterator(); return elemIterator; } /** * ??? * @param element * @return */ public List<Object> elementList(Element element) { @SuppressWarnings("unchecked") List<Object> list = element.elements(); return list; } /** * ??? * * @param element * * @param elementName? * @return? */ public Iterator<Object> elementIterator(Element element, String elementName) { @SuppressWarnings("unchecked") Iterator<Object> elemIterator = element.elementIterator(elementName); return elemIterator; } /** * ????? * * @param element * * @return??? */ public ArrayList<String> getElementNameList(Element element) { Iterator<?> elemIterator = element.elementIterator(); ArrayList<String> list = new ArrayList<String>(); while (elemIterator.hasNext()) { Element childElement = (Element) elemIterator.next(); list.add(childElement.getName()); } return list; } /** * ??? * * @param element * * @param attributeName * ?? * @return ? */ public void setAttributeValue(Element element, String attributeName, String attributeValue) { Attribute attribute = element.attribute(attributeName); attribute.setValue(attributeValue); } /** * ??? * * @param element * ? * @param elementName * ??? */ public void setElementName(Element element, String elementName) { element.setName(elementName); } /** * * @param element * @param attributeName */ public boolean deleteAllAttribute(Element element, String attributeName) { Attribute attribute = (Attribute) element.attribute(attributeName); boolean deleteFlag = element.remove(attribute); return deleteFlag; } /** * ? * * @param element * ? */ public boolean deleteAttribute(Element element) { boolean deleteFlag = false; Iterator<?> iterator = element.attributeIterator(); while (iterator.hasNext()) { Attribute attribute = (Attribute) iterator.next(); deleteFlag = element.remove(attribute); } return deleteFlag; } //--------------------XPath??-------------------- /** * ? * * @param document * * @param attributeName * ??????//??/@??? * //article/@levelarticlelevel * @param attributeValue * */ public void setAttrValues(Document document, String attributeName, String attributeValue) { List<?> list = document.selectNodes(attributeName); Iterator<?> iter = list.iterator(); while (iter.hasNext()) { Attribute attribute = (Attribute) iter.next(); attribute.setValue(attributeValue); } } /** * ? * * @param document * @param NodeName??????//??? * //articlearticle * @param element * ??? */ public void setElementValue(Document document, String NodeName, String elementName, String elementValue) { List<?> list = document.selectNodes(NodeName); Iterator<?> iter = list.iterator(); while (iter.hasNext()) { Element element = (Element) iter.next(); Iterator<?> iterator = element.elementIterator(elementName); while (iterator.hasNext()) { Element titleElement = (Element) iterator.next(); titleElement.setText(elementValue); } } } /** * ? * * @param document * * @param nodesName * ?? ?"//??"/??",????? * @return ? */ public List<?> getNodes(Document document, String nodesName) { List<?> list = document.selectNodes(nodesName); //list?????? /* * for (Iterator iter = list.iterator(); iter.hasNext(); ) { Attribute * attribute = (Attribute) iter.next(); //??attribute String url = * attribute.getValue(); } */ /* * for (Iterator iter = list.iterator(); iter.hasNext(); ) { Element * element = (Element) iter.next(); //??element } */ return list; } /** * ?? * * @param document * * @param nodesName * ?? ?"//??"/??" * ?//foo/barbar??? * @return ?? */ public Node getNode(Document document, String nodesName) { Node node = document.selectSingleNode(nodesName); return node; } /** * ? * * @param document * * @param nodesName * ?? ?"//??"/??" * ?//foo/barbar??? * @return ?? */ public String getNodeText(Node node, String nodeName) { String nodeText = node.valueOf(nodeName); return nodeText; } public static void main(String[] args) throws DocumentException, MalformedURLException { //----------------------??XML----------------------------- /* * XmlParseUtil xp = new XmlParseUtil(); Document doc = * xp.CreateDocument();// Element rootElement = * xp.createRootElement(doc, "?");// xp.addComment(rootElement, * "??");// //? Element goodstudElement = * xp.addElement(rootElement, "?"); //? Element * plainElement = xp.addElement(rootElement, "?"); * //?? Element studentElement = * xp.addElement(goodstudElement, ""); //23 * xp.addAttribute(studentElement, "", ""); * xp.addAttribute(studentElement, "", "23"); //??? Element * nameElement = xp.addElement(studentElement, "??"); //?? * xp.setText(nameElement, ""); //? Element * scoreElement = xp.addElement(studentElement, ""); * //600 xp.setText(scoreElement, "600"); //?? * Element techerElement = xp.addElement(studentElement, "?"); * //?? xp.setText(techerElement, "?"); //? * Element actElement = xp.addElement(studentElement, ""); * //?20042? xp.addAttribute(actElement, "", * ""); xp.addAttribute(actElement, "", ""); * xp.addAttribute(actElement, "?", ""); Element totalElement = * xp.addElement(actElement, "?"); //? * xp.setText(totalElement, ""); Element pjiaElement = * xp.addElement(actElement, "?"); //? * xp.setText(pjiaElement, ""); //??catalog.xml File file = new * File("c:/catalog.xml"); //? xp.writeXml(doc, file); */ //----------------------??XML-------------------------- /*File file = new File("c:/catalog.xml"); XmlParseUtil xp = new XmlParseUtil(); // Document document = xp.readXml(file); //?ROOT Element rootElement = xp.getRootElement(document); //??? String rootName = xp.getRootName(rootElement); log.info(rootName); // ????? ArrayList elementNameList = xp.getElementNameList(rootElement); if (!elementNameList.isEmpty()) { for (int i = 0; i < elementNameList.size(); i++) { //??? log.info((String) elementNameList.get(i)); } } //????? Iterator iterator = xp.elementIterator(rootElement); while (iterator.hasNext()) { //? Element stuelement = (Element) iterator.next(); log.info(stuelement.getName()); //stuelement? Iterator stuiterator = xp.elementIterator(stuelement); while (stuiterator.hasNext()) { Element studentelement = (Element) stuiterator.next(); //???? String saleAttrValue = xp.getAttributeValue(studentelement, ""); log.info(saleAttrValue); String ageAttrValue = xp .getAttributeValue(studentelement, ""); log.info(ageAttrValue); //?? HashMap attributeMap = (HashMap) xp .attributeMap(studentelement); //......?HashMap? //???? ArrayList studentelementList = xp .getElementNameList(studentelement); if (!studentelementList.isEmpty()) { for (int i = 0; i < studentelementList.size(); i++) { log.info(studentelementList.get(i)); } } //?? log.info(xp.getElementCount(studentelement)); //? log.info(xp.getAttributeCount(studentelement)); // xp.setAttributeValue(studentelement, "", ""); // log.info(xp.getAttributeValue(studentelement, "")); xp.setElementName(studentelement, ""); //??? Iterator elementiterator = xp.elementIterator(studentelement); while (elementiterator.hasNext()) { //??? Element element = (Element) elementiterator.next(); //?? int count = xp.getElementCount(element); //? if (count == 0) { String elementText = xp.getElementText(element); log.info(elementText); } else { //?? Iterator attrIterator = xp.attributeIterator(element); if (attrIterator.hasNext()) { //?? Attribute attribute = (Attribute) attrIterator .next(); log.info(attribute.getName()); log.info(attribute.getValue()); log.info(xp.getAttributeCount(element)); } Iterator elemIterator = xp.elementIterator(element); if (elemIterator.hasNext()) { Element bxelement = (Element) elemIterator.next(); String elementText = xp.getElementText(bxelement); log.info(elementText); } } } } } xp.writeXml(document, file);*/ File file = new File("generator.xml"); XmlParseUtil xp = new XmlParseUtil(); // Document document = xp.readXml(file); //?ROOT Element rootElement = xp.getRootElement(document); //??? String rootName = xp.getRootName(rootElement); System.out.println(rootName);//?? //???? Iterator<?> iterator = xp.elementIterator(rootElement); //???? List<?> list = xp.elementList(rootElement); System.out.println(list.size());//?? System.out.println(xp.getElementCount(rootElement));//?? //?? while (iterator.hasNext()) { //?? Element element = (Element) iterator.next(); //?? String attributeValue = xp.getAttributeValue(element, "name"); //???? if (attributeValue.equals("zhongyi")) { //???? System.out.println(element.getName()); //???? Iterator<?> it = xp.elementIterator(element); while (it.hasNext()) { Element childElement = (Element) it.next(); String elementName = xp.getRootName(childElement); String elementText = xp.getElementText(childElement); System.out.println(elementName + ":=" + elementText); if (elementName.equals("address")) { xp.setText(childElement, ""); } } } } // xp.writeXml(document, file); } }