Java by API/javax.xml.stream/XMLEventReader
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
XMLEventReader: hasNext()
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());
}
}
}
}
XMLEventReader: nextEvent() throws XMLStreamException
import java.io.FileInputStream;
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 {
String filename = "yourXML.xml";
XMLInputFactory factory = XMLInputFactory.newInstance();
System.out.println("FACTORY: " + factory);
XMLEventReader r = factory.createXMLEventReader(filename,
new FileInputStream(filename));
while (r.hasNext()) {
XMLEvent e = r.nextEvent();
System.out.println(e.toString());
}
}
public static final String getEventTypeString(int eventType) {
switch (eventType) {
case XMLEvent.START_ELEMENT:
return "START_ELEMENT";
case XMLEvent.END_ELEMENT:
return "END_ELEMENT";
case XMLEvent.PROCESSING_INSTRUCTION:
return "PROCESSING_INSTRUCTION";
case XMLEvent.CHARACTERS:
return "CHARACTERS";
case XMLEvent.ruMENT:
return "COMMENT";
case XMLEvent.START_DOCUMENT:
return "START_DOCUMENT";
case XMLEvent.END_DOCUMENT:
return "END_DOCUMENT";
case XMLEvent.ENTITY_REFERENCE:
return "ENTITY_REFERENCE";
case XMLEvent.ATTRIBUTE:
return "ATTRIBUTE";
case XMLEvent.DTD:
return "DTD";
case XMLEvent.CDATA:
return "CDATA";
case XMLEvent.SPACE:
return "SPACE";
}
return "UNKNOWN_EVENT_TYPE " + "," + eventType;
}
}
XMLEventWriter: add(XMLEvent event) throws XMLStreamException
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Namespace;
public class Main {
public static void main(String[] args) throws Exception {
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);
Namespace ns1 = eventFactory.createNamespace("ns1", "http://www.e.ru/ns1");
Namespace ns2 = eventFactory.createNamespace("ns2", "http://www.e.ru/ns2");
List<Namespace> namespaceList = new ArrayList<Namespace>();
namespaceList.add(ns1);
namespaceList.add(ns2);
Attribute attribute = eventFactory.createAttribute(ns2.getPrefix(), ns2.getNamespaceURI(),
"attribute", "true");
writer.add(eventFactory.createStartElement(ns1.getPrefix(), ns1.getNamespaceURI(), "sample",
Collections.singletonList(attribute).iterator(), namespaceList.iterator()));
writer.add(eventFactory.createEndDocument());
writer.flush();
}
}
XMLEventWriter: add(XMLEventReader reader) throws XMLStreamException
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();
}
}