英文:
Error in XML parser with JDOM2 package in android studio (java)
问题
此代码应该将xml
转换为json,但我在开头遇到了以下错误。SAXBuilder
无法读取 xml
文件。
我在这段代码中使用了 JDOM2 库。
Java 代码:
package com.example.books_xmltojson;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
EditText edittext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext = findViewById(R.id.edittext);
// SAX builder
InputStream input = getResources().openRawResource(R.raw.books);
SAXBuilder builder = new SAXBuilder();
try {
Document doc = builder.build(input);
} catch (Exception e) {
edittext.setText(e.toString());
}
}
}
XML R.raw.books:
// 资源
<catalog>
<book>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
</book>
<book>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
</book>
</catalog>
错误:
org.jdom2.JDOMException: http://xml.org/sax/features/external-general-entities feature http://xml.org/sax/features/external-general-entities not supported for SAX driver org.apache.harmony.xml.ExpatReader
英文:
This code should convert xml
to json and i got below error in the first of it.
SAXBuilder
can't read xml
file.
I use JDOM2 library in this code.
Java code:
package com.example.books_xmltojson;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
EditText edittext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edittext = findViewById(R.id.edittext);
// SAX builder
InputStream input = getResources().openRawResource(R.raw.books);
SAXBuilder builder = new SAXBuilder();
try {
Document doc = builder.build(input);
} catch (Exception e) {
edittext.setText(e.toString());
}
}
}
XML R.raw.books:
// Resources
<catalog>
<book>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
</book>
<book>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
</book>
</catalog>
The error:
org.jdom2.JDOMException: http://xml.org/sax/features/external-general-entities feature http://xml.org/sax/features/external-general-entities not supported for SAX driver org.apache.harmony.xml.ExpatReader
答案1
得分: -1
我首先必须将 xercesImpl.jar
添加到我的项目中。
然后,我必须将 SAXBuilder
更改如下:
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
而且,猜猜,ChatGPT
逐步解决了我的问题。真是令人惊奇。
英文:
First i had to implement xercesImpl.jar
to my project.
Then i had to change the SAXBuilder
as below:
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
builder.setFeature("http://xml.org/sax/features/external-general-entities", false);
And guess what, ChatGPT
solved my problem step by step. it's amazing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论