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

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

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.

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:

确定