0

私の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"。これを行う方法は?助けてくれてありがとう

4

2 に答える 2

1

以下は、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 に答える