Newer
Older
dxcgt / app / src / main / java / com / smartdot / cgt / util / ResponseXmlItem.java
wangxitong on 6 Apr 2021 1 KB first commit
package com.smartdot.cgt.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

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

public class ResponseXmlItem {
    Document doc;

    public ResponseXmlItem(String xmlString) throws IOException {
        ByteArrayInputStream byteArrayInputStream;
        try {
            byteArrayInputStream = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
            SAXReader reader = new SAXReader();
            doc = reader.read(byteArrayInputStream);
        } catch (DocumentException e) {
            e.printStackTrace();
            throw new IOException();
        }
    }

    public ResponseXmlItem(InputStream stream) throws IOException {
        try {
            SAXReader reader = new SAXReader();
            doc = reader.read(stream);
        } catch (DocumentException e) {
            e.printStackTrace();
            throw new IOException();
        }
    }

    @SuppressWarnings("unchecked")
    public List < ? extends Node > selectNodes(String xpath) {
        return doc.selectNodes(xpath);
    }

    public Node selectSingleNode(String xpath) {
        return doc.selectSingleNode(xpath);
    }
}