Error in XML parser with JDOM2 package in android studio (java)

huangapple go评论86阅读模式
英文:

Error in XML parser with JDOM2 package in android studio (java)

问题

此代码应该将xml转换为json,但我在开头遇到了以下错误。SAXBuilder 无法读取 xml 文件。

我在这段代码中使用了 JDOM2 库。

Java 代码:

  1. package com.example.books_xmltojson;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.EditText;
  5. import org.jdom2.Document;
  6. import org.jdom2.input.SAXBuilder;
  7. import java.io.InputStream;
  8. public class MainActivity extends AppCompatActivity {
  9. EditText edittext;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. edittext = findViewById(R.id.edittext);
  15. // SAX builder
  16. InputStream input = getResources().openRawResource(R.raw.books);
  17. SAXBuilder builder = new SAXBuilder();
  18. try {
  19. Document doc = builder.build(input);
  20. } catch (Exception e) {
  21. edittext.setText(e.toString());
  22. }
  23. }
  24. }

XML R.raw.books:

  1. // 资源
  2. <catalog>
  3. <book>
  4. <title>XML Developer's Guide</title>
  5. <genre>Computer</genre>
  6. </book>
  7. <book>
  8. <title>Midnight Rain</title>
  9. <genre>Fantasy</genre>
  10. </book>
  11. </catalog>

错误:

  1. 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:

  1. package com.example.books_xmltojson;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.EditText;
  5. import org.jdom2.Document;
  6. import org.jdom2.input.SAXBuilder;
  7. import java.io.InputStream;
  8. public class MainActivity extends AppCompatActivity {
  9. EditText edittext;
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14. edittext = findViewById(R.id.edittext);
  15. // SAX builder
  16. InputStream input = getResources().openRawResource(R.raw.books);
  17. SAXBuilder builder = new SAXBuilder();
  18. try {
  19. Document doc = builder.build(input);
  20. } catch (Exception e) {
  21. edittext.setText(e.toString());
  22. }
  23. }
  24. }

XML R.raw.books:

  1. // Resources
  2. <catalog>
  3. <book>
  4. <title>XML Developer's Guide</title>
  5. <genre>Computer</genre>
  6. </book>
  7. <book>
  8. <title>Midnight Rain</title>
  9. <genre>Fantasy</genre>
  10. </book>
  11. </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:

  1. SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");
  2. builder.setFeature("http://xml.org/sax/features/external-general-entities", false);

And guess what, ChatGPT solved my problem step by step. it's amazing.

huangapple
  • 本文由 发表于 2023年5月10日 21:21:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定