xml文件如下:
<?xml version="1.0" encoding="UTF-8" ?>
<Report>
<Generator>combit® List & Label® Report Generator</Generator>
<Title>List & Label Report</Title>
<Page Number="1">
<Object Type="Table">
<Lines>
<Line Type="Header">
<Cells>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>编 码</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>数量</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>规格</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>直径</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>A</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>B</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>C</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>R</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>R1</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>总长</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>重量</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>Bar Type</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
</Cells>
</Line>
<Line Type="Body">
<Cells>
<Cell>
<Object Type="Text">
<Paragraphs>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>1</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>HRB400</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>32</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>250.00000</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>7665.00000</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>250.00000</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>90.00000</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>90.00000</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>8165.00000</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Text">
<Paragraphs>
<Paragraph>
<Text>125.8</Text>
</Paragraph>
</Paragraphs>
</Object>
</Cell>
<Cell>
<Object Type="Drawing">
<Image>
<Filename>00000001.jpg</Filename><Size>1818</Size><Width.Pixel>148</Width.Pixel><Height.Pixel>114</Height.Pixel>
</Image>
</Object>
</Cell>
</Cells>
</Line>
</Lines>
</Object>
</Page>
</Report>
java代码解析:
package com.org.tjkmes.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
/**只针对固定标签,xml格式不能改变
* xml解析
* @author wds
*/
public class Jdom2util {
public static String [] getHeadData(File file) {
// 进行对xml文件的JDOM解析
// 准备工作
// 1.创建一个SAXBuilder的对象
SAXBuilder saxBuilder = new SAXBuilder();//注意SAXBuilder是org.jdom2.input包下的
InputStream in;
String [] header=null;
try {
// 2.创建一个输入流,将xml文件加载到输入流中
in=new FileInputStream(file);//如果将xml文件放在src/res包下,此时应该输入“src/res/books.xml”
InputStreamReader isr = new InputStreamReader(in, "UTF-8");//使用包装流InputStreamReader进行读取编码的指定,防止乱码
// 3.通过saxBuilder的build方法,将输入流加载到saxBuilder中
Document document = saxBuilder.build(isr);
// 4.通过document对象获取xml文件的根节点
Element rootElement = document.getRootElement();
// 5.获取根节点下的子节点的List集合
List<Element> proList = rootElement.getChildren();
//❤ 继续解析,采用for循环对proList进行遍历
for (Element pro : proList) {
//只有page里面是有用的,其他的标签就不要了
if(pro.getName().equals("Page")){
List<Element> lineData = (List<Element>) pro.getChild("Object").getChild("Lines").getChildren("Line");
for(Element data :lineData){
int count =0;
List<Element> alldata = data.getChild("Cells").getChildren("Cell");
if(header==null){
header = new String[alldata.size()];
}
if(data.getAttributeValue("Type").equals("Header")){
for(Element nodedata :alldata){
if(nodedata.getChild("Object").getAttributeValue("Type").equals("Text")){
header[count]=nodedata.getChild("Object").getChild("Paragraphs").getChild("Paragraph").getChild("Text").getValue().replaceAll(" ", "");
}
count++;
}
}
}
}else{
continue;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return header;
}
public static List<String []> getBodyData(File file) {
// 进行对xml文件的JDOM解析
// 准备工作
// 1.创建一个SAXBuilder的对象
SAXBuilder saxBuilder = new SAXBuilder();//注意SAXBuilder是org.jdom2.input包下的
InputStream in;
String [] header=null;
List<String []> bodylist=new ArrayList<String[]>();
boolean flag =false;
try {
// 2.创建一个输入流,将xml文件加载到输入流中
in=new FileInputStream(file);//如果将xml文件放在src/res包下,此时应该输入“src/res/books.xml”
InputStreamReader isr = new InputStreamReader(in, "UTF-8");//使用包装流InputStreamReader进行读取编码的指定,防止乱码
// 3.通过saxBuilder的build方法,将输入流加载到saxBuilder中
Document document = saxBuilder.build(isr);
// 4.通过document对象获取xml文件的根节点
Element rootElement = document.getRootElement();
// 5.获取根节点下的子节点的List集合
List<Element> proList = rootElement.getChildren();
//❤ 继续解析,采用for循环对proList进行遍历
for (Element pro : proList) {
String [] body=null;
//只有page里面是有用的,其他的标签就不要了
if(pro.getName().equals("Page")){
List<Element> lineData = (List<Element>) pro.getChild("Object").getChild("Lines").getChildren("Line");
for(Element data :lineData){
int count =0;
List<Element> alldata = data.getChild("Cells").getChildren("Cell");
if(header==null){
header = new String[alldata.size()];
}
if(data.getAttributeValue("Type").equals("Header")){
for(Element nodedata :alldata){
if(nodedata.getChild("Object").getAttributeValue("Type").equals("Text")){
header[count]=nodedata.getChild("Object").getChild("Paragraphs").getChild("Paragraph").getChild("Text").getValue().replaceAll(" ","");
}
count++;
}
}else if (data.getAttributeValue("Type").equals("Body")) {
body = new String[alldata.size()];
for(Element nodedata :alldata){
flag=true;
if(nodedata.getChild("Object").getAttributeValue("Type").equals("Text")){
String str = nodedata.getChild("Object").getChild("Paragraphs").getChild("Paragraph")==null?"":nodedata.getChild("Object").getChild("Paragraphs").getChild("Paragraph").getChild("Text").getValue();
body[count]=header[count]+":"+str;
}
if(nodedata.getChild("Object").getAttributeValue("Type").equals("Drawing")){
List<Element> imagedata = nodedata.getChild("Object").getChild("Image").getChildren();
String str ="[";
for(Element attr :imagedata){
String name = attr.getName();
String value = attr.getValue();
str+=name+":"+value+",";
}
body[count]=header[count]+":"+str.substring(0,str.length()-1)+"]";
}
count++;
}
}
if(flag){
bodylist.add(body);
}
}
}else{
continue;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bodylist;
}
}
数据处理业务层代码如下:
/**
* xml解析
*/
@Override
public void readxmlFile(File file) {
//用于组织表头的哪些字段需要合并在一起储存
String [] headata= Jdom2util.getHeadData(file);
//开始往数据库中插入数据,便于以后的产品处理,同时那些带字母的数字可以用来组织数据类型
List<String[]> bodyDataList = Jdom2util.getBodyData(file);
//开始
//将含有字母r的变量放在一起,同时将其他字母的放一起,其他的不含有字母的单独存储
int count =0;
List<Integer> conRindex = new ArrayList<Integer>();
List<Integer> nconRindex = new ArrayList<Integer>();
List<Integer> othindex = new ArrayList<Integer>();
for(String str:headata){
if(judgeContainsStr(str)){
if(str.contains("R") && !str.equals("BarType")){
//这里记住属性对应的索引编号
conRindex.add(count);
}
if(!str.contains("R") && !str.equals("BarType")){
nconRindex.add(count);
}
if(str.equals("BarType")){
othindex.add(count);
}
}else {
othindex.add(count);
}
count++;
}
//根据名称的集合处理这些data中的数据
for(String [] singlebody:bodyDataList){
//循环插入
Prodata prodata = new Prodata();
prodata.setPkprodata(UUIDUtil.getUUID());
prodata.setDr(0);
//合并角数据
String biandata ="";
String jiaodata ="";
for(int i=0;i<conRindex.size();i++){
if(i!=conRindex.size()-1){
jiaodata+=singlebody[conRindex.get(i)]+",";
}else {
jiaodata+=singlebody[conRindex.get(i)];
}
}
//合并边数据
for(int j=0;j<nconRindex.size();j++){
biandata+=singlebody[nconRindex.get(j)]+",";
}
for(Integer othIndexno : othindex){
String tempstr = singlebody[othIndexno];
String [] tempArr = tempstr.split(":");
if(tempArr.length>2){
tempstr=tempstr.substring(tempArr[0].length()+2,tempstr.length()-1);
}else if(tempArr.length==2){
tempstr=tempArr[tempArr.length-1];
}else if(tempArr.length==1){
tempstr="";
}else {
throw new RuntimeException("数据异常请后台人员检查");
}
if(headata[othIndexno].equals("总长")){
prodata.setZlong(tempstr);
}else if(headata[othIndexno].equals("重量")){
prodata.setZweight(tempstr);
}else if(headata[othIndexno].equals("BarType")){
prodata.setBartype(tempstr);
}else if(headata[othIndexno].equals("编码")){
prodata.setCode(tempstr);
}else if(headata[othIndexno].equals("数量")){
prodata.setNum(tempstr);
}else if(headata[othIndexno].equals("规格")){
prodata.setSpec(tempstr);
}else if(headata[othIndexno].equals("直径")){
prodata.setDiameter(tempstr);
}
}
prodata.setParmdata(biandata+jiaodata);
this.iProdataService.saveProdata(prodata);
}
}
/**
* 使用正则表达式来判断字符串中是否包含字母
* @param str 待检验的字符串
* @return 返回是否包含
* true: 包含字母 ;false 不包含字母
*/
public boolean judgeContainsStr(String str) {
String regex=".*[a-zA-Z]+.*";
Matcher m=Pattern.compile(regex).matcher(str);
return m.matches();
}