gpt4 book ai didi

java - 从资源加载和解析 xml 时出现问题

转载 作者:行者123 更新时间:2023-11-30 04:46:27 25 4
gpt4 key购买 nike

我已经编写了一个解析器,它从 HttpURLConnection 解析一个 xml 文件。这很好用。

问题:我需要重写这个,以便从本地资源而不是从 Internet 加载 xml 文件,但我无法让它工作...只是让您了解原始 Web 解析器的外观:

InputStream in=null;

URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();

if (responseCode == HttpURLConnection.HTTP_OK) {


in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

Document dom = db.parse(in);
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);

//START PARSING......

现在这是我用来尝试从位于资源文件夹中的 xml/myfile.xml 中的本地资源进行解析的代码:

InputStream in=null;
in = mParentActivity.getResources().openRawResource(R.xml.myfile);

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

Document dom = builder.parse(in); // THIS IS WHERE I GET AN SAXParseException

Element root = dom.getDocumentElement();
NodeList nl = root.getChildNodes();

for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);

//START PARSING......

本地的xml文件和web文件完全一样...如果有人看一下:http://agens.no/Eniro/Android/SEWheel/Category2.plist.xml

这是堆栈跟踪:

02-01 16:08:45.546: WARN/System.err(19703): org.xml.sax.SAXParseException: name expected (position:START_TAG @1:309 in java.io.InputStreamReader@47668728)

最佳答案

找到答案.. 当文件位于 res/xml 文件夹中时,输入流显示大量无效字符。当我把它放在 res/raw 文件夹中时,它工作正常。

关于java - 从资源加载和解析 xml 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4864273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com