Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog
报这个错的原因是什么啊?有人知道么?会不会和weblogic版本有关呢?我在tomcat下跑没事,在weblogic下跑就不行,在读取一个xml的时候报这个错

解决方案 »

  1.   

    异常提示是Content is not allowed in prolog.意思就是说文件开头的时候有问题。
      

  2.   


    解析XMLCSDN 上有人提过解析XML的问题,你可以参考一下。
    http://topic.csdn.net/u/20101217/21/8e9c3169-248a-439c-84c6-5d5a3a9763c9.html?1885069617
    http://topic.csdn.net/u/20110221/10/6894845d-1e56-4bba-a1e4-5dc185d70ef8.html另外:
    Android 支持JAVA传统的两种方式:DOM 和SAX。DOM 需要load整个文件,所以需要比较多的内存,担正因为此,整颗树都生成好,比较直观。DOM的代码稍微复杂些,以下是几个准则:· 整个文档是一个文档节点· 每个 XML 标签是一个元素节点(element)· 包含在 XML 元素中的文本是文本节点(text)· 每一个 XML 属性是一个属性节点 (attribute) · 注释属于注释节点关键是得到一个Document对象,并得到对应的层层Node。(element、attribute、textnode都是node。其中element是比较直观理解的元素)。SAX 的优点是选择性的parse 某个节点,比较方便。关键是自己实现一个org.xml.sax.helpers.DefaultHandler的基础类,并重载:startDocument 打开一个文档的callbackendDocument 关闭一个文档的callbackstartElement 打开一个元素的callbackendElement 关闭一个元素的callbackcharacters 读到一个元素的内容的callback (注:这里没有什么文本节点的概念了,只有元素的概念)在这个函数里面,可以选择性的读取某些元素的内容例子:<?xml version="1.0"?>
    <books>
    <book category="computer-programming">
     <author>Steven Haines</author>
     <title>Java 2 Primer Plus</title>
     <price>44.95</price>
    </book>
    <book category="fiction">
     <author>Tim LaHaye</author>
     <title>Left Behind</title>
     <price>15.95</price>
    </book>
    </books>两种方法的parse:DOM: String parseXMLDom(String filename)   
     {
     String result = null;
     String temp;
     try
     {
     File file = new File( "/sdcard/book.xml" );
     if( !file.exists() )
     {
     String st = String.format( "Couldn't find file..." );
     return st;
     } // Parse the document
     DocumentBuilderFactory factory =
     DocumentBuilderFactory.newInstance();
     DocumentBuilder builder = factory.newDocumentBuilder();
     Document document = builder.parse( file ); // Walk the document
     Element root = document.getDocumentElement();
     temp =( "root=" + root.getTagName()+"\n" );
     result += temp;
     // List the children of <books>; a set of <book> elements
     NodeList list = root.getChildNodes();
     for( int i=0; i<list.getLength(); i++ )
     {
     Node node = list.item( i );
     if( node.getNodeType() == Node.ELEMENT_NODE )
     {
     // Found a <book> element
     temp = ( "Handling node: " +
     node.getNodeName()+"\n" );
       
     result += temp;
       
     Element element = ( Element )node;
     temp = ( "tCategory Attribute: " +
     element.getAttribute( "category" )+"\n" );
       
     result += temp;
       
     // Get its children: <author>, <title>, <price>
     NodeList childList = element.getChildNodes();
     for( int j=0; j<childList.getLength(); j++ )
     {
     // Once one of these nodes is attained, the next
     // step is locating its text element
     // It is important. It is NOT <author>'s value, but it is its text element's value
     Node childNode = childList.item( j );
     if( childNode.getNodeType() == childNode.ELEMENT_NODE )
     {
     NodeList childNodeList =
     childNode.getChildNodes();
     for( int k=0; k<childNodeList.getLength(); k++ )
     {
     Node innerChildNode = childNodeList.item( k );
       
     temp = ( "ttNode (" + childNode.getNodeName()+ ") = "+
     innerChildNode.getNodeValue() +"\n");
     result += temp;
       
     //innerChildNode's name is "text", and childNode's value is null
     }
     }
     }
     }
     }
       
     
       
     
     } catch( Exception e )
     {
     e.printStackTrace();
     }
       
     
     return result; }SAX: String parseSAX(String filename)
     {
     String result = "";
       
     File file = new File( "/sdcard/book.xml" );
     if( !file.exists() )
     {
     String st = String.format( "Couldn't find file..." );
     return st;
     }
       
     FileInputStream read;
     try {
     read = new FileInputStream(file);   
     
     SAXParserFactory spf = SAXParserFactory.newInstance();
     SAXParser sp = spf.newSAXParser();   
     XMLReader xr = sp.getXMLReader();
       
     ExampleHandler myExampleHandler = new ExampleHandler();
     xr.setContentHandler(myExampleHandler);
       
     
     xr.parse(new InputSource(read));
       
     
     result = myExampleHandler.getParsedData();
     } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }catch (SAXException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     } catch (ParserConfigurationException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     }
     return result;
     }public class ExampleHandler extends DefaultHandler {
     
    String result = "";
    int index = -1;String category[] = new String [2];
    String author[] = new String [2];
    String title[] = new String [2];
    String price[] = new String [2]; private boolean bbook = false;
     private boolean bprice = false;
     private boolean bauthor = false;
     private boolean btitle = false;
       
     public String getParsedData() {
     return this.result;
     }@Override
    public void characters(char[] ch, int start, int length)
     throws SAXException {
     // TODO Auto-generated method stub
     if(bbook && bprice)
     price[index] = new String(ch, start, length)+ "\n";
     if(bbook && bauthor)
     author[index] = new String(ch, start, length)+ "\n";
     if(bbook && btitle)
     title[index] = new String(ch, start, length)+ "\n";
     super.characters(ch, start, length);
    }@Override
    public void endDocument() throws SAXException {
     // TODO Auto-generated method stub
     int i;
     for(i=0; i<=index; i++){
     result += "Book: \n";
     result += title[i];
     result += category[i];
     result += author[i];
     result += price[i];
       
     }
     super.endDocument();
    }@Override
    public void endElement(String uri, String localName, String name)
     throws SAXException {
     // TODO Auto-generated method stub if (localName.equals("book")) {
     bbook = false; } else if (localName.equals("author")){
     bauthor = false;   
     }else if (localName.equals("title")){
     btitle = false;   
     }else if (localName.equals("price")){
     bprice = false;   
     } super.endElement(uri, localName, name);
    }@Override
    public void startDocument() throws SAXException {
     // TODO Auto-generated method stub
     index = -1;
     super.startDocument();
    }@Override
    public void startElement(String uri, String localName, String name,
     Attributes atts) throws SAXException {
     // TODO Auto-generated method stub
     if (localName.equals("book")) {
     bbook = true;
     index ++;
     String attrValue = atts.getValue("category");
     category[index]= attrValue + "\n"; } else if (localName.equals("author")){
     bauthor = true;   
     }else if (localName.equals("title")){
     btitle = true;   
     }else if (localName.equals("price")){
     bprice = true;   
     }
     super.startElement(uri, localName, name, atts);
    }}希望对你有帮助
      

  3.   

    http://cache.baidu.com/c?m=9f65cb4a8c8507ed4fece76310458f355d0797634b8a90406283d91fd179111e197be0bc25241613d3b226215ef15d19b7b0607d665e7cf1cc94ce5dddcac969388856682d5a9141638047f19603729737902bb2f30ee7cbb272cef08f86&p=8b2a9356908007ff57efe66d4f&user=baidu&fm=sc&query=Content+is+not+allowed+in+prolog&qid=a7d3e3b004bd7f10&p1=21.xml编码错误 有无BOM头?
    2.xml文件存在非法字符?
    sax解析xml运行出现错误org.xml.sax.SAXParseException: Content is not allowed in prolog.
     
    原因及其解决办法:
    1.xml编码错误
    该xml是UTF-8编码的,如果该文件通过UltraEdit编辑后,会在无BOM头的UTF-8文件中加入BOM,但是DOM4j不认这个BOM(dom4j1.3),解决的办法可以通过升级dom4j到1.6解决www.dom4j.org
    什么是BOM?http://www.unicode.org/faq/utf_bom.html#22
    Unicode规范中有一个BOM的概念。BOM——Byte Order Mark,就是字节序标记。在这里找到一段关于BOM的说明:
    在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输字符"ZERO WIDTH NO-BREAK SPACE"。这样如果接收者收到FEFF,就表明这个字节流是Big-Endian的;如果收到FFFE,就表明这个字节流是Little-Endian的。因此字符"ZERO WIDTH NO-BREAK SPACE"又被称作BOM。
    UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8编码是EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。
    Windows就是使用BOM来标记文本文件的编码方式的。
    2.xml文件存在非法字符
    把xml 动手敲一遍,删除不该有的间隔符:比如空格,制表符(TAB)等。
     
    异常提示是Content is not allowed in prolog.意思就是说文件开头的时候有问题。
    怎么去掉BOM:
    首先windows自带的记事本会在保存UTF-8编码的文件时不管是否含有BOM一律加上BOM,所以千万别用记事本。比较新版本的UltraEdit(以下简称UE,大家都是这么叫的)都对BOM支持比较好,所以推荐使用新版本的UE来解决这个问题。我用的版本是UltraEdit-32 11.00a+中文版。首先打开UE,选择菜单栏“高级”-“配置”,出现一个选项卡窗口,在“常规”选项卡中找到“保存时对所有UTF-8文件头标记(BOM)”和“对在UltraEdit里创建的新文件写入UTF-8文件头标记(如上面)”,把它们的复选框都取消不选,然后确定。这个时候UE默认情况下就不会对没有BOM的文件加入BOM,注意如果不执行上述操作UE默认也是会加的!那已经含有BOM的文件如何清除BOM呢?下面演示给大家。首先打开文件,选择菜单栏“文件”-“格式转换”-“UTF-8 转 ASCII”,这样文件会被转为ASCII编码,BOM就不会存在了,因为上面说过BOM是在UCS编码的头部的,然后再选择菜单栏“文件”-“格式转换”-“ASCII 转 UTF-8(Unicode编辑)”,这个时候文件会被转回为UTF-8编码,因为我们上面选了不自动加BOM,所以这个时候的文件是不带BOM的,然后保存文件。整个操作过程就完毕了。