XML配置文件怎么写,都有些什么属性?java代码如何解析XML配置文件?
3个回答
展开全部
xml是类似这种的:
<?xml version="1.0" encoding="gbk"?>
<head></head>
<body></body>
可以在节点中加属性例如:
<body id = "" name = ""></body>
java代码解析一般用DOM4j.分两种类型的解析方式:
1.DOM: Document Object Model 文档对象模型
树形结构装载到内存中.完成后才开始解析.
缺点:如果文件比较大,内存有压力,时间会延迟.
优点:可以对树形结构做任何操作:加节点,删节点,遍历...
2.SAX: Simple API for XML
流(Stream)的特征,解析可以立即开始.
不保存数据. 对内存无压力.不能修改节点.
适合大文件的解析.
下面附上代码:
public static List<String> parseCustomXML(String fileName) {
// 定义解析器
SAXReader saxReader = new SAXReader();
List<String> eleList = new ArrayList<String>();
// 开始解析,返回文档对象
/*
* <root>
<msg>
<element totalLenghtorder = "1" totalLenghtlenght = "8">totalLenght</element>
<element totalLenghtorder = "2" totalLenghtlenght = "8">bodyLenght</element>
<element jydmorder = "3" jydmlenght = "6">jydm</element>
<element msgTypeorder = "4" msgTypelenght = "2">msgType</element>
<element sendSignorder = "5" sendSignlenght = "1">sendSign</element>
<element msgCheckCodeorder = "6" msgCheckCodelenght = "32">msgCheckCode</element>
<element serviceNameorder = "7" serviceNamelenght = "8">serviceName</element>
<element msgAgreementorder = "8" msgAgreementlenght = "1">msgAgreement</element>
<element cprqorder = "9" cprqlenght = "8">cprq</element>
<element fkrzhorder = "10" fkrzhlenght = "20">fkrzh</element>
<element pjhmorder = "11" pjhmlenght = "8">pjhm</element>
<element jeorder = "12" jelenght = "20">je</element>
<element zfmmorder = "13" zfmmlenght = "20">zfmm</element>
<element jghorder = "14" jghlenght = "20">jgh</element>
<element yhdmorder = "15" yhdmlenght = "20">yhdm</element>
</msg>
</root>
*/
try {
Document doc = saxReader.read(new File(fileName));
Element root = doc.getRootElement();// 获取根节点
Element msg = root.element("msg");
Iterator itEle = msg.elementIterator("element");// 遍历msg节点下的所有element节点
while (itEle.hasNext()) {
Element element = (Element) itEle.next();
String value = element.getText();
//测试
System.out.println(value);
eleList.add(value);
Iterator itAttr = element.attributeIterator();// 遍历每一个element节点的所有属性
while (itAttr.hasNext()) {
Attribute attr = (Attribute) itAttr.next();
//String keyAttr = attr.getName();
String valueAttr = attr.getValue();
//测试
System.out.println(valueAttr);
eleList.add(valueAttr);
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return eleList;
}
有不明白的 在帮你分析
<?xml version="1.0" encoding="gbk"?>
<head></head>
<body></body>
可以在节点中加属性例如:
<body id = "" name = ""></body>
java代码解析一般用DOM4j.分两种类型的解析方式:
1.DOM: Document Object Model 文档对象模型
树形结构装载到内存中.完成后才开始解析.
缺点:如果文件比较大,内存有压力,时间会延迟.
优点:可以对树形结构做任何操作:加节点,删节点,遍历...
2.SAX: Simple API for XML
流(Stream)的特征,解析可以立即开始.
不保存数据. 对内存无压力.不能修改节点.
适合大文件的解析.
下面附上代码:
public static List<String> parseCustomXML(String fileName) {
// 定义解析器
SAXReader saxReader = new SAXReader();
List<String> eleList = new ArrayList<String>();
// 开始解析,返回文档对象
/*
* <root>
<msg>
<element totalLenghtorder = "1" totalLenghtlenght = "8">totalLenght</element>
<element totalLenghtorder = "2" totalLenghtlenght = "8">bodyLenght</element>
<element jydmorder = "3" jydmlenght = "6">jydm</element>
<element msgTypeorder = "4" msgTypelenght = "2">msgType</element>
<element sendSignorder = "5" sendSignlenght = "1">sendSign</element>
<element msgCheckCodeorder = "6" msgCheckCodelenght = "32">msgCheckCode</element>
<element serviceNameorder = "7" serviceNamelenght = "8">serviceName</element>
<element msgAgreementorder = "8" msgAgreementlenght = "1">msgAgreement</element>
<element cprqorder = "9" cprqlenght = "8">cprq</element>
<element fkrzhorder = "10" fkrzhlenght = "20">fkrzh</element>
<element pjhmorder = "11" pjhmlenght = "8">pjhm</element>
<element jeorder = "12" jelenght = "20">je</element>
<element zfmmorder = "13" zfmmlenght = "20">zfmm</element>
<element jghorder = "14" jghlenght = "20">jgh</element>
<element yhdmorder = "15" yhdmlenght = "20">yhdm</element>
</msg>
</root>
*/
try {
Document doc = saxReader.read(new File(fileName));
Element root = doc.getRootElement();// 获取根节点
Element msg = root.element("msg");
Iterator itEle = msg.elementIterator("element");// 遍历msg节点下的所有element节点
while (itEle.hasNext()) {
Element element = (Element) itEle.next();
String value = element.getText();
//测试
System.out.println(value);
eleList.add(value);
Iterator itAttr = element.attributeIterator();// 遍历每一个element节点的所有属性
while (itAttr.hasNext()) {
Attribute attr = (Attribute) itAttr.next();
//String keyAttr = attr.getName();
String valueAttr = attr.getValue();
//测试
System.out.println(valueAttr);
eleList.add(valueAttr);
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
return eleList;
}
有不明白的 在帮你分析
追问
谢谢你的回答!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询