Java 读取XML
Java 读取XML
糊不糊涂 发表于3年前
Java 读取XML
  • 发表于 3年前
  • 阅读 9
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: 声明类对象,根据Public字段读取xml,读取到对象中

package com.utility;


import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.lang.reflect.Field;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;


import org.apache.log4j.Logger;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.Node;

import org.dom4j.io.SAXReader;



/**

 * 文件配置读取类

 * @author zhangyang 20150603

 *

 */

public class XmlFile {


private static String section="Section";

private static String entry="Entry";

/**

* 获取Document对象

* @param xmlfilepath

* @return

* @throws DocumentException

*/

public static Document getDocument(String xmlfilepath) throws DocumentException{

SAXReader reader = new SAXReader();

Document document = null;

InputStream is=null;

is =ClassPathUtils.getWebrootInputStream(xmlfilepath);

if(is==null){

System.out.println("Error: Read file "+xmlfilepath+" to inputStream is failed!");

return document;

}

document=reader.read(is);

return document;

}

/**

* 获取对象数组

* @param customizerType

* @return

* @throws InstantiationException

* @throws IllegalAccessException

*/

public static List getObjectList(Element root,Class customizerType) throws InstantiationException, IllegalAccessException{

List customizerList=null;

try{

String sectionNode=section+"[@name='"+customizerType.getSimpleName()+"']";

Field[] fields= customizerType.getFields();

customizerList=new ArrayList();

List<Node> list= root.selectNodes(sectionNode);

System.out.println(customizerType.getSimpleName()+":"+list.size());

for(int i=0;i<list.size();i++){

Object customizer= customizerType.newInstance();

if(customizer!=null){

for (Field field : fields) {

Node node= list.get(i);

String entryStr= entry+"[@name='"+field.getName()+"']";

Node entryNode=node.selectSingleNode(entryStr);

if(entryNode!=null){

if(field.getType().equals(String.class)){

field.set(customizer, entryNode.getText());

}

}

}

}

customizerList.add(customizer);

}

}

catch(Exception e){


throw e;

}

return customizerList;

}


/**

* 读取xml

* @param customizer

* @throws IllegalArgumentException

* @throws IllegalAccessException

*/

public static void getFieldValue(Element root,Object customizer) throws IllegalArgumentException, IllegalAccessException{

try{

Class customizerType=customizer.getClass();

Field[] fields= customizerType.getFields();


String sectionNode=section+"[@name='"+customizerType.getSimpleName()+"']";


Node node= root.selectSingleNode(sectionNode);

for (Field field : fields) {

Node entryNode=node.selectSingleNode(entry+"[@name='"+field.getName()+"']");

if(entryNode!=null){

if(field.getType().equals(String.class)){

field.set(customizer, entryNode.getText());

}

}

}

}

catch(Exception e){


throw e;

}

}


}


共有 人打赏支持
粉丝 0
博文 1
码字总数 283
×
糊不糊涂
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: