Example usage for org.dom4j.io SAXReader read

List of usage examples for org.dom4j.io SAXReader read

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader read.

Prototype

public Document read(InputSource in) throws DocumentException 

Source Link

Document

Reads a Document from the given InputSource using SAX

Usage

From source file:com.safi.workshop.sqlexplorer.history.SQLHistory.java

License:Open Source License

private void loadFromFileV350() {
    try {/*from  w  w w.j ava  2s .  c  om*/
        File file = new File(ApplicationFiles.SQLHISTORY_FILE_NAME_V350);
        SAXReader reader = new SAXReader();
        Element root = reader.read(file).getRootElement();
        _history.clear();
        _filteredHistory.clear();
        for (Object o : root.elements(SQLHistoryElement.ELEMENT)) {
            _history.add(new SQLHistoryElement((Element) o));
        }

    } catch (DocumentException e) {
        SQLExplorerPlugin.error("Cannot load history (v3.5.0 format)", e);
    }
}

From source file:com.safi.workshop.sqlexplorer.preview.XmlPreviewer.java

License:Open Source License

private Element getXml(Object data) throws ExplorerException {
    try {// ww w .  j  av a  2s . c om
        if (data == null)
            return null;

        if (data instanceof XmlDataType)
            return ((XmlDataType) data).getRootElement();

        String text = data.toString();
        if (text == null)
            return null;

        SAXReader reader = new SAXReader();
        return reader.read(new StringReader(text)).getRootElement();
    } catch (DocumentException e) {
        throw new ExplorerException(e);
    }
}

From source file:com.sap.data.db.dao.StructureUtil.java

private Document getDocument(String filePath) throws DocumentException, FileNotFoundException, SAXException {
    InputStream inputStream = null;
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        inputStream = classLoader.getResourceAsStream(filePath);
    }/*w ww .j  a va 2s.  c  o  m*/
    SAXReader reader = new SAXReader();
    reader.setFeature(this.NONVALIDATING_DTD, false);
    Document document = reader.read(inputStream);
    return document;
}

From source file:com.seer.datacruncher.utils.generic.StreamsUtils.java

License:Open Source License

/**
 * Gets xml text nodes for stream./*from w w w  . j  a  va 2 s .  c  o  m*/
 * 
 * @param streamXml  
 * @return text node list
 * @throws DocumentException
 */
public static List<Element> parseStreamXml(String streamXml) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(streamXml));
    List<Element> xmlTextNodes = new ArrayList<Element>();
    getXmlChilds(document.getRootElement(), xmlTextNodes);
    return xmlTextNodes;
}

From source file:com.sharksharding.sql.SQLTemplate.java

License:Apache License

/**
 * dom4j?xml/*from w ww . j  a  va  2 s  .  c  om*/
 * 
 * @author gaoxianglong
 * 
 * @param in
 *            ?
 * 
 * @exception XmlResolveException
 * 
 * @return void
 */
@SuppressWarnings("unchecked")
private void resolveXml(InputStream in) {
    if (null == in)
        return;
    Document document = null;
    SAXReader saxReader = new SAXReader();
    try {
        document = saxReader.read(in);
    } catch (DocumentException e) {
        throw new XmlResolveException("xml resolve fail");
    }
    Element root = document.getRootElement();
    List<Element> elements = root.selectNodes(xpathExpression);
    if (!elements.isEmpty()) {
        for (Element element : elements) {
            final String sql = element.attribute(name).getValue();
            sqlMap.put(sql, element.getText());
        }
    } else {
        throw new XmlResolveException("element <sql/> not found");
    }
}

From source file:com.shopximity.jpt.PageTemplateImpl.java

License:Open Source License

public PageTemplateImpl(URL url, SAXParser saxParser) throws PageTemplateException {
    try {/*from  ww w  . j a va2 s .  co  m*/
        this.url = url;
        SAXReader reader = new SAXReader(saxParser);
        template = reader.read(url);
    } catch (Exception e) {
        throw new PageTemplateException(e);
    }
}

From source file:com.sjdf.platform.attachment.helper.AttachmentHelper.java

/**
 * ?XMLVO List/*from ww  w. j  a v  a 2  s  .  co  m*/
 *
 * @param xml XML
 * @return VO List
 */
@SuppressWarnings("unchecked")
public static List<AttachmentHouseVo> parse(String xml) throws Exception {
    List<AttachmentHouseVo> attachmentVoList = new ArrayList<>();
    SAXReader saxReader = new SAXReader();
    try (InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"))) {
        Document document = saxReader.read(is);

        Node node = document.selectSingleNode("/error");
        if (node != null) {
            LOGGER.error(xml);
            throw new RuntimeException(((Element) node).getData().toString());
        }

        Element root = (Element) document.selectSingleNode("/attachmentList");
        List<Element> xmlDictionaryList = root.elements();
        if (xmlDictionaryList != null && !xmlDictionaryList.isEmpty()) {
            for (Element element : xmlDictionaryList) {
                List<Element> xmlDictionary = element.elements();

                AttachmentHouseVo attachmentHouseVo = new AttachmentHouseVo();
                for (Element e : xmlDictionary) {
                    if (e.elements().isEmpty() && (Tools.isEmpty(e.getData().toString())
                            || "null".equals(e.getData().toString()))) {
                        continue;
                    }
                    if ("path".equals(e.getName())) {
                        attachmentHouseVo.setPath(e.getData().toString());
                    } else if ("userId".equals(e.getName())) {
                        attachmentHouseVo.setUserId(Long.valueOf(e.getData().toString()));
                    } else if ("attachmentUseCode".equals(e.getName())) {
                        attachmentHouseVo.setAttachmentUseCode(Long.valueOf(e.getData().toString()));
                    } else if ("attachmentUseType".equals(e.getName())) {
                        attachmentHouseVo.setAttachmentUseType(Long.valueOf(e.getData().toString()));
                    } else if ("systemType".equals(e.getName())) {
                        attachmentHouseVo.setSystemType(Long.valueOf(e.getData().toString()));
                    } else if ("markDelete".equals(e.getName())) {
                        attachmentHouseVo.setMarkDelete(Long.valueOf(e.getData().toString()));
                    } else if ("remark".equals(e.getName())) {
                        attachmentHouseVo.setRemark(e.getData().toString());
                    } else if ("byte".equals(e.getName())) {
                        attachmentHouseVo.setAttachmentFileString(e.getData().toString());
                    } else if ("username".equals(e.getName())) {
                        attachmentHouseVo.setUsername(e.getData().toString());
                    } else if ("whetherChanged".equals(e.getName())) {
                        attachmentHouseVo.setWhetherChanged(Long.valueOf(e.getData().toString()));
                    } else if ("format".equals(e.getName())) {
                        attachmentHouseVo.setFormat(Long.valueOf(e.getData().toString()));
                    } else if ("sourcePath".equals(e.getName())) {
                        attachmentHouseVo.setSourcePath(e.getData().toString());
                    } else if ("subSystemPath".equals(e.getName())) {
                        attachmentHouseVo.setSubSystemPath(e.getData().toString());
                    } else if ("attachmentId".equals(e.getName())) {
                        attachmentHouseVo.setId(Long.valueOf(e.getData().toString()));
                    } else if ("auditResult".equals(e.getName())) {
                        attachmentHouseVo.setAuditResult(Long.valueOf(e.getData().toString()));
                    } else if ("fontColor".equals(e.getName())) {
                        attachmentHouseVo.setFontColor(Long.valueOf(e.getData().toString()));
                    } else if ("createTime".equals(e.getName())) {
                        attachmentHouseVo.setBeginDate(e.getData().toString());
                    } else if ("updateTime".equals(e.getName())) {
                        attachmentHouseVo.setEndDate(e.getData().toString());
                    } else if ("attachmentUseTypeMark".equals(e.getName())) {
                        attachmentHouseVo.setAttachmentUseTypeMark(e.getData().toString());
                    } else if ("attachmentSpecialMark".equals(e.getName())) {
                        //? element?
                        List<Element> ppgjzElementList = e.elements();
                        String specialMark;
                        if (ppgjzElementList.isEmpty() && !Tools.isEmpty(e.getData().toString())) {
                            specialMark = e.getData().toString();
                        } else {
                            specialMark = parseAttachmentSpecialMark(attachmentHouseVo.getAttachmentUseType(),
                                    attachmentHouseVo.getAttachmentUseCode(), ppgjzElementList);
                        }
                        attachmentHouseVo.setAttachmentSpecialMark(specialMark);
                    }
                }
                attachmentVoList.add(attachmentHouseVo);
            }
        }
    }
    return attachmentVoList;
}

From source file:com.smartwork.im.utils.XMLProperties.java

License:Open Source License

/**
 * Builds the document XML model up based the given reader of XML data.
 *//*from   w w w . j a  v a2s  .  co  m*/
private void buildDoc(Reader in) throws IOException {
    try {
        SAXReader xmlReader = new SAXReader();
        document = xmlReader.read(in);
    } catch (Exception e) {
        logger.error("Error reading XML properties", e);
        throw new IOException(e.getMessage());
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.ssy.havefunweb.util.MessageUtil.java

public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
    Map<String, String> map = new HashMap<>();
    SAXReader reader = new SAXReader();

    InputStream ins = request.getInputStream();
    Document doc = reader.read(ins);
    Element root = doc.getRootElement();

    List<Element> list = root.elements();

    list.stream().forEach((e) -> {/*from  ww w.j  a  v a 2s  . com*/
        map.put(e.getName(), e.getText());
    });

    ins.close();
    return map;
}

From source file:com.stgmastek.core.util.XMLUtil.java

License:Open Source License

/**
 * Returns the response data object//w w w  . ja  v a 2s  . c o  m
 * 
 * @param responseString
 *         The response string.
 * 
 * @return List of Element instances from data tag.
 * 
 * @throws Exception 
 */
public static List<org.w3c.dom.Element> getData(String responseString) throws Exception {

    DocumentFactory factory = new org.dom4j.dom.DOMDocumentFactory();
    SAXReader reader = new SAXReader(factory);
    List<org.w3c.dom.Element> elements = new ArrayList<org.w3c.dom.Element>();
    try {
        Document responseDocument = reader.read(new StringReader(responseString));
        org.dom4j.Node dataNode = responseDocument.selectSingleNode(DATA_XPATH);

        if ((dataNode != null) && (dataNode.getNodeType() == Node.ELEMENT_NODE)) {
            Element dataElement = (Element) dataNode;
            for (Iterator<?> nodeIterator = dataElement.elementIterator(); nodeIterator.hasNext();) {
                Element objectNode = (Element) nodeIterator.next();
                elements.add(DOMNodeHelper.asDOMElement(objectNode));
            }
        }

    } catch (DocumentException e) {
        throw new Exception("Invalid response", e);
    }
    return elements;
}