无法解析xml,获取IOException

时间:2013-10-04 06:27:26

标签: java xml xml-parsing

我正在为这行代码获取IOException。

      Response oresponse = orequest.send();

     **This above Response object contains Xml data** :

         <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
         <person>
         <first-name>xyz</first-name>
         <last-name>abc</last-name>
         <api-standard-profile-request>
         <url>http:[Removed]</url>
         <headers total="1">
             <http-header>
              <name>[removed]</name>
              <value>[removed]</value>
             </http-header>
         </headers>
         </api-standard-profile-request>
         </person>

我的解析代码如下所示。

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
Document doc = dBuilder.parse(oresponse.getStream());

我在最后一行获得IOException,即dBuilder.parse(oresponse.getStream())。意味着在解析期间。我怎么解析这个xml.It给了我:

java.io.IOException: stream is closed.
at sun.net.www.http.ChunkedInputStream.ensureOpen(Unknown Source)
at sun.net.www.http.ChunkedInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(Unknown Source)

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码

public class Parser {

public static void parse(Context context, int type) {
        try {

            // File fXmlFile = new File("/root/Desktop/staff.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputStream catDoc = null;
            ConstantLib.flashCardList = new ArrayList<FlashCardBean>();
            Log.e("N", "TG " + type);

                catDoc =context.getResources().openRawResource(R.raw.animal);


            Document doc = dBuilder.parse(catDoc);

            // optional, but recommended
            // read this -
            // http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("content");

            for (int temp = 0; temp < nList.getLength(); temp++) {

                Node nNode = nList.item(temp);

                System.out.println("\nCurrent Element :" + nNode.getNodeName());

                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;
                    FlashCardBean cardBean = new FlashCardBean();
                    cardBean.setText(eElement.getElementsByTagName("text")
                            .item(0).getTextContent());
                    cardBean.setDetail(eElement.getElementsByTagName("detail")
                            .item(0).getTextContent());
                    cardBean.setImageSource(eElement
                            .getElementsByTagName("imgsource").item(0)
                            .getTextContent());
                    cardBean.setBorderColor(eElement
                            .getElementsByTagName("bordercolor").item(0)
                            .getTextContent());

                    cardBean.setMusicFile(eElement
                            .getElementsByTagName("imgsource").item(0)
                            .getTextContent().replace(".svg", ".mp3"));

                    ConstantLib.flashCardList.add(cardBean);

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}