OAF_文件系列5_实现OAF解析XML文件javax.xml.parsers(案例)

20150729 Created By BaoXinjian

一、摘要


通过javax.xml.parsers.DocumentBuilder解析从系统系统导入的XML文件

此过程需要用到一些包方法

1. File:获取XML文件

2. DocumentBuilderFactory / DocumentBuilder : 解析XML文件,获取节点结构

3. 节点值获取

3.1 NodeList:获取文件的节点列表

3.2 Node:获取节点列表中的某一个节点

3.3 Element:获取节点中的某一个子元素,并获取值

 

二、实现分析


1. 创建ImportEmployee.java程序

 

2. 实现解析的主要过程

package bxj.oracle.apps.ap.java;

import java.io.File;
import org.w3c.dom.*;

import javax.xml.parsers.*;
import org.xml.sax.*;
import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class importEmployeeXML {

    //获取节点列表
    private void importEmployeeXML(String xmlType) 
    {
      try 
      {  
        //初始化参数,获取文件
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        String empfilepath = "/u2/VIS/visappl/gavin/employeexml/employeexml.xml";
        empfilepath=empfilepath.replace("/", File.separator);
        
        //获取更节点
        File empxmlfile = new File(empfilepath);
        Document doc = docBuilder.parse (empxmlfile);
        doc.getDocumentElement ().normalize ();
        System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
        
        //获取EMPLOYEE节点列表
        NodeList listOfEmployees = doc.getElementsByTagName(xmlType);
        System.out.println("Employee Record Count="+listOfEmployees.getLength());
         
        //循环根节点列表取值
        for ( int i=0; i < listOfEmployees.getLength(); i++)
        {
          Node empNode = listOfEmployees.item(i);
          if (empNode.getNodeType() == Node.ELEMENT_NODE)
          {  
            System.out.println("*************the "+(i+1)+"th node*************");
            System.out.println("EMPLOYEEID="+getElementValue(empNode,"EMPLOYEEID"));
            System.out.println("EMPLOYEENUM="+getElementValue(empNode,"EMPLOYEENUM"));
            System.out.println("EMPLOYEENAME="+getElementValue(empNode,"EMPLOYEENAME"));
            System.out.println("EMPLOYEETYPE="+getElementValue(empNode,"EMPLOYEETYPE"));
            System.out.println("COUNTRY="+getElementValue(empNode,"COUNTRY"));
            System.out.println("ADDRESS="+getElementValue(empNode,"ADDRESS"));
            System.out.println("DESCRIPTION="+getElementValue(empNode,"DESCRIPTION"));
          }
        }
      }catch (Exception ex) 
      {
        ex.printStackTrace();
      }
    }
    
    //获取节点的值
    private String getElementValue(Node node, String xmlType) {
       String result = null;
       if(node.getNodeType() == Node.ELEMENT_NODE){
            Element firstPersonElement = (Element) node;
            NodeList firstNameList = firstPersonElement.getElementsByTagName(xmlType);
            Element firstNameElement = (Element) firstNameList.item(0);
            if (firstNameElement == null)
            {
                result=  "";
            }
                
            NodeList textFNList = firstNameElement.getChildNodes();
            if (textFNList.item(0) == null)
            {
                result =  "";
            }
            if (((Node)textFNList.item(0)).getNodeValue() == null)
            {
                result = "";
            }
            else 
            {
                result = ((Node)textFNList.item(0)).getNodeValue().trim();
            }
        }
        return result;
    }
    
    //主程序
    public static void main(String[] args){
        System.out.println("gavin main");
        String xmlType = "EMPLOYEE";
        importEmployeeXML importEmployeeXML = new importEmployeeXML();
        importEmployeeXML.importEmployeeXML(xmlType);
    }

}

 

三、运行测试


1. 运行测试

 

2. 创建XML测试数据文件

 

3. 通过system.out.println输出的XML信息数据,读取顺利成功,可以通过其他方法更新到VO或者Database中


Thanks and Regards


posted on 2015-05-05 20:18  东方瀚海  阅读(648)  评论(0编辑  收藏  举报