Java by API/javax.xml.transform.stream/StreamResult

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

new StreamResult(OutputStream outputStream)

   <source lang="java">

/** <?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>";  

}

      </source>