This discussion is archived
1 Reply Latest reply: Sep 17, 2011 1:19 PM by 805536 RSS

Writing data as XML from java

888819 Newbie
Currently Being Moderated
I want to write the data to an XML.It should have two child nodes,TC name and TC status for the parent node.Ex: for TC1, TC name and TC status are the child nodes and similarly for the other test cases.<end of TC1> should end the TC1.
TC name data is coming from one function and TC status data is coming from another function,so how to create the XML file.

Ex:
<root>
<TC1>
<TC name="abc">
<TC status="pass">
<end of TC1>
<TC2>
<TC name="abc">
<TC status="pass">
<end of TC2>

I have created three functions:
1.XML_create(String s1)
2.xmlAppendtest(String testcase)
3.xmlAppendstatus(String verdictValue)

When the first test case is run,it will go into the first function XML_create and from second ,third ,etc..test cases,it will go to second function xmlAppendtest as the here only appending is done.Creation of the root node is done in the first function.I created two global variables,flag and count.Here everything is getting added to the root node.Can anyone please correct my code.

     int flag=1;
     int count =1;
     public void XML_create(String s1)
     {
          String s= s1;
          
          System.out.println("value of s"+ s);
          System.out.println("value of flag"+ flag);
          
          if(flag==1)
          {
          File file = new File("c:\\test.xml");
          
          flag =0;
          

          //System.out.println("*********************creating the file ******************"+ss);
          
          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

          DocumentBuilder documentBuilder = null;
          try {
               documentBuilder = documentBuilderFactory.newDocumentBuilder();
          } catch (ParserConfigurationException e1) {
               // TODO Auto-generated catch block
               e1.printStackTrace();
          }

          Document document = documentBuilder.newDocument();

          Element rootElement = document.createElement("root"); // creates a element
          
          Element element= document.createElement("Testcase" + count);

          Element element1 = document.createElement("Testcase"); //create another element
          
          //creates an Attribute of element1 as type and sets its value to String

          element1.setAttribute(new String("name"), s);
          
          rootElement.appendChild(element);
          
          element.appendChild(element1); // add element1 under rootElement

          document.appendChild(rootElement); // add the rootElement to the document

          TransformerFactory transformerFactory = TransformerFactory.newInstance();

          Transformer transformer = null;
          try {
               transformer = transformerFactory.newTransformer();
          } catch (TransformerConfigurationException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }

          DOMSource source = new DOMSource(document);

          StreamResult result = new StreamResult(file);

          try {
               transformer.transform(source, result);
          } catch (TransformerException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }//end of if condition
          else{
               xmlAppendtest(s);
               count ++;
          }
     }
     
     
     //public void xmlAppendtest(String testcase, Document document, Element root)
     public void xmlAppendtest(String testcase)
     {
          String t2=testcase;
          System.out.println("t2 name is"+ t2 );
          try {
          System.out.print("Enter the password:------------------->>>>>> ");
     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
     Document doc = docBuilder.parse (new File("c:\\test.xml"));

     Element rootElement = doc.getDocumentElement();
     Element elm= doc.createElement("Testcase" + count); // added new
     Element elm1 = doc.createElement("Testcase");
     elm.setAttribute(new String("name"), t2);
     //elm.setAttribute("name","name");
     // elm.setAttribute("status","t2");
     
     /*if(elm.hasAttributes()){
          if(elm.getAttribute("name").equals("Test1")){
               elm.setAttribute("status","Pass");
          }
          
     }*/
     rootElement.appendChild(elm);
     TransformerFactory transformerFactory = TransformerFactory.newInstance();
     Transformer transformer = transformerFactory.newTransformer();
     DOMSource source = new DOMSource(doc);
     StreamResult result = new StreamResult("c:\\test.xml");
     transformer.transform(source, result);
     
                         
     }catch (SAXParseException err) {
     System.out.println ("** Parsing error" + ", line "
     + err.getLineNumber () + ", uri " + err.getSystemId ());
     System.out.println(" " + err.getMessage ());

     }catch (SAXException e) {
     Exception xexe = e.getException ();
     ((xexe == null) ? e : xexe).printStackTrace ();

     }catch (Throwable t) {
     t.printStackTrace ();
     }
          }
     
     public void xmlAppendstatus(String s, String verdictValue)
     {
          String testcasename=s;
          String verdict=verdictValue;
          System.out.println("tc name is"+ testcasename );
          System.out.println("verdict value is"+ verdict );
          try{
               System.out.println("sdfsdf--->entered");
          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
          Document doc = docBuilder.parse (new File("c:\\test.xml"));
          Element rootElement = doc.getDocumentElement();
          //Element elm = doc.getElementById(s1);
          Element elm = doc.createElement("Testcase");
          
          elm.setAttribute(new String("status"), verdict);
          
          //elm.setAttribute("status","status");
          
          // if(elm.hasAttributes()){
               System.out.println("sdfsdf--->entered2");
               System.out.println("attri" +elm.getAttribute("name"));
     if(rootElement.getAttribute("name").equals("s")){
          System.out.println("sdfsdf--->"+s);
     elm.setAttribute("status",verdict);
     }
rootElement.appendChild(elm);

          TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult("c:\\test.xml");
transformer.transform(source, result);
          
     }
          catch (SAXParseException err) {
     System.out.println ("** Parsing error" + ", line "
     + err.getLineNumber () + ", uri " + err.getSystemId ());
     System.out.println(" " + err.getMessage ());

     }catch (SAXException e) {
     Exception xexe = e.getException ();
     ((xexe == null) ? e : xexe).printStackTrace ();

     }catch (Throwable t) {
     t.printStackTrace ();
     }
}

     }

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points