处理XML数据的特殊字符以在Java中解析

时间:2012-09-25 15:27:39

标签: java android xml

我只是学习了如何使用ListView并在此tutorial之后解析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);

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

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

String encodedXML = URLEncoder.encode(xml);

然后解析。