易截截图软件、单文件、免安装、纯绿色、仅160KB

JAVA 解析 XML

XML文件时很多项目经常使用的配置或者其他用途的文件格式,解析xml文件格式的开发包也很多,本文主要使用java自带的DOM解析包。本文以一个实际例子来讲述解析xml文件的过程。
目标:解析这样一个XML文件
employees.xml
=======内容开始========
<?xml version="1.0" encoding="UTF-8"?>
<Personnel>
  <Employee type="permanent">
        <Name>Seagull</Name>
        <Id>3674</Id>
        <Age>34</Age>
   </Employee>
  <Employee type="contract">
        <Name>Robin</Name>
        <Id>3675</Id>
        <Age>25</Age>
    </Employee>
  <Employee type="permanent">
        <Name>Crow</Name>
        <Id>3676</Id>
        <Age>28</Age>
    </Employee>
</Personnel>
=======内容结束========
目标是:输出如下内容
Employee Details - Name:Seagull, Type:permanent, Id:3674, Age:34.
Employee Details - Name:Robin, Type:contract, Id:3675, Age:25.
Employee Details - Name:Crow, Type:permanent, Id:3676, Age:28.
主要步骤
1,
Get a document builder using document builder factory and parse the xml
file to create a DOM object

Get a list of employee elements from the DOM

For each employee element get the id, name, age and type. Create an
employee value object and add it to the list.

At the end iterate through the list and print the employees to verify we
parsed it right.
a) 获取document builder
public void parseXmlFile(){
//get the factory
DocumentBuilderFactory dbf = DocumentBu


相关文档:

Java异常处理及异常机制介绍

一,什么是异常
      当出现程序无法控制的外部环境问题(用户提供的文件不存在,文件内容损坏,网络不可用...)时,JAVA就会用异常对象来描述。
JAVA中用2种方法处理异常:
1.在发生异常的地方直接处理;
2.将异常抛给调用者,让调用者处理。
JAVA异常可分为3种:
  (1)检查性异常:jav ......

java Bigdecimal的应用

import java.io.*;
import java.math.*;
import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        Scanner cin=new Scanner(System.in);
        BigDecimal a; ......

Java文件操作大全(二)

11.写入属性
//import java.io.*;
File filereadonly=new File(%%1);
try {
boolean b=filereadonly.setReadOnly();
}
catch (Exception e) {
System.out.println("拒绝写访问:"+e.printStackTrace());

12.枚举一个文件夹中的所有文件
//import  ......

Java文件操作大全(六)

39.写入XML数据库
//import java.io.*;
//import javax.xml.parsers.*;
//import org.xml.sax.*;
//import org.w3c.dom.*;
//import javax.xml.transform.*;
//import javax.xml.transform.dom.*;
//import javax.xml.transform.stream.*;
private Document  ......

Java文件操作大全(七)

45.验证Schema
//import java.io.*;
//import javax.xml.parsers.*;
//import org.xml.sax.*;
//import org.w3c.dom.*;
String xsdFileName = \"Q:\\\\_dev_stu\\\\xsdtest\\\\src\\\\note.xsd\"; 
try { 
//创建默认的XML错误处理器 
XMLErrorHan ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号