原生java读取存储为xml格式的数据,并存储到java bean里

一、举例读取的文件为:X-bond可交易债券信息_20180917.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--X-Bond可交易债券信息-->
<IMIXML>
  <SecDef Desc="国债" CentraQuoteBondIndic="Y">
    <Instrmt ID="CFT" Sym="现券买卖"/>
  </SecDef>
</IMIXML>

二、开始读取准备

在rdi.monitor.bean目录下新建  SecDef.java Instrmt.java

package rdi.monitor.bean;

import java.util.List;

public class SecDef {
    private String desc;//
    private String CentraQuoteBondIndic;

    private String highYldBondType;
    private String maxQteSpread;

    private String securityDefineMsgType;
    private String ccy;

    private String status;
    private String trdPubInd;

    private String txt;

    private Instrmt instrmt;
    private List<BondIndicatorGrp> bondIndicatorGrp;
    private Pty pty;

    public SecDef() {

    }

    public SecDef(String desc, String centraQuoteBondIndic,
            String highYldBondType, String maxQteSpread,
            String securityDefineMsgType, String ccy, String status,
            String trdPubInd, String txt, Instrmt instrmt,
            List<BondIndicatorGrp> bondIndicatorGrp, Pty pty) {
        super();
        this.desc = desc;
        CentraQuoteBondIndic = centraQuoteBondIndic;
        this.highYldBondType = highYldBondType;
        this.maxQteSpread = maxQteSpread;
        this.securityDefineMsgType = securityDefineMsgType;
        this.ccy = ccy;
        this.status = status;
        this.trdPubInd = trdPubInd;
        this.txt = txt;
        this.instrmt = instrmt;
        this.bondIndicatorGrp = bondIndicatorGrp;
        this.pty = pty;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getCentraQuoteBondIndic() {
        return CentraQuoteBondIndic;
    }

    public void setCentraQuoteBondIndic(String centraQuoteBondIndic) {
        CentraQuoteBondIndic = centraQuoteBondIndic;
    }

    public String getHighYldBondType() {
        return highYldBondType;
    }

    public void setHighYldBondType(String highYldBondType) {
        this.highYldBondType = highYldBondType;
    }

    public String getMaxQteSpread() {
        return maxQteSpread;
    }

    public void setMaxQteSpread(String maxQteSpread) {
        this.maxQteSpread = maxQteSpread;
    }

    public String getSecurityDefineMsgType() {
        return securityDefineMsgType;
    }

    public void setSecurityDefineMsgType(String securityDefineMsgType) {
        this.securityDefineMsgType = securityDefineMsgType;
    }

    public String getCcy() {
        return ccy;
    }

    public void setCcy(String ccy) {
        this.ccy = ccy;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getTrdPubInd() {
        return trdPubInd;
    }

    public void setTrdPubInd(String trdPubInd) {
        this.trdPubInd = trdPubInd;
    }

    public String getTxt() {
        return txt;
    }

    public void setTxt(String txt) {
        this.txt = txt;
    }

    public Instrmt getInstrmt() {
        return instrmt;
    }

    public void setInstrmt(Instrmt instrmt) {
        this.instrmt = instrmt;
    }

    public List<BondIndicatorGrp> getBondIndicatorGrp() {
        return bondIndicatorGrp;
    }

    public void setBondIndicatorGrp(List<BondIndicatorGrp> bondIndicatorGrp) {
        this.bondIndicatorGrp = bondIndicatorGrp;
    }

    public Pty getPty() {
        return pty;
    }

    public void setPty(Pty pty) {
        this.pty = pty;
    }

    @Override
    public String toString() {
        return "SecDef [desc=" + desc + ", CentraQuoteBondIndic="
                + CentraQuoteBondIndic + ", highYldBondType=" + highYldBondType
                + ", maxQteSpread=" + maxQteSpread + ", securityDefineMsgType="
                + securityDefineMsgType + ", ccy=" + ccy + ", status=" + status
                + ", trdPubInd=" + trdPubInd + ", txt=" + txt + ", instrmt="
                + instrmt + ", bondIndicatorGrp=" + bondIndicatorGrp + ", pty="
                + pty + "]";
    }

}
SecDef.java
package rdi.monitor.bean;


public class Instrmt {
    private String id;
    private String sym;

    public Instrmt() {
        super();
    }

    public Instrmt(String id, String sym) {
        super();
        this.id = id;
        this.sym = sym;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSym() {
        return sym;
    }

    public void setSym(String sym) {
        this.sym = sym;
    }

    @Override
    public String toString() {
        return "Instrmt [id=" + id + ", sym=" + sym + "]";
    }

}
Instrmt.java

在rdi.monitor.download下新建 LoadFromXMLXBondTransactableBondInformation.java

package rdi.monitor.download;

import java.util.HashMap;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import rdi.monitor.bean.Instrmt;
import rdi.monitor.bean.SecDef;

/**
 * ODM可交易债券列表(X-bond可交易债券信息)
 * 二期实现
 */
public class LoadFromXMLXBondTransactableBondInformation extends DefaultHandler {
    public static SAXParserFactory parserFactory;
    public static SAXParser parser;
    public static HashMap<String, SecDef> xBondTransactableBondInformation_ResultDataForMap = new HashMap<String, SecDef>();
    String xBondTransactableBondInformation_Id; // 债券代码
    SecDef secdef;
    Instrmt instrmt;
    public static Logger LOG = LoggerFactory
            .getLogger(LoadFromXMLXBondTransactableBondInformation.class);

    @Override
    public void startDocument() throws SAXException {

    }

    // 读取XML的<后开始执行
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // 高收益债券信息
        if ("SecDef".equals(qName)) {
            secdef = new SecDef();
            instrmt = new Instrmt();
            secdef.setCentraQuoteBondIndic(attributes
                    .getValue("CentraQuoteBondIndic"));
            secdef.setDesc(attributes.getValue("Desc"));
            secdef.setInstrmt(instrmt);
        }
        if ("Instrmt".equals(qName)) {
            xBondTransactableBondInformation_Id = attributes.getValue("ID");
            instrmt.setId(attributes.getValue("ID"));
            instrmt.setSym(attributes.getValue("Sym"));
        }

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
    }

    // 读取XML的</后开始执行
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        xBondTransactableBondInformation_ResultDataForMap.put(
                xBondTransactableBondInformation_Id, secdef);
    }

    @Override
    public void endDocument() throws SAXException {

    }

    /**
     * 解析方法
     * 
     * @param need
     *            to analysis
     * @return map
     */
    public static HashMap<String, SecDef> loadXML(String filePath) {
        try {
            parserFactory = SAXParserFactory.newInstance();
            parser = parserFactory.newSAXParser();
            LoadFromXMLXBondTransactableBondInformation reader = new LoadFromXMLXBondTransactableBondInformation();
            parser.parse(new InputSource(filePath), reader);
        } catch (Exception e) {
            LOG.info("Parsing XML is failed", e);
            e.printStackTrace();
            return null;
        }
        return xBondTransactableBondInformation_ResultDataForMap;
    }

    public static HashMap<String, SecDef> hashMapXBondTransactableBondInformation;

    public static void main(String[] args) {
        hashMapXBondTransactableBondInformation = loadXML("data/download/X-Bond可交易债券信息_20180904.xml");
        for (String key : hashMapXBondTransactableBondInformation.keySet()) {
            System.out.print("key=" + key + "\t");
            System.out.println("value="
                    + hashMapXBondTransactableBondInformation.get(key));
        }
    }

}
LoadFromXMLXBondTransactableBondInformation.java

猜你喜欢

转载自www.cnblogs.com/zhanzhuang/p/9660645.html