-1

我想通过我的活动从我的内部存储传递一个 xml 文件:我的错误:

07-06 01:58:39.494: E/Error:(15253): Unexpected token (position:TEXT /sdcard/xml.xlm...@1:24 in java.io.StringReader@4152db98) 
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        XMLParserLocal parser = new XMLParserLocal();
        //String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement("storage/emulated/0/xml.xml"); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_COST, parser.getValue(e, KEY_COST));
            map.put(KEY_COST2, parser.getValue(e, KEY_COST2));
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
            map.put(KEY_texton, parser.getValue(e, KEY_texton));
            map.put(KEY_textoff, parser.getValue(e, KEY_textoff));

            // adding HashList to ArrayList
            menuItems.add(map);
        }

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item,
                new String[] { KEY_NAME, KEY_DESC, KEY_ID, KEY_COST, KEY_texton, KEY_textoff}, new int[] {
                        R.id.name, R.id.desciption, R.id.cost, R.id.deviceid,R.id.on,R.id.off });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
                String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
                String deviceid = ((TextView) view.findViewById(R.id.deviceid)).getText().toString();
                String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
                String on = ((Button) view.findViewById(R.id.on)).getText().toString();
                String off = ((Button) view.findViewById(R.id.off)).getText().toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(KEY_NAME, name);
                in.putExtra(KEY_COST, cost);
                in.putExtra(KEY_DESC, description);
                in.putExtra(KEY_ID, deviceid);
                in.putExtra(KEY_texton, on);
                in.putExtra(KEY_textoff, off);
                startActivity(in);

            }
        });

这里是我的 XMLParserLocal:

package de.heron.cloudbox;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

public class XMLParserLocal {

    // constructor
    public XMLParserLocal() {

    }

    /**
     * Getting XML from URL making HTTP request
     * @param url string
     * */
    public String getXmlFromUrl(String url) {
        String xml = "/xml.xml";


        return xml;
    }

    /**
     * Getting XML DOM element
     * @param XML string
     * */
    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }

            return doc;
    }

    /** Getting node value
      * @param elem element
      */
     public final String getElementValue( Node elem ) {
         Node child;
         if( elem != null){
             if (elem.hasChildNodes()){
                 for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                     if( child.getNodeType() == Node.TEXT_NODE  ){
                         return child.getNodeValue();
                     }
                 }
             }
         }
         return "";
     }

     /**
      * Getting node value
      * @param Element node
      * @param key string
      * */
     public String getValue(Element item, String str) {     
            NodeList n = item.getElementsByTagName(str);        
            return this.getElementValue(n.item(0));
        }
}

有人可以帮我解决我的问题吗?

编辑:

这里是我的 xml,我想通过我的 Activity 传递,我在部分 xml 上取空了还有其他词:

    <?xml version="1.0" encoding="utf-8"?>
    <devices>
    <device>
    <deviceid>empty</deviceid>
    <devicename>empty</devicename>
    <actions>empty</actions>
    <actions2>empty</actions2>
    <devicetype>empty</devicetype>
    <texton>empty</texton>
    <textoff>empty</textoff>
    </device>



  <device>
        <deviceid>empty</deviceid>
        <devicename>empty</devicename>
        <actions>empty</actions>
        <actions2>empty</actions2>
        <devicetype>empty</devicetype>
        <texton>empty</texton>
        <textoff>empty</textoff>
        </device>


  </devices>
4

1 回答 1

1

您在上面发布的 XML 无效。你有

</device>
<device>
<device>

它连续有两个打开的设备标签。然后在结束 xml 部分之前打开另一个

<device>
</devices>
于 2013-07-06T00:52:08.620 回答