使用 DocumentBuilder 解析 InputStreamReader

发布于 2024-12-21 17:32:45 字数 639 浏览 0 评论 0原文

我对 Java 的经验很少。我试图强制将文档读取为 UTF-8,但在尝试将 InputStream 阅读器连接到文档生成器时遇到了困难。

到目前为止,这是我所得到的:

import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;


if( pathToFile == null ) throw new Exception("You must supply a pathToFile parameter");

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

InputStreamReader in = new InputStreamReader( new FileInputStream( pathToFile ), "utf-8" );

BufferedReader reader = new BufferedReader ( new InputStreamReader ( in ) );

Element records = builder.parse(reader).getDocumentElement();

如果有人能为我提供一些指示,我将不胜感激

I've very little Java experience. I'm trying to force a document to be read as UTF-8, but have come stuck trying to hook the InputStream reader to the document builder.

Here's what I have so far:

import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;


if( pathToFile == null ) throw new Exception("You must supply a pathToFile parameter");

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

InputStreamReader in = new InputStreamReader( new FileInputStream( pathToFile ), "utf-8" );

BufferedReader reader = new BufferedReader ( new InputStreamReader ( in ) );

Element records = builder.parse(reader).getDocumentElement();

Be grateful if someone can provide me with some pointers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

俯瞰星空 2024-12-28 17:32:46

不要将 InputStreamReader 包裹在 InputStreamReader 周围。 (编辑此外,由于没有方法从 Reader 解析 XML,因此您需要将 reader 包装在 InputSource):

if( pathToFile == null )
    throw new Exception("You must supply a pathToFile parameter");

DocumentBuilder builder = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder();

InputStreamReader in = new InputStreamReader(
    new FileInputStream( pathToFile ), "utf-8" );

BufferedReader reader = new BufferedReader ( in ); // CHANGED

InputSource input = new InputSource(reader);

Element records = builder.parse(input).getDocumentElement();

Don't wrap an InputStreamReader around your InputStreamReader. (EDIT Also, since there's no method to parse XML from a Reader, you need to wrap the reader in an InputSource):

if( pathToFile == null )
    throw new Exception("You must supply a pathToFile parameter");

DocumentBuilder builder = DocumentBuilderFactory.newInstance()
    .newDocumentBuilder();

InputStreamReader in = new InputStreamReader(
    new FileInputStream( pathToFile ), "utf-8" );

BufferedReader reader = new BufferedReader ( in ); // CHANGED

InputSource input = new InputSource(reader);

Element records = builder.parse(input).getDocumentElement();
彼岸花ソ最美的依靠 2024-12-28 17:32:46

假设这是 Groovy,您可以摆脱很多 Java 的麻烦:

没有尝试过,但是:

if( pathToFile == null ) throw new Exception("You must supply a pathToFile parameter");

Element records = new File( pathToFile ).withReader( "utf-8" ) { r ->
  DocumentBuilderFactory.newInstance().newDocumentBuilder().with { b ->
    b.parse( new InputSource( r ) ).documentElement
  }
}

应该可以...

Assuming this is Groovy, you can rid yourself of a lot of the Java cruft:

Not tried it, but:

if( pathToFile == null ) throw new Exception("You must supply a pathToFile parameter");

Element records = new File( pathToFile ).withReader( "utf-8" ) { r ->
  DocumentBuilderFactory.newInstance().newDocumentBuilder().with { b ->
    b.parse( new InputSource( r ) ).documentElement
  }
}

should work...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文