2011-10-07 116 views
0

我想在黑莓做一個XML解析器。但是我遇到了一個奇怪的問題。XML解析document.CreateElement異常黑莓

import org.w3c.dom.DOMException; 
import org.w3c.dom.Document; 
import org.w3c.dom.Element; 

import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.*; 
import net.rim.device.api.ui.container.MainScreen; 
import net.rim.device.api.xml.parsers.DocumentBuilder; 
import net.rim.device.api.xml.parsers.DocumentBuilderFactory; 
import net.rim.device.api.xml.parsers.ParserConfigurationException; 

public class HelloWorld extends UiApplication { 
    public static void main(String[] args) { 
     HelloWorld theApp = new HelloWorld(); 
     theApp.enterEventDispatcher(); 
    } 

    public HelloWorld() { 
     pushScreen(new HelloWorldScreen()); 
    } 
} 

final class HelloWorldScreen extends MainScreen { 


    public HelloWorldScreen() {  
     super();   
     LabelField title = new LabelField("XML TEST", LabelField.ELLIPSIS 
       | LabelField.USE_ALL_WIDTH); 
     setTitle(title); 
     RichTextField rcfield = new RichTextField("XML TEST!"); 
     add(rcfield); 
     this.doPaint(); 
     this.invalidate(); 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db=null; 
     Document doc = null; 

     try { 
      db = dbf.newDocumentBuilder();   
     } catch (ParserConfigurationException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     doc = db.newDocument();   
     try{    
     Element e = doc.createElement("s:Envelop"); 
     }catch(DOMException ex){ 
      System.out.println(ex.getMessage()); 
     } 



    } 

    public boolean onClose() { 
     Dialog.alert("Goodbye!"); 
     System.exit(0); 
     return true; 
    } 
} 

錯誤消息:

[0.0] DOMException 
[0.0] No detail message 
[0.0] net_rim_xml(4C48DD8C) 
[0.0] DOMInternalRepresentation 
[0.0] isNCName 
[0.0] 0x3930 
[0.0] net_rim_xml(4C48DD8C) 
[0.0] DOMDocumentImpl 
[0.0] createElement 
[0.0] 0x4CC 
[0.0] VM:+CR 
[0.0] VM:-CR=7 

錯誤代碼4:無效字符錯誤 也許 「:」 是無效字符?但它在android上工作正常:/ 我沒有任何想法如何解決它:(

+0

看看這個http://stackoverflow.com/questions/7671523/ksoap-parsing-query/7683966#7683966 –

+0

感謝代表 我想從字符串創建一個文檔(我有一個節點類型,我可以得到他們的孩子等) 所以我只是想創建一個元素形成一個字符串。:( – hEngi

回答

1

您試圖創建一個名稱空間元素,但您尚未定義「s」前綴或其名稱空間URI嘗試之前使用它們,使用Document.createElementNS(),而不是創建一個Document.createElement()命名空間的元素。

或者,嘗試調用newDocument()之前調用newDocumentBuilder(),或DocumentBuilder.setAllowUndefinedNamespaces(true)之前調用DocumentBuilderFactory.setAllowUndefinedNamespaces(true)