Java by API/javax.xml.stream/XMLInputFactory
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
- 1 XMLInputFactory: createFilteredReader(XMLEventReader reader, EventFilter filter)
- 2 XMLInputFactory: createFilteredReader(XMLStreamReader reader, StreamFilter filter)
- 3 XMLInputFactory: createXMLEventReader(InputStream stream)
- 4 XMLInputFactory: createXMLEventReader(Reader reader)
- 5 XMLInputFactory: createXMLStreamReader(InputStream stream)
- 6 XMLInputFactory.IS_COALESCING
- 7 XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES
- 8 XMLInputFactory.setProperty(String name, Object value)
- 9 XMLInputFactory: setXMLResolver(XMLResolver resolver)
XMLInputFactory: createFilteredReader(XMLEventReader reader, EventFilter filter)
import java.io.File;
import java.io.FileInputStream;
import javax.xml.stream.EventFilter;
import javax.xml.stream.StreamFilter;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
public class Main {
public static void main(String[] args) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(new File("a.xml")));
reader = inputFactory.createFilteredReader(reader, new EventFilter() {
public boolean accept(XMLEvent event) {
return false;
}
});
reader = inputFactory.createFilteredReader(reader, new ElementOnlyFilter());
System.out.println(reader.hasNext());
System.out.println(reader.next());
}
}
class ElementOnlyFilter implements EventFilter, StreamFilter {
public boolean accept(XMLEvent event) {
return accept(event.getEventType());
}
public boolean accept(XMLStreamReader reader) {
return accept(reader.getEventType());
}
private boolean accept(int eventType) {
return eventType == XMLStreamConstants.START_ELEMENT
|| eventType == XMLStreamConstants.END_ELEMENT;
}
}
XMLInputFactory: createFilteredReader(XMLStreamReader reader, StreamFilter filter)
import java.io.File;
import java.io.FileInputStream;
import javax.xml.stream.EventFilter;
import javax.xml.stream.StreamFilter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
public class Main {
public static void main(String[] args) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));
System.out.println(countEvents(reader));
reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));
StreamFilter filter = new ElementOnlyFilter();
reader = inputFactory.createFilteredReader(reader, filter);
System.out.println(countEvents(reader));
}
private static int countEvents(XMLStreamReader reader) throws XMLStreamException {
int counter = 1;
while (reader.hasNext()) {
reader.next();
counter++;
}
return counter;
}
}
class ElementOnlyFilter implements EventFilter, StreamFilter {
public boolean accept(XMLEvent event) {
return accept(event.getEventType());
}
public boolean accept(XMLStreamReader reader) {
return accept(reader.getEventType());
}
private boolean accept(int eventType) {
return eventType == XMLStreamConstants.START_ELEMENT
|| eventType == XMLStreamConstants.END_ELEMENT;
}
}
XMLInputFactory: createXMLEventReader(InputStream stream)
import java.io.File;
import java.io.FileInputStream;
import javax.xml.stream.EventFilter;
import javax.xml.stream.StreamFilter;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
public class Main {
public static void main(String[] args) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(new File("a.xml")));
reader = inputFactory.createFilteredReader(reader, new EventFilter() {
public boolean accept(XMLEvent event) {
return false;
}
});
reader = inputFactory.createFilteredReader(reader, new ElementOnlyFilter());
System.out.println(reader.hasNext());
System.out.println(reader.next());
}
}
class ElementOnlyFilter implements EventFilter, StreamFilter {
public boolean accept(XMLEvent event) {
return accept(event.getEventType());
}
public boolean accept(XMLStreamReader reader) {
return accept(reader.getEventType());
}
private boolean accept(int eventType) {
return eventType == XMLStreamConstants.START_ELEMENT
|| eventType == XMLStreamConstants.END_ELEMENT;
}
}
XMLInputFactory: createXMLEventReader(Reader reader)
import java.io.FileReader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;
public class Main {
public static void main(String args[]) throws Exception {
XMLInputFactory xmlif = XMLInputFactory.newInstance();
XMLEventReader xmler = xmlif.createXMLEventReader(new FileReader("yourFile.xml"));
XMLEvent event;
while (xmler.hasNext()) {
event = xmler.nextEvent();
if (event.isStartElement()) {
System.out.println(event.asStartElement().getName());
} else if (event.isCharacters()) {
System.out.println(event.asCharacters().getData());
}
}
}
}
XMLInputFactory: createXMLStreamReader(InputStream stream)
import java.io.File;
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
File file = new File("test.xml");
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(file));
while (reader.getEventType() == 6)
reader.next();
int eventTypeID = reader.next();
System.out.println("Hello " + reader.getText());
}
}
XMLInputFactory.IS_COALESCING
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.CHARACTERS)
System.out.println(reader.getText());
else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
System.out.println("en: " + reader.getLocalName());
System.out.println("er: " + reader.getText());
}
}
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.CHARACTERS)
System.out.println(reader.getText());
else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
System.out.println("en: " + reader.getLocalName());
System.out.println("er: " + reader.getText());
}
}
}
}
XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.CHARACTERS)
System.out.println(reader.getText());
else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
System.out.println("en: " + reader.getLocalName());
System.out.println("er: " + reader.getText());
}
}
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.CHARACTERS)
System.out.println(reader.getText());
else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
System.out.println("en: " + reader.getLocalName());
System.out.println("er: " + reader.getText());
}
}
}
}
XMLInputFactory.setProperty(String name, Object value)
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.CHARACTERS)
System.out.println(reader.getText());
else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
System.out.println("en: " + reader.getLocalName());
System.out.println("er: " + reader.getText());
}
}
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE);
reader = inputFactory.createXMLStreamReader(new FileInputStream("e.xml"));
while (reader.hasNext()) {
int event = reader.next();
if (event == XMLStreamConstants.CHARACTERS)
System.out.println(reader.getText());
else if (event == XMLStreamConstants.ENTITY_REFERENCE) {
System.out.println("en: " + reader.getLocalName());
System.out.println("er: " + reader.getText());
}
}
}
}
XMLInputFactory: setXMLResolver(XMLResolver resolver)
import java.io.FileInputStream;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLResolver;
import javax.xml.stream.XMLStreamException;
public class Main {
public static void main(String[] args) throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setXMLResolver(new XMLResolver() {
public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace)
throws XMLStreamException {
System.out.println(systemID);
return null;
}
});
XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(
"dtd.xml"));
XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);
writer.add(reader);
writer.flush();
}
}