當前位置: 首頁>>代碼示例>>Java>>正文


Java SAX2DOMEx類代碼示例

本文整理匯總了Java中com.sun.xml.bind.marshaller.SAX2DOMEx的典型用法代碼示例。如果您正苦於以下問題:Java SAX2DOMEx類的具體用法?Java SAX2DOMEx怎麽用?Java SAX2DOMEx使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


SAX2DOMEx類屬於com.sun.xml.bind.marshaller包,在下文中一共展示了SAX2DOMEx類的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: marshal

import com.sun.xml.bind.marshaller.SAX2DOMEx; //導入依賴的package包/類
public void marshal(Object obj, Result result) throws JAXBException {
    //XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);

    if(so==null)
        throw new MarshalException( 
            Messages.format( Messages.NOT_MARSHALLABLE ) );


    if (result instanceof SAXResult) {
        write(so, ((SAXResult) result).getHandler());
        return;
    }
    if (result instanceof DOMResult) {
        Node node = ((DOMResult) result).getNode();

        if (node == null) {
            try {
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                dbf.setNamespaceAware(true);
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document doc = db.newDocument();
                ((DOMResult) result).setNode(doc);
                write(so, new SAX2DOMEx(doc));
            } catch (ParserConfigurationException pce) {
                throw new JAXBAssertionError(pce);
            }
        } else {
            write(so, new SAX2DOMEx(node));
        }

        return;
    }
    if (result instanceof StreamResult) {
        StreamResult sr = (StreamResult) result;
        XMLWriter w = null;

        if (sr.getWriter() != null)
            w = createWriter(sr.getWriter());
        else if (sr.getOutputStream() != null)
            w = createWriter(sr.getOutputStream());
        else if (sr.getSystemId() != null) {
            String fileURL = sr.getSystemId();

            if (fileURL.startsWith("file:///")) {
                if (fileURL.substring(8).indexOf(":") > 0)
                    fileURL = fileURL.substring(8);
                else
                    fileURL = fileURL.substring(7);
            } // otherwise assume that it's a file name

            try {
                w = createWriter(new FileOutputStream(fileURL));
            } catch (IOException e) {
                throw new MarshalException(e);
            }
        }

        if (w == null)
            throw new IllegalArgumentException();

        write(so, w);
        return;
    }

    // unsupported parameter type
    throw new MarshalException( 
        Messages.format( Messages.UNSUPPORTED_RESULT ) );
}
 
開發者ID:nhrdl,項目名稱:javacash,代碼行數:70,代碼來源:MarshallerImpl.java


注:本文中的com.sun.xml.bind.marshaller.SAX2DOMEx類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。