如何解码android中的xml字符串?

时间:2013-09-23 08:39:37

标签: android decode

在我的xml中,我有<ns5:Name>mil &amp; carbon</ns5:Name> 从我的java代码我正在通过

检索该值
public class MasterdataParser extends MasterdataBaseParser {

    protected MasterdataParser(InputStream response) {
        super(response);
    }

    public MasterData parse() {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        MasterData masterData;

        try {

            DocumentBuilder builder = factory.newDocumentBuilder();
            Document dom = builder.parse(this.getInputStream());
            Element root = dom.getDocumentElement();

            masterData = new MasterData();

            NodeList list;
            Element element;
            NodeList childNodeList;
            Node node;
            String nodeName; 


list = root.getElementsByTagName(PREFIX+CATEGORY);

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

                element = (Element) list.item(i);
                childNodeList = element.getChildNodes();
                Category category = new Category();

                for (int j = 0; j < childNodeList.getLength(); j++) {

                    node = childNodeList.item(j);
                    nodeName = node.getNodeName();

                    if (nodeName.equalsIgnoreCase(PREFIX+CATEGORY_ID)) {
                        try{
                            category.setId(node.getFirstChild().getNodeValue());
                        } catch (NullPointerException ex) {
                            category.setId(null);
                        }
                    } 
     if (nodeName.equalsIgnoreCase(PREFIX+CATEGORY_NAME)) {
                            try{
    //                          System.out.println("Masterdataparser category name html "+Html.fromHtml(node.getFirstChild().getNodeValue()));   // Not worked
                                System.out.println("URL Decoder "+URLDecoder.decode(node.getFirstChild().getNodeValue(), "UTF-8"));  // Not worked
                                category.setName(node.getFirstChild().getNodeValue());
                            } catch (NullPointerException ex) {
                                category.setName(null);
                            }
                        }
}

public abstract class MasterdataBaseParser implements MasterdataParserInterface {
            static final String CATEGORY = "Category";
        static final String CATEGORY_ID = "Id";
        static final String CATEGORY_NAME = "Name";
    final InputStream response;

        protected MasterdataBaseParser(InputStream response){
                this.response = response;
        }

        protected InputStream getInputStream() {
            return response;
        }
    }

但它仅显示"mil",并且未将"&amp;"视为"&"。但我想显示"mil & carbon"。怎么做这个?预感谢任何帮助

2 个答案:

答案 0 :(得分:1)

以下是我使用一个类解析器和几个xml流执行操作的示例:

public class RSSHandler extends DefaultHandler {
    int                         RSS_ID;
    ContentValues               parsedValues    = new ContentValues();
    private String              parsedString    = null;
    protected DataController    _dataController;
    private StringBuffer        parsedBuffer    = new StringBuffer();

    public void setRssId(int rssId) {

        RSS_ID = rssId;
    }

    public void setDataController(DataController dataController) {

        _dataController = (DataController) dataController;
    }

    @Override
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {

    }

    @Override
    public void endElement(String namespaceURI, String localName, String qName) throws SAXException {

        parsedString = parsedString.trim();
        switch (RSS_ID) {
            case (0):
                if (localName.equals("Application")) {
                    _dataController.insertRowInTable(RSS_ID, parsedValues);
                }
                else {
                    parsedValues.put(localName, parsedString);
                }
                break;
            case (1):
                if (localName.equals("tab")) {
                    _dataController.insertRowInTable(RSS_ID, parsedValues);
                }
                else {
                    parsedValues.put(localName, parsedString);
                }
                break;
            case (2):
                if (localName.equals("category")) {
                    _dataController.insertRowInTable(RSS_ID, parsedValues);
                }
                else {
                    parsedValues.put(localName, parsedString);
                }
                break;
            case (3):
                if (localName.equals("item")) {
                    _dataController.insertRowInTable(RSS_ID, parsedValues);
                }
                else {
                    parsedValues.put(localName, parsedString);
                }
                break;
            case (4):
                if (localName.equals("event")) {
                    _dataController.insertRowInTable(RSS_ID, parsedValues);
                }
                else {
                    parsedValues.put(localName, parsedString);
                }
                break;
            case (5):
                if (localName.equals("album")) {
                    _dataController.insertRowInTable(RSS_ID, parsedValues);
                }
                else {
                    parsedValues.put(localName, parsedString);
                }
                break;
            case (6):
                if (localName.equals("picture")) {
                    _dataController.insertRowInTable(RSS_ID, parsedValues);
                }
                else {
                    parsedValues.put(localName, parsedString);
                }
                break;
            case (7):
                if (localName.equals("location")) {
                    _dataController.insertRowInTable(RSS_ID, parsedValues);
                }
                else {
                    parsedValues.put(localName, parsedString);
                }
                break;
            default:
                break;
        }
        parsedString = "";
        parsedBuffer = new StringBuffer();
    }

    @Override
    public void characters(char ch[], int start, int length) {

        parsedBuffer.append(ch, start, start + length);
        parsedString = parsedBuffer.toString();
    }
}

DataController只是一个访问方法和属性的全局类。

public void insertRowInTable(int tableId, ContentValues myRow) {

        String myTable = _tableNameList.get(tableId);
        _dataBaseHelper.createRowInTable(myTable, myRow);
    }

public void createRowInTable(String tableName, ContentValues values) {

    try {
        myDataBase.insert(tableName, null, values);
    }
    catch (Exception e) {
        //catch
    }
}

答案 1 :(得分:0)

解析xml时,默认为“&amp;”表现为“逃脱序列”(在2.2的低级版本中),这就是为什么我无法解析“mil&amp; carbon”。因此,在解析之前,我将"&amp;"替换为文件下的"ampersand"(In words),然后将该节点取为"mil ampersand carbon",然后将"ampersand"替换为"&"(symbol)。非常感谢“Yume”先生