java.net.MalformedURLException:无协议:XML

时间:2014-04-12 07:10:29

标签: java xml file protocols writer

我正在尝试将XML String输出到.xml文件中以进行调试。我的字符串看起来像这样:

System.out.println("xmlString = \n" + xmlString);
===========================================================
INFO: xmlString = 
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
  <int name="status">0</int>
  <int name="QTime">2</int>
  <lst name="params">
    <str name="indent">true</str>
    <str name="q">source_t:ST</str>
    <str name="wt">xml</str>
  </lst>
</lst>
...

现在,以下代码将String输出到XML文件中。

try {
                System.out.println("Writing xmlString to xml file");

                File file = new File("xmlString.xml");

                // if file doesnt exists, then create it
                if (!file.exists()) {
                        file.createNewFile();
                }

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(xmlString);
                bw.close();

                System.out.println("Done writing file");

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

但这是我从输出日志中得到的错误:

INFO: Writing xmlString to xml file
INFO: Done writing file
SEVERE: java.net.MalformedURLException: no protocol: <?xml version="1.0" encoding="UTF-8"?>

没有创建文件。 有人能告诉我这是什么意思吗?以及如何解决这个问题? 提前致谢

=============================================== ======================================= 编辑(我最初的错误在于FileWritter,这就是为什么我没有发布整个代码):我编辑了我的帖子来显示我的整个代码。此代码实际上由JSP文件调用。哪个传递了xmlString

public class SAXParserClass {

    //Declare as public since it needs to be redefined for appending into ArrayList
    public static String row[] = new String[5];         //id, full_message_t, source_t, news_t, link_t  

    public ArrayList<String[]> XMLReader(String xmlString)
    {
        //Output: GlassFish Server. Test if String is passed in correctly
        System.out.println("xmlString = \n" + xmlString);
        try {
                System.out.println("Writing xmlString to xml file");

                File file = new File("xmlString.xml");

                // if file doesnt exists, then create it
                if (!file.exists()) {
                        file.createNewFile();
                }

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(xmlString);
                bw.close();

                System.out.println("Done writing file");

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

          //Declaration
          final ArrayList<String[]> tableResult = new ArrayList<String[]>();    //Store ArrayList of row[]

          try {

              SAXParserFactory factory = SAXParserFactory.newInstance();
              SAXParser saxParser = factory.newSAXParser();

              DefaultHandler handler = new DefaultHandler() 
              {
                  //When these are TRUE, extract the content
                    boolean b_id = false;
                    boolean b_full_message_t = false;
                    boolean b_source_t = false;
                    boolean b_news_t = false;
                    boolean b_link_t = false;                

                    //############################################################################################
                    //Look for <start> tags
                    public void startElement(String uri, String localName,String qName, 
                            Attributes attributes) throws SAXException {

                            System.out.println("Start Element :" + qName);

                            //Only interested in <str>
                            if (qName.equalsIgnoreCase("str")) {
                                String name = attributes.getValue("name");

                                    //Check for name="id"
                                if(name.equalsIgnoreCase("id")){
                                    b_id = true;
                                }                                
                                    //Check for name="full_message_t"
                                else if(name.equalsIgnoreCase("full_message_t")){
                                    b_full_message_t = true;
                                }
                                else if(name.equalsIgnoreCase("source_t")){
                                    b_source_t = true;
                                }
                                else if(name.equalsIgnoreCase("news_t")){
                                    b_news_t= true;
                                }
                                else if(name.equalsIgnoreCase("link_t")){
                                    b_link_t = true;
                                }                             
                            }//end if

                    }//end startElement

                    //############################################################################################
                    //Look for <end> tags
                    public void endElement(String uri, String localName,
                            String qName) throws SAXException {

                            System.out.println("End Element :" + qName);

                            //When reach </doc>, row Array is complete. Add into ArrayList
                            if (qName.equalsIgnoreCase("doc")) {
                                    System.out.println("Push row into Arraylist : " + row[0]);
                                    tableResult.add(row);
                                    row = new String[5];    //reinitialize String[]
                            }
                    }//end endElement

                    //############################################################################################
                    //Get the content
                    public void characters(char ch[], int start, int length) throws SAXException {

                            if (b_id) {
                                    //System.out.println("id : " + new String(ch, start, length));
                                    row[0] = new String(ch, start, length);
                                    System.out.println("The id is " + row[0]);
                                    b_id = false;
                            }
                            else if (b_full_message_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[1] = new String(ch, start, length);
                                    System.out.println("The fullmsg is " + row[1]);
                                    b_full_message_t = false;
                            }
                            else if (b_source_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[2] = new String(ch, start, length);
                                    System.out.println("The source is " + row[2]);
                                    b_source_t = false;
                            }
                            else if (b_news_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[3] = new String(ch, start, length);
                                    System.out.println("The news is " + row[3]);
                                    b_news_t = false;
                            }
                            else if (b_link_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[4] = new String(ch, start, length);
                                    System.out.println("The link is " + row[4]);
                                    b_link_t = false;
                            }

                    }//end characters

           };//end DefaultHandler

              //############################################################################################
              //Read the String  
              //saxParser.parse("solrTest.xml", handler);
              //File file = new File("solrTest.xml");
              //InputStream inputStream= new FileInputStream(file);
//            Reader reader = new InputStreamReader(xmlString,"UTF-8");
// 
//            InputSource is = new InputSource(reader);
//            is.setEncoding("UTF-8");

              saxParser.parse(xmlString, handler);

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

          //Test output Arraylist
          System.out.println("Test Result!!!");

          for(String[] columns : tableResult){
              for(int i=0; i<columns.length; i++){
                  System.out.println("Number = " + columns[i]);
              }
          }

          //Return the ArrayList of String[]          
          return tableResult;
      }//end XMLReader

}//end class

1 个答案:

答案 0 :(得分:5)

  

saxParser.parse(xmlString,handler);

问题出在这里。您正在传递XML字符串,就好像它是一个URL。事实并非如此。您需要阅读SAXParser.parse()的Javadoc。

注意:

  1. 您发布了错误的代码。堆栈跟踪告诉您抛出异常的位置。你显然甚至没有看过它。
  2. 当被问及时,你没有回答问题。
  3. 你问了无关紧要的问题。
  4. 你拒绝做导致问题的事情,之后提出你必须做的事情。所以你甚至没有检查。
  5. 这不是解决问题的理性策略。