- private static Document doc= null;
- private static Person[] persons= null;
- private static File file= new File("dom.xml");
- public static void main(String[] A) {
- // init();
- // setBeanValue();
- parseInit();
- }
- public static void init() {//Document
- DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder builder= factory.newDocumentBuilder();
- doc= builder.newDocument();
- } catch (ParserConfigurationException e) {
- System.out.println("DocumentBuilder.");
- e.printStackTrace();
- }
- }
- public static void setBeanValue() {
- Element root= doc.createElement("root");
- doc.appendChild(root);
- Element personsxml= doc.createElement("persons");
- root.appendChild(personsxml);
- persons= new Person[3];
- for(int i= 0; i< 3; i++) {
- persons[i]= new Person();
- persons[i].setAge(i+ "");
- persons[i].setSex("");
- Element person= doc.createElement("person");
- personsxml.appendChild(person);
- createXML(person, persons[i]);
- }
- try {
- DOMSource source= new DOMSource(doc);
- if(!file.exists()) {
- file.createNewFile();
- }
- FileOutputStream output= new FileOutputStream(file);
- TransformerFactory factory= TransformerFactory.newInstance();
- Transformer transform= factory.newTransformer();
- StreamResult result= new StreamResult(output);
- transform.setOutputProperty(OutputKeys.ENCODING, "gb2312");
- transform.setOutputProperty(OutputKeys.INDENT, "yes");
- transform.transform(source, result);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (TransformerConfigurationException e) {
- e.printStackTrace();
- } catch (TransformerException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void createXML(Node node, Object obj) {//xml
- Class className= obj.getClass();
- Field[] fields= className.getDeclaredFields();
- for(Field field: fields) {
- field.setAccessible(true);
- String name= field.getName();
- Element elementName= doc.createElement(name);
- try {
- elementName.setTextContent((String)field.get(obj));
- } catch (DOMException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- node.appendChild(elementName);
- }
- }
- public static void parseInit() {
- DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();
- try {
- DocumentBuilder builder= factory.newDocumentBuilder();
- Document doc= builder.parse(file);
- NodeList nodeList= doc.getChildNodes();
- for(int i= 0; i< nodeList.getLength(); i++) {
- Node node= nodeList.item(i);//root
- parseXML(node);
- }
- } catch (ParserConfigurationException e) {
- e.printStackTrace();
- } catch (SAXException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static void parseXML(Node node) {
- NodeList list= node.getChildNodes();//persons
- if(list!= null) {
- for(int i= 0; i< list.getLength(); i++) {
- Node childNode= list.item(i);//person
- NodeList childList= childNode.getChildNodes();
- if(childList!= null&& childNode.getNodeType()!= 3) {
- System.out.println("childList!= null : "+childNode.getNodeName());
- parseXML(childNode);
- }
- if(childNode.getTextContent().trim()!= null|| !(childNode.getTextContent().trim().equals(""))) {
- System.out.println(childNode.getNodeName()+"--->"+childNode.getTextContent().trim());
- }
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/171220138090.html
来源: http://www.codesnippet.cn/detail/171220138090.html