0

In my xml, i have <ns5:Name>mil &amp; carbon</ns5:Name> From my java code i am retrieving that value by

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;
        }
    }

But it displays only "mil" and it is not taking "&amp;" as "&". But i want to display "mil & carbon". How to do this one? Advance thanks for any help

4

2 回答 2

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
    }
}
于 2013-09-23T08:58:13.993 回答
0

在解析 xml 时,默认情况下“&”表现为“转义序列”(在 android 较低版本中,如 2.2),这就是为什么我无法解析“mil & carbon”的原因。因此,在解析之前,我在文件下替换"&amp;"为,"ampersand"(In words)并在获取该节点"mil ampersand carbon"后替换"ampersand""&"(symbol). 非常感谢“Yume”先生

于 2013-09-24T06:09:38.730 回答