1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import org.xml.sax.*; import org.xml.sax.ext.*; import org.xml.sax.helpers.*; import java.io.IOException; public class SAXEncodingDetector extends DefaultHandler { public static void main(String[] args) throws SAXException, IOException { XMLReader parser = XMLReaderFactory.createXMLReader(); SAXEncodingDetector handler = new SAXEncodingDetector(); parser.setContentHandler(handler); for (int i = 0; i < args.length; i++) { try { parser.parse(args); } catch (SAXException ex) { System.out.println(handler.encoding); } } } private String encoding; private Locator2 locator; _cnnew1@Override public void setDocumentLocator(Locator locator) { if (locator instanceof Locator2) { this.locator = (Locator2) locator; } else { this.encoding = "unknown"; } } @Override public void startDocument() throws SAXException { if (locator != null) { this.encoding = locator.getEncoding(); } throw new SAXException("Early termination"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.IOException; import org.apache.xerces.parsers.*; import org.apache.xerces.xni.*; import org.apache.xerces.xni.parser.*; public class XNIEncodingDetector extends XMLDocumentParser { public static void main(String[] args) throws XNIException, IOException { XNIEncodingDetector parser = new XNIEncodingDetector(); for (int i = 0; i < args.length; i++) { try { XMLInputSource document = new XMLInputSource("", args, ""); parser.parse(document); } catch (XNIException ex) { System.out.println(parser.encoding); } } } private String encoding = "unknown"; @Override public void startDocument(XMLLocator locator, String encoding, NamespaceContext context, Augmentations augs) throws XNIException { this.encoding = encoding; throw new XNIException("Early termination"); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.io.IOException; import org.apache.xerces.parsers.*; import org.apache.xerces.xni.*; import org.apache.xerces.xni.parser.*; public class AdvancedXNIEncodingDetector extends XMLDocumentParser { public static void main(String[] args) throws XNIException, IOException { AdvancedXNIEncodingDetector parser = new AdvancedXNIEncodingDetector(); for (int i = 0; i < args.length; i++) { try { XMLInputSource document = new XMLInputSource("", args, ""); parser.parse(document); } catch (XNIException ex) { System.out.println("Actual: " + parser.actualEncoding); System.out.println("Declared: " + parser.declaredEncoding); } } } private String actualEncoding = "unknown"; private String declaredEncoding = "none"; @Override public void startDocument(XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs) throws XNIException { this.actualEncoding = encoding; this.declaredEncoding = "none"; // reset } @Override // this method is not called if there's no XML declaration public void xmlDecl(String version, String encoding, String standalone, Augmentations augs) throws XNIException { this.declaredEncoding = encoding; } @Override public void startElement(QName element, XMLAttributes attributes, Augmentations augs) throws XNIException { throw new XNIException("Early termination"); } } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |