Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > декодирование html мнемоник


Автор: Evropeec 11.2.2011, 18:13
помогите разобраться,

есть xml файл. в нем есть такая строчка:
Код


<?xml version="1.0" encoding="UTF-8"?>
<body>
    <text>Hallo</text>
    <content_id>&lt;smil&gt;</content_id>
</body>


обрабатываю через org.w3c.dom.Document

Код

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
_xmlDocument = docBuilder.parse(new InputSource(new StringReader(sXmlResp)));


где sXmlResp мой документ.

затем я его читаю примерно так:

Код

public void createMmsList(Document doc) {

        ArrayList<Mms> mmsList = new ArrayList<Mms>();
        try {
            Node body = doc.getElementsByTagName("body").item(0);

            for (int i = 0; i < body.getChildNodes().getLength(); i++) {
                Node child = body.getChildNodes().item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    if (child.getNodeName().equals("text")) {
                        String text = child.getFirstChild().getNodeValue()
                    }
                    if (child.getNodeName().equals("content_id")) {
                        String content_id = child.getFirstChild().getNodeValue()
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }


так вот переменная text = "Hallo", а content_id = "<".

почему, как исправить?

Автор: Samotnik 12.2.2011, 01:00
Temp.java
Код

package com.my.test;

import java.io.File;
import java.io.IOException;

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

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Temp {
    
    public static void main(String ... args) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File("c:/laLiga.xml"));
            doc.getDocumentElement().normalize();
            NodeList nodeLst = doc.getElementsByTagName("body");
            for (int s = 0; s < nodeLst.getLength(); s++) {
                Node fstNode = nodeLst.item(s);
                if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
                    
                    Element elText = (Element) fstNode;
                    
                    NodeList elTextLst = elText.getElementsByTagName("text");
                    Element elTextElmnt = (Element) elTextLst.item(0);
                    NodeList fstNm = elTextElmnt.getChildNodes();
                    System.out.println("Text : "  + ((Node) fstNm.item(0)).getNodeValue());
                    
                    NodeList elContentLst = elText.getElementsByTagName("content_id");
                    Element elContentElmnt = (Element) elContentLst.item(0);
                    NodeList elContent = elContentElmnt.getChildNodes();
                    System.out.println("content_id : " + ((Node) elContent.item(0)).getNodeValue());
                }
            }
        } catch (ParserConfigurationException e) {            
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }
}


laLiga.xml
Код

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <body>
        <text>Real Madrid</text>
        <content_id>&lt;one&gt;</content_id>
    </body>
    <body>
        <text>Barcelona</text>
        <content_id>&amp;two&quot;</content_id>
    </body>
    <body>
        <text>Valencija</text>
        <content_id>&amp;three&amp;</content_id>
    </body>
    <body>
        <text>Real Sosiedad</text>
        <content_id>&quot;four&quot;</content_id>
    </body>
    <body>
        <text>Atletico Madrid</text>
        <content_id>&lt;five&gt;</content_id>
    </body>    
</root>

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)