Dom4jUtil

public class Dom4jUtil implements XmlDocument {

	String root="employees";
	public void createXml(String fileName) {
		// TODO Auto-generated method stub
		Document document = DocumentHelper.createDocument();   
        Element employees=document.addElement(root);//root
        Element employee=employees.addElement("employee");   
        employee.addAttribute("title", "XML Zone"); //给employee添加title属性,并设置他的值
        Element name= employee.addElement("name");name.setText("小倪");
        Element sex=employee.addElement("sex");sex.setText("famale");
       
        Element age=employee.addElement("age");age.setText("29");
       
        try {
            XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(fileName));
//            Writer fileWriter=new FileWriter(fileName);   
//            XMLWriter xmlWriter=new XMLWriter(fileWriter);   
            xmlWriter.write(document);   
            xmlWriter.close();

        } catch (IOException e) {   
            System.out.println(e.getMessage());   
        }   

	}

	public void parserXml(String fileName) {//只能解析2层结构的xml(不包括根root)
		// TODO Auto-generated method stub
		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());   
       }   
//       System.out.println("dom4j parserXml");   

	}
	public static void main(String[] args){
		//System.out.println(new Util().hash("111111"));
		Dom4jUtil dom=new Dom4jUtil();
		String fileName="C:\\t.xml";
		dom.createXml(fileName);
		System.out.println("ok!!!");
//		dom.parserXml(fileName);
		dom.getRoot(dom.readXML(fileName));
		
	}

	//递归解析XML
	//用法:dom.getRoot(dom.readXML(fileName));
	public Document readXML(String fileName) {
        Document document = null;   
        SAXReader saxReader = new SAXReader();   
        try {   
            document = saxReader.read(new File(fileName));   
        } catch (DocumentException e) {   
            e.printStackTrace();   
        }   
        return document;   
    }
	 public void getRoot(Document document) {   
	        Element root = document.getRootElement();   
	        tree(root);   
	    }

    private void tree(Element element) {//递归解析xml
        Iterator iterator = element.elementIterator();   
        List list = null;   
        while (iterator.hasNext()) {   
            Element node = (Element) iterator.next();   
            list = node.elements();   
            if (list != null && list.size() > 0) {   
//                System.out.print(node.getName()+"\n");//父元素 
                int count = node.attributeCount();   
                for (int i = 0; i < count; i++) {   
                    Attribute attr = node.attribute(i);   
                    System.out.println("属性" + attr.getName() + ":" + attr.getText());   
                }
                tree(node);   
            } else {   
                System.out.println(node.getName() + "=" + node.getText());   
            }   
        }   
    }

}


出现Invalid byte 2 of 2-byte UTF-8 sequence原因:
FileWriter对象进行文件的输出是不能正确进行文件编码的原因所在,java中由Writer类继承下来的子类没有提供编码格式处理,所以dom4j也就无法对输出的文件进行正确的格式处理。这时候所保存的文件会以系统的默认编码对文件进行保存,在中文版的window下java的默认的编码为GBK,也就是所虽然我们标识了要将xml保存为utf-8格式但实际上文件是以GBK格式来保存的,所以这也就是为什么能够我们使用GBK、GB2312编码来生成xml文件能正确的被解析,而以UTF-8格式生成的文件不能被xml解析器所解析的原因。
所以我们在使用dom4j的来生成我们的XML文件时不应该直接为在构建XMLWriter时,不应该直接为其赋一个Writer对象,而应该通过一个OutputStream的子类对象来构建。也就是说在我们上面的代码中,不应该用FileWriter对象来构建xml文档,而应该使用FileOutputStream对象来构建

你可能感兴趣的:(xml,J#)