java - 如何将文档对象转换为输入源?

标签 java xml xerces jdom

我正在动态创建一个 XML 文档对象。我需要根据架构验证它。我正在使用 xerces 2。我已经为解析器设置了功能。现在我需要解析以验证 XML。

为此我需要调用“parser.parse()”。但 parse() 方法采用“InputSource”作为参数。但我有 Document 对象。如何将此 Document 对象转换为“InputSource”以将其传递给 parse() 方法。

谁能帮忙。

最诚挚的问候,

最佳答案

ByteArrayOutputStream docOutputStream = new ByteArrayOutputStream();
((XmlDocument)domDocument).write(docOutputStream);
ByteArrayInputStream docInputStream = new
ByteArrayInputStream(docOutputStream.toByteArray());
InputSource inputSource = new InputSource(docInputStream);
parser.parse(inputSource);

关于java - 如何将文档对象转换为输入源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7727170/

相关文章:

java - ThreadPoolExecutor.shutdownNow() 没有在 Thread 中抛出 InterruptedException

SQL 使用 sys.fn_xe_file_target_read_file 导入扩展事件文件如何仅获取自上次导入以来的值

c++ - 我如何在 Windows 环境中使用 Xerces?

android - LinearLayout 中 TextView 旁边的 ImageView

java - XMLEntityScanner.peekChar 从 HttpRequest 读取 XML 时出现 ArrayIndexOutOfBoundsException

java.lang.ClassCastException : org. apache.xerces.jaxp.SAXParserFactoryImpl 与 javax.xml.parsers.SAXParserFactory 不兼容

java - 在 OS X Lion 上构建 jamvm 1.5.4

java - 在java swing中注册外部字体的多种样式

java - 文件读取未找到文件

java - 选择性编码对象