Vitaly Menchikovsky
Vitaly Menchikovsky

Reputation: 8924

create simple xml in Java

I am trying to build server that sends a xml file to client. I am getting info from db and wants to build from that xml file.

But I have a problem with:

DocumentBuilder documentBuilder = null;
Document doc =documentBuilder.newDocument();

I am getting NullPointerException. Here is me full code:

public void createXmlTree() throws Exception {
  //This method creates an element node
  DocumentBuilder documentBuilder = null;
  Document doc =documentBuilder.newDocument();

  Element root = doc.createElement("items");
  //adding a node after the last child node of the specified node.

  doc.appendChild(root);
  for(int i=0;i<db.stories.size();i++){
    Element child = doc.createElement("item");
    root.appendChild(child);

    Element child1 = doc.createElement("title");
    child.appendChild(child1);

    Text text = doc.createTextNode(db.stories.get(i).title);
    child1.appendChild(text);

    //Comment comment = doc.createComment("Employee in roseindia");
    //child.appendChild(comment);

    Element child2 = doc.createElement("date");
    child.appendChild(child2);

    Text text2 = doc.createTextNode(db.stories.get(i).date);
    child2.appendChild(text2);

    Element child3 = doc.createElement("text");
    child.appendChild(child3);

    Text text3 = doc.createTextNode(db.stories.get(i).text);
    child3.appendChild(text3);

    root.appendChild(child3);

Upvotes: 0

Views: 522

Answers (4)

YMomb
YMomb

Reputation: 2397

Do you really need to write you XML manually? Do you have the XSD of the XML you want to write?

Because, it would be easier to generate some classes using XJC/JAXB and use the marshaller to write your XML file.

Upvotes: 1

AlexR
AlexR

Reputation: 115388

Guys are right about DocumentBuilder. But may I offer you other solution? Your servlet mostly deals with generating of XML itself, i.e. produces kind of markup. This is the purpose of JSP. You can implement simple JSP page that will actually contain template of your XML and some code that inserts dynamic data. This is much simpler and easier to maintain.

Yes, JSP typically generate HTML but no-one said that they cannot generate XML or any other text format. Just do not forget to set content type to text/xml.

Upvotes: 1

peshkira
peshkira

Reputation: 6239

of course you are getting a NullPointerException, your DocumentBuilder is null. Try instantiating it first.

    // Step 1: create a DocumentBuilderFactory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    // Step 2: create a DocumentBuilder
    DocumentBuilder db = dbf.newDocumentBuilder();

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503140

Well yes, you would get a NullPointerException. You're calling a method on a null reference - very clearly, given that you've assigned the documentBuilder a null value on the line before. You need to get an instance of DocumentBuilder to start with. For example:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();

Upvotes: 2

Related Questions