Search in sources :

Example 86 with DocumentException

use of org.dom4j.DocumentException in project EssayJoke by qiyei2015.

the class Dom4jHelper method parseXml.

/**
 * 解析xml
 * @param inputStream
 */
public static void parseXml(InputStream inputStream) {
    try {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(inputStream);
        Element element = document.getRootElement();
        readeNode(element, "");
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document)

Example 87 with DocumentException

use of org.dom4j.DocumentException in project jbosstools-hibernate by jbosstools.

the class ResourceReadUtils method adjustXmlText.

/**
 * Parse, i.e. adjust xml text so attributes for same xmls
 * will be in one order.
 *
 * @param sample
 * @return adjusted xml
 */
public static String adjustXmlText(String sample) {
    Document doc = null;
    try {
        doc = DocumentHelper.parseText(sample);
    } catch (DocumentException e) {
    // ignore
    }
    if (doc == null) {
        return sample;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ConfigurationXMLFactory.dump(baos, doc.getRootElement());
    return baos.toString().trim();
}
Also used : DocumentException(org.dom4j.DocumentException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.dom4j.Document)

Example 88 with DocumentException

use of org.dom4j.DocumentException in project core by craftercms.

the class AbstractFileBasedContentStoreAdapter method doFindItem.

@Override
protected Item doFindItem(Context context, CachingOptions cachingOptions, String path, boolean withDescriptor) throws InvalidContextException, PathNotFoundException, XmlFileParseException, StoreException {
    path = normalizePath(path);
    File file = findFile(context, path);
    if (file == null) {
        return null;
    }
    Item item = new Item();
    item.setName(file.getName());
    item.setUrl(path);
    item.setFolder(file.isDirectory());
    if (withDescriptor) {
        File descriptorFile;
        // a DOM.
        if (file.isFile() && item.getName().endsWith(descriptorFileExtension)) {
            item.setDescriptorUrl(path);
            descriptorFile = file;
        // If it's not a file (a dir) or is not a descriptor (a static asset, like an image), locate the file's
        // descriptor by appending a metadata file extension to the file name. If the file exists, load it as
        // a DOM.
        } else {
            String descriptorPath = FilenameUtils.removeExtension(path) + metadataFileExtension;
            item.setDescriptorUrl(descriptorPath);
            descriptorFile = findFile(context, descriptorPath);
            if (descriptorFile != null && !descriptorFile.isFile()) {
                throw new StoreException("Descriptor file at " + descriptorFile + " is not really a file");
            }
        }
        if (descriptorFile != null) {
            try {
                InputStream fileInputStream = new BufferedInputStream(descriptorFile.getInputStream());
                Reader fileReader = new InputStreamReader(fileInputStream, charset);
                try {
                    item.setDescriptorDom(createXmlReader().read(fileReader));
                } finally {
                    IOUtils.closeQuietly(fileReader);
                }
            } catch (IOException e) {
                throw new StoreException("Unable to open input stream for descriptor file at " + descriptorFile, e);
            } catch (DocumentException e) {
                throw new XmlFileParseException("Error while parsing xml document at " + descriptorFile, e);
            }
        }
    }
    return item;
}
Also used : Item(org.craftercms.core.service.Item) InputStreamReader(java.io.InputStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) DocumentException(org.dom4j.DocumentException) SAXReader(org.dom4j.io.SAXReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) StoreException(org.craftercms.core.exception.StoreException) XmlFileParseException(org.craftercms.core.exception.XmlFileParseException)

Example 89 with DocumentException

use of org.dom4j.DocumentException in project core by craftercms.

the class Dom4JDocumentJsonSerializerTest method setUpTestDocument.

private void setUpTestDocument() throws SAXException {
    SAXReader reader = new SAXReader();
    reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
    reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
    reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    try {
        document = reader.read(new StringReader(XML));
    } catch (DocumentException e) {
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader)

Example 90 with DocumentException

use of org.dom4j.DocumentException in project summer by foxsugar.

the class PayUtil method readStringXmlOut.

/**
 * @description 将xml字符串转换成map
 * @param xml
 * @return Map
 */
public static Map readStringXmlOut(String xml) {
    Map map = new HashMap();
    Document doc = null;
    try {
        // 将字符串转为XML
        doc = DocumentHelper.parseText(xml);
        // 获取根节点
        Element rootElt = doc.getRootElement();
        // 拿到根节点的名称
        System.out.println("根节点:" + rootElt.getName());
        // 获取根节点下的子节点head
        Iterator iter = rootElt.elementIterator("head");
        // 遍历head节点
        while (iter.hasNext()) {
            Element recordEle = (Element) iter.next();
            // 拿到head节点下的子节点title值
            String title = recordEle.elementTextTrim("title");
            System.out.println("title:" + title);
            map.put("title", title);
            // 获取子节点head下的子节点script
            Iterator iters = recordEle.elementIterator("script");
            // 遍历Header节点下的Response节点
            while (iters.hasNext()) {
                Element itemEle = (Element) iters.next();
                // 拿到head下的子节点script下的字节点username的值
                String username = itemEle.elementTextTrim("username");
                String password = itemEle.elementTextTrim("password");
                System.out.println("username:" + username);
                System.out.println("password:" + password);
                map.put("username", username);
                map.put("password", password);
            }
        }
        // 获取根节点下的子节点body
        Iterator iterss = rootElt.elementIterator("body");
        // 遍历body节点
        while (iterss.hasNext()) {
            Element recordEless = (Element) iterss.next();
            // 拿到body节点下的子节点result值
            String result = recordEless.elementTextTrim("result");
            System.out.println("result:" + result);
            // 获取子节点body下的子节点form
            Iterator itersElIterator = recordEless.elementIterator("form");
            // 遍历Header节点下的Response节点
            while (itersElIterator.hasNext()) {
                Element itemEle = (Element) itersElIterator.next();
                // 拿到body下的子节点form下的字节点banlce的值
                String banlce = itemEle.elementTextTrim("banlce");
                String subID = itemEle.elementTextTrim("subID");
                System.out.println("banlce:" + banlce);
                System.out.println("subID:" + subID);
                map.put("result", result);
                map.put("banlce", banlce);
                map.put("subID", subID);
            }
        }
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return map;
}
Also used : Element(org.dom4j.Element) DocumentException(org.dom4j.DocumentException) Document(org.dom4j.Document) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

DocumentException (org.dom4j.DocumentException)123 Document (org.dom4j.Document)80 SAXReader (org.dom4j.io.SAXReader)73 Element (org.dom4j.Element)51 IOException (java.io.IOException)45 File (java.io.File)27 InputStream (java.io.InputStream)21 StringReader (java.io.StringReader)15 InputStreamReader (java.io.InputStreamReader)11 ArrayList (java.util.ArrayList)10 XMLWriter (org.dom4j.io.XMLWriter)9 InputSource (org.xml.sax.InputSource)9 FileInputStream (java.io.FileInputStream)7 URL (java.net.URL)7 List (java.util.List)7 Node (org.dom4j.Node)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 FileNotFoundException (java.io.FileNotFoundException)5 Reader (java.io.Reader)5