Java by API/javax.xml.stream/XMLStreamConstants
Содержание
XMLStreamConstants.CDATA
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;
}
}
XMLStreamConstants.CHARACTERS
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());
}
}
}
}
XMLStreamConstants.END_ELEMENT
import java.io.FileReader;
import java.io.Reader;
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 factory = XMLInputFactory.newInstance();
Reader fileReader = new FileReader("source.xml");
XMLStreamReader reader = factory.createXMLStreamReader(fileReader);
while (reader.hasNext()) {
process(reader);
reader.next();
}
}
private static void process(XMLStreamReader reader) {
int eventType = reader.getEventType();
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
System.out.println("Start element: " + reader.getLocalName());
int count = reader.getAttributeCount();
for (int i = 0; i < count; i++) {
String name = reader.getAttributeLocalName(i);
String value = reader.getAttributeValue(i);
System.out.println("\tAttribute name/value: " + name + "/" + value);
}
break;
case XMLStreamConstants.END_ELEMENT:
System.out.println("End element: " + reader.getLocalName());
break;
case XMLStreamConstants.CHARACTERS:
System.out.println("Text: " + reader.getText());
break;
default:
break;
}
}
}
XMLStreamConstants.ENTITY_REFERENCE
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());
}
}
}
}
XMLStreamConstants.START_ELEMENT
import java.io.File;
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();
XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));
int eventTypeID = reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "person");
eventTypeID = reader.nextTag();
reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name");
System.out.println(reader.getElementText());
}
}