All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alilitech.generate.XmlParser Maven / Gradle / Ivy

/*
 *    Copyright 2017-2021 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.alilitech.generate;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Zhou Xiaoxiang
 * @since 1.0
 */
public class XmlParser {

    private Document document;

    public XmlParser(String xmlPath) {
        //获取解析器
        SAXReader saxReader = new SAXReader();
        try {
            //获取文档对象
            document = saxReader.read(xmlPath);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }

    public XmlParser(InputStream inputStream) {
        //获取解析器
        SAXReader saxReader = new SAXReader();
        try {
            //获取文档对象
            document = saxReader.read(inputStream);
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }

    public  T parseText(String elementPath, Class clazz)  {

        String[] nodeArray = elementPath.split("\\.");
        Element element = null;
        for(int i=0; i T parseAttribute(String elementPath, Class clazz) {

        String[] nodeArray = elementPath.split("\\.");
        Element element = null;
        for(int i=0; i List parseListAttribute(String elementPath, Class clazz)  {
        List retList = new ArrayList<>();
        String[] nodeArray = elementPath.split("\\.");
        Element element = null;
        List elements = null;
        for(int i=0; i= nodeArray.length - 1){
                elements = element.elements(nodeArray[i]);
            } else {
                element = element.element(nodeArray[i]);
            }
        }
        Field[] declaredFields = clazz.getDeclaredFields();
        for(Element temp : elements) {
            T t;
            try {
                t = clazz.newInstance();
                for(Field field : declaredFields) {
                    String text = temp.attributeValue(field.getName());
                    if(text == null) {
                        continue;
                    }
                    field.setAccessible(true);
                    field.set(t, covertFieldValue(text, field));
                }
                retList.add(t);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return retList;
    }

    private Object covertFieldValue(String value, Field field) {
        if(field.getType().equals(String.class)) {
            return value;
        } else if(field.getType().equals(Boolean.class) || field.getType().equals(boolean.class)) {
            return Boolean.parseBoolean(value);
        }
        return value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy