本文整理汇总了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 ) );
}