2

我只是按照本教程学习了如何使用 ListView 并在 Java 中解析 XML 数据。但是,由于 XML 数据包含一些特殊字符,因此出现错误。有人可以就这个问题给我建议吗?

这是我的 XML 数据的样子。

<menu>

<item>      
    <id>1</id>
    <question>What's my phone number?</question>
    <answer>my phone number is "000-000-0000".</answer>        
</item>  

<item>      
    <id>2</id>
    <question>What's my fax number?</question>
    <answer>my fax number is "000-000-0000"</answer>        
</item>  

<item>      
    <id>3</id>
    <question>What's my mailing address?</question>
    <answer>my address here</answer>        
</item> 

<item>      
    <id>4</id>
    <question>What's my web address?</question>
    <answer>https://mywebsite.com/Login/Login.aspx?cID=53044&lng=en-US</answer>        
</item> 

</menu>

这是Java类。

public class FaqsQuestionsActivity extends ListActivity {


    static final String URL = "http://myweb.com/faq_list.xml";
    // XML node keys
    static final String KEY_ITEM = "item"; // parent node   
    static final String KEY_ID = "id";
    static final String KEY_QUESTION = "question";
    static final String KEY_ANSWER = "answer";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.faqs_questions);

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

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

            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_QUESTION, parser.getValue(e, KEY_QUESTION ));
            map.put(KEY_ANSWER, parser.getValue(e, KEY_ANSWER));

            menuItems.add(map);
        }

        ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.faqs_question_list,
        new String[] { KEY_QUESTION, KEY_ANSWER  }, new int[] {R.id.question, R.id.answer});


        setListAdapter(adapter);

        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                String question = ((TextView) view.findViewById(R.id.question)).getText().toString();
                String answer = ((TextView) view.findViewById(R.id.answer)).getText().toString();


                Intent in = new Intent(getApplicationContext(), FaqsAnswersActivity.class);

                in.putExtra(KEY_QUESTION, question);
                in.putExtra(KEY_ANSWER, answer);
                startActivity(in);

            }
        });
    }
}
4

1 回答 1

0

您可以在尝试解析 XML 之前尝试对其进行编码:

String encodedXML = URLEncoder.encode(xml);

然后解析。

于 2012-09-25T15:43:37.270 回答