欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jdom解析

程序员文章站 2022-05-28 10:41:35
...

什么都别说, 直接撸代码: 一个通过xml配置解析,从数据库里面读取相应的数据,通过ftp 发送到指定地方的项目:
下面是XML的样板:

<?xml version="1.0" encoding="UTF-8"?> oracle.jdbc.driver.OracleDriver <![CDATA[jdbc:oracle:thin:@//***********:1521/orcl]]> ****** ***** ********* 22 ****** ******** oracle_17 relay_cloud C:\\yanjingxiang district_******_scale
false
district_*****_scale date district_name ALL_USER STABLE_USER DAILY_USER NIGHT_USER LIVE_USER WORK_USER FLOW_USER

解析分为几种格式: 1 读取根节点:
Document doc = builder.build(xmlPath);//从传入xml文件中提取出doc
Element equipments = doc.getRootElement();//从doc中得到根节点,赋值给equipments
获取子节点:;
Element equipment = equipments.getChild(“config-set”);
获取子节点集合:
List itemList = items.getChildren(“item”);
lamda表达式遍历子节点集合:
itemList.forEach(item->{
Element ItemName = (Element)item;
String name = ItemName.getText();
});
写入的时候同理, 我就不一一撸代码了,下面是我撸的解析上面xml的代码, 因为保密信息, 部分信息,不可见。

public class XMLHelper {
//获取XML数据
public static List readXML(String xmlPath) {
SAXBuilder builder = new SAXBuilder();//选用jdom中的SAXBuilder解析器解析xml16
List list = new ArrayList<>();
try {
Document doc = builder.build(xmlPath);//从传入xml文件中提取出doc
Element equipments = doc.getRootElement();//从doc中得到根节点,赋值给equipments
//List equipmentList = equipments.getChildren(“config-set”);//在equipments中得到名字为equipment的子节点List
Element equipment = equipments.getChild(“config-set”);
Element configEquipment = equipment.getChild(“config”);
String path = configEquipment.getChild(“ftp-path”).getText();
Element items = configEquipment.getChild(“items”);
List itemList = items.getChildren(“item”);
itemList.forEach(item->{
Element ItemName = (Element)item;
String name = ItemName.getText();
equipments.getChild(name);
System.out.println(name);
Element iter = equipments.getChild(name);
// for (Iterator iter = fileList.iterator(); iter.hasNext(); ) {//循环List
Element file = iter.getChild(“file”);
String id = file.getAttributeValue(“id”);
if(id.equals(name)){
ObjectModoul objectModoul = new ObjectModoul();
objectModoul.header = file.getChild(“header”).getText();
objectModoul.fileName = file.getChild(“file-name”).getText();
Element column = file.getChild(“columns”);
objectModoul.path = path;
List itemColumns = column.getChildren(“column”);
Map<String,String > ColumnMap = new HashMap<>();
itemColumns.forEach(Columnitem->{
Element Column = (Element)Columnitem;
ColumnMap.put( Column.getText(),Column.getAttributeValue(“type”));
});

                    objectModoul.clomn = ColumnMap;
                    list.add(objectModoul);
                }
        });
    } catch (JDOMException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return list;
}

}

下面就得看你们根据自己的情况解析代码了。希望能够帮到你。

相关标签: jdom XML 解析

上一篇: set IFS

下一篇: xml解析之JDOM