Создание XMl в Blackberry с использованием net.rim.device.api.xml.parsers.DocumentBuilder - PullRequest
0 голосов
/ 05 мая 2011

Я пытаюсь создать и написать XML в приложении Blackberry, используя API DocumentBuilder.Я написал следующий метод для создания хотя бы одного элемента и его атрибута:

  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 Ответ

1 голос
/ 14 августа 2011

Возможно, вам нужно добавить еще одну инструкцию после serverElement.setAttrib

doucment.appendChild(surveyElement);

этот оператор добавляет дочерний элемент, который вы создали.

...