samedi 1 août 2015

modifying xml through java

I have an excel sheet from which i input a data from a particular cell. After fetching the data, i have to modify the same data into xml..

xml file.

<?xml version="1.0" encoding="UTF-8" standalone="no"?><suite name="google" verbose="3">
    <parameter name="selenium.host" value="localhost"/>
    <parameter name="selenium.port" value="4444"/>
    <parameter name="selenium.browser" value="*firefox"/>
    <parameter name="selenium.url" value="http://www.google.com"/>
    <test name="name2" preserve-order="true">
        <classes>
        </classes>
    </test>
</suite>

I want to pick up the <classes> tag, i want to modify something like this
.....
.....
<classes>
         <class name="xxxx">
                  <method>
                         <include name="yyy">
                         </include>
                  </method>
         </class>
</classes>
....     

here is my java file.

package org.com.test;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.WriteException;

public class TestCaseRunner {
    public static void main(String[] args) 
              throws BiffException, IOException, 
              WriteException, ParserConfigurationException, SAXException, TransformerException
           {
              Workbook workbook = Workbook.getWorkbook(new 
                      File("C:\\Users\\sumanth\\Desktop\\pfa\\TestCaseProfiling.xls"));
              DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 
              domFactory.setIgnoringComments(true);
              DocumentBuilder builder = domFactory.newDocumentBuilder(); 
              Document doc = builder.parse(new File("C:\\Users\\sumanth\\Desktop\\pfa\\test_suite.xml"));             
              Sheet sheet = workbook.getSheet(0);
              NodeList nodes = doc.getElementsByTagName("classes");
              int nodesCount = nodes.getLength();
              System.out.println(nodesCount);
              for(int i=3,j=4;j<=10;j++) {
                  System.out.println("i:"+i + "\t" +"j:"+j+"");
                  Cell cell = sheet.getCell(i, j);
                  String cellExecutionValue = (String)cell.getContents();
                  System.out.println(cellExecutionValue);
                  if(!cellExecutionValue.equals("")){
                      if(cellExecutionValue.equalsIgnoreCase("Yes") ) {
                          Cell cell1 = sheet.getCell(i-1, j);
                          Cell cell2 = sheet.getCell(i-1, j);

                          //Todo : Append Class to classes

                          Element classElement = doc.createElement("class"); 
                          Attr classElementAttribute = doc.createAttribute("name");
                          classElementAttribute.setValue(cell1.getContents());
                          classElement.setAttributeNode(classElementAttribute);


                          Element methodElement = doc.createElement("method");
                          classElement.appendChild(methodElement);

                          Element includeElement = doc.createElement("include");
                          Attr includeElementAttribute = doc.createAttribute("name");
                          includeElementAttribute.setValue(cell2.getContents());
                          includeElement.setAttributeNode(includeElementAttribute);
                          methodElement.appendChild(includeElement);
                    }
                  }

              }
              TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = (Transformer) transformerFactory.newTransformer();
                DOMSource source = new DOMSource(doc);
                StreamResult result = new StreamResult(new File("C:\\Users\\sumanth\\Desktop\\pfa\\test_suite.xml"));
                transformer.transform(source, result);
              workbook.close();
           }
}

Please provide me solution on the same..

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire