dom生成和解析xml文件,dom解析xml,我特别不喜欢这种方式,最
分享于 点击 49516 次 点评:20
dom生成和解析xml文件,dom解析xml,我特别不喜欢这种方式,最
我特别不喜欢这种方式,最不方便,所以解析上就做的不好,不想再继续做了,烦劳诸位给个好的方式用dom的,直接回复代码。
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://byrx.net
用户点评