0

我得到这行代码的 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。它给了我:

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)
4

1 回答 1

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

}

于 2013-10-04T06:40:57.780 回答