java 读取xml
<?xml version= ecodeinf= ?>
<parms name="xxx">
<param name="a">this is for test</param>
<param name="b">this is for test</param>
<param name="c">this is for test</param>
</parms>
1.xml解析还是用dom4j方便,
import
java.util.List;
import
org.dom4j.Document;
import
org.dom4j.DocumentException;
import
org.dom4j.Element;
import
org.dom4j.io.SAXReader;
public
class
XMLPaser {
public
static
void
main(String[] args) {
paserXML();
}
public
static
void
paserXML(){
SAXReader reader =
new
SAXReader();
try
{
// 读取XML文件
Document doc = reader.read(
"NewFile.xml"
);
Element root = doc.getRootElement();
System.out.println(root.getName());
List<Element> param = root.elements();
for
(Element element : param) {
if
(element.attributeValue(
"name"
).equals(
"a"
)){
System.out.println(element.getText());
}
}
}
catch
(DocumentException e) {
e.printStackTrace();
}
}
}
2.老方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public static void main(String args[]) { readXmlFile( "etc/accoumt.xml" ); } public static void readXmlFile(String fileName) { Element element = null ; File f = new File(fileName); DocumentBuilder db = null ; // documentBuilder为抽象不能直接实例化(将XML文件转换为DOM文件) DocumentBuilderFactory dbf = null ; try { dbf = DocumentBuilderFactory.newInstance(); // 返回documentBuilderFactory对象 db = dbf.newDocumentBuilder(); // 返回db对象用documentBuilderFatory对象获得返回documentBuildr对象 Document dt = db.parse(f); // 得到一个DOM并返回给document对象 element = dt.getDocumentElement(); // 得到一个elment根元素 System.out.println( "根元素:" + element.getNodeName()); // 获得根节点 NodeList childNodes = element.getChildNodes(); // 获得根元素下的子节点 for ( int i = 0 ; i < childNodes.getLength(); i++) // 遍历这些子节点 { Node node1 = childNodes.item(i); // childNodes.item(i); // 获得每个对应位置i的结点 if ( "Account" .equals(node1.getNodeName())) { // 如果节点的名称为"Account",则输出Account元素属性type System.out.println( "\r\n找到一个账号. 所属区域:s" + node1.getAttributes().getNamedItem( "type" ).getNodeValue() + "$$" ); NodeList nodeDetail = node1.getChildNodes(); // 获得<Accounts>下的节点 for ( int j = 0 ; j < nodeDetail.getLength(); j++) { // 遍历<Accounts>下的节点 Node detail = nodeDetail.item(j); // 获得<Accounts>元素每一个节点 if ( "code" .equals(detail.getNodeName())) // 输出code System.out.println( "卡号: " + detail.getTextContent()); else if ( "pass" .equals(detail.getNodeName())) // 输出pass System.out.println( "密码: " + detail.getTextContent()); else if ( "name" .equals(detail.getNodeName())) // 输出name System.out.println( "姓名: " + detail.getTextContent()); else if ( "money" .equals(detail.getNodeName())) // 输出money System.out.println( "余额: " + detail.getTextContent()); } } } } catch (Exception e) { System.out.println(e); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "gbk" ?> < Accounts > < Account type = "by0003" > < code >100001</ code > < pass >123</ pass > < name >李四</ name > < money >1000000.00</ money > </ Account > < Account type = "hz0001" > < code >100002</ code > < pass >123</ pass > < name >张三</ name > < money >1000.00</ money > </ Account > </ Accounts > |