导入源码后注释出现中文乱码
package com.imooc.dom4j;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class HrReader {
public void readXml(){
String file = "d:/workspace/xml/src/hr.xml";
//SAXReader���Ƕ�ȡXML�ļ��ĺ����࣬���ڽ�XML�������ԡ���������ʽ�������ڴ��С�
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
//��ȡXML�ĵ��ĸ��ڵ㣬��hr��ǩ
Element root = document.getRootElement();
//elements�������ڻ�ȡָ���ı�ǩ����
List<Element> employees = root.elements("employee");
for(Element employee : employees){
//element�������ڻ�ȡΨһ���ӽڵ����
Element name = employee.element("name");
String empName = name.getText();//getText()�������ڻ�ȡ��ǩ�ı�
System.out.println(empName);
System.out.println(employee.elementText("age"));
System.out.println(employee.elementText("salary"));
Element department = employee.element("department");
System.out.println(department.element("dname").getText());
System.out.println(department.element("address").getText());
Attribute att = employee.attribute("no");
System.out.println(att.getText());
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
HrReader reader = new HrReader();
reader.readXml();
}
}
我试了把编码类型改为utf-8但是没用
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星