2011-05-05 59 views
0

我試圖在使用DocumentBuilder API的Blackberry應用程序中創建和編寫XML。我已經寫了下面的方法來創建一個ATLEAST元素和它的屬性:使用net.rim.device.api.xml.parsers.DocumentBuilder在Blackberry中創建XMl

private void createResponseXML() 
    { 
    try 
    { 
     // Build a document based on the XML file. 
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder builder = factory.newDocumentBuilder(); 

     Document document = builder.newDocument(); 
     Element surveyElement = document.createElement("Survey"); 
     surveyElement.setAttribute("xmlns:abapsurvey", "http://www.sap.com/abapsurvey"); 

     FileConnection fileConnection = (FileConnection) Connector.open("file:///SDCard/survey_response.xml", Connector.READ_WRITE, true); 
     OutputStream outputStream = fileConnection.openOutputStream(); 
     XMLWriter writer = new XMLWriter(outputStream); 
     writer.setPrettyPrint(); 
     DOMInternalRepresentation.parse(document, writer); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 

    } 

這我試圖創建XML的結構如下:

<?xml version="1.0" encoding="utf-8" ?> 
- <Survey xmlns:abapsurvey="http://www.sap.com/abapsurvey" xmlns:bee="http://www.sap.com/survey/bee" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:htmlb="http://www.sap.com/survey/htmlb" xmlns:out="http://www.w3.org/1999/XSL/Output" xmlns:svy="http://www.sap.com/survey/svy" xmlns:tmp="http://www.sap.com/survey/tmp" xmlns:values="http://www.w3.org/1999/XSL/TransformValues" xmlns:wff="http://www.mysap.com/wff/2001"> 
- <Values> 
- <Question QuestionId="q1"> 
- <Answer AnswerId="id_4214130a0e731678e10000000a114eea"> 
    <Value>id_4214134d0e731678e10000000a114eea</Value> 
    </Answer> 
    </Question> 
- <Question QuestionId="id_421413b20e731678e10000000a114eea"> 
- <Answer AnswerId="id_421413cb0e731678e10000000a114eea"> 
    <Value>id_421413f70e731678e10000000a114eea</Value> 
    </Answer> 
    </Question> 
- <Question QuestionId="id_4214142c0e731678e10000000a114eea"> 
- <Answer AnswerId="id_4214143e0e731678e10000000a114eea"> 
    <Value>id_4214f3f6f3eb3d67e10000000a114eea</Value> 
    </Answer> 
    </Question> 
- <Question QuestionId="id_4214f40cf3eb3d67e10000000a114eea"> 
- <Answer AnswerId="id_42144d6d48021679e10000000a114eea"> 
    <Value>id_42144d9048021679e10000000a114eea</Value> 
    </Answer> 
    </Question> 
    </Survey> 

我創建我的筆記本電腦硬盤上的一個文件夾來模擬SD卡。當我把這種方法中,XML僅與一個頭創建如下:

<?xml version="1.0"?> 

爲什麼是它的元素,我想用沒有得到的XML創建了下面的代碼追加?

 Element surveyElement = document.createElement("Survey"); 
     surveyElement.setAttribute("xmlns:abapsurvey", "http://www.sap.com/abapsurvey"); 

請建議。

回答

1

你可能需要經過serverElement.setAttrib

doucment.appendChild(surveyElement); 

這種說法追加您創建的子元素添加其他聲明。