Java by API/javax.xml.transform/Transformer — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:17, 31 мая 2010

Transformer: setOutputProperty(String name, String value)

  
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Main {
  public static void main(String args[]) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();
    Element results = doc.createElement("Results");
    doc.appendChild(results);
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager
        .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");
    
    ResultSet rs = con.createStatement().executeQuery("select * from product");
    ResultSetMetaData rsmd = rs.getMetaData();
    int colCount = rsmd.getColumnCount();
    while (rs.next()) {
      Element row = doc.createElement("Row");
      results.appendChild(row);
      for (int i = 1; i <= colCount; i++) {
        String columnName = rsmd.getColumnName(i);
        Object value = rs.getObject(i);
        Element node = doc.createElement(columnName);
        node.appendChild(doc.createTextNode(value.toString()));
        row.appendChild(node);
      }
    }
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    System.out.println(sw.toString());
    con.close();
    rs.close();
  }
}





Transformer: transform(Source xmlSource, Result outputTarget)

 
/**
<?xml version="1.0" encoding="UTF-8"?>
<PHONEBOOK>  <PERSON>   <NAME>Joe Wang</NAME>   <EMAIL>joe@yourserver.ru</EMAIL>   
<TELEPHONE>202-999-9999</TELEPHONE>   <WEB>www.jexp.ru</WEB>  </PERSON>  </PHONEBOOK> 
 * */
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class MainClass {
  public static void main(String[] args) throws Exception{
    DocumentBuilder parser = 
      DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = parser.parse( new InputSource(new StringReader(xmlString)) );
    Transformer transformer = 
      TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource( document );
    Result output = new StreamResult( System.out );
    transformer.transform( source, output );
  }
  
  static String xmlString = "<PHONEBOOK>" +
      "  <PERSON>" +
      "   <NAME>Joe Wang</NAME>" +
      "   <EMAIL>joe@yourserver.ru</EMAIL>" +
      "   <TELEPHONE>202-999-9999</TELEPHONE>" +
      "   <WEB>www.jexp.ru</WEB>" +
      "  </PERSON>" +
      "  </PHONEBOOK>";  
}