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
ERP技术讨论群: 288307890
技术交流,技术讨论,欢迎加入
Technology Blog Created By Oracle ERP - 鲍新建
技术交流,技术讨论,欢迎加入
Technology Blog Created By Oracle ERP - 鲍新建
标签:
OAF
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?