1 7 8 package org.dom4j.io; 9 10 import junit.textui.TestRunner; 11 12 import java.io.File ; 13 import java.io.FileReader ; 14 import java.io.StringWriter ; 15 16 import javax.xml.stream.XMLInputFactory; 17 18 import org.dom4j.AbstractTestCase; 19 import org.dom4j.Document; 20 21 27 public class StaxTest extends AbstractTestCase { 28 public static void main(String [] args) { 29 TestRunner.run(StaxTest.class); 30 } 31 32 35 39 public void testEncoding() { 40 43 try { 44 XMLInputFactory.newInstance(); 45 } catch (javax.xml.stream.FactoryConfigurationError e) { 46 return; 48 } 49 50 try { 51 File file = getFile("/xml/russArticle.xml"); 52 STAXEventReader xmlReader = new STAXEventReader(); 53 Document doc = xmlReader.readDocument(new FileReader (file)); 54 55 assertEquals("russArticle.xml encoding wasn't correct", "koi8-r", 56 doc.getXMLEncoding()); 57 58 StringWriter writer = new StringWriter (); 59 STAXEventWriter xmlWriter = new STAXEventWriter(writer); 60 xmlWriter.writeDocument(doc); 61 62 String output = writer.toString(); 63 String xmlDecl = output.substring(0, output.indexOf("?>") + 2); 64 String expected = "<?xml version=\'1.0\' encoding=\'koi8-r\'?>"; 65 assertEquals("Unexpected xml declaration", expected, xmlDecl); 66 System.out.println(output); 67 } catch (Exception e) { 68 e.printStackTrace(); 69 fail(e.getMessage()); 70 } 71 } 72 } 73 74 110 | Popular Tags |