Java by API/javax.xml.stream/XMLStreamReader — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

XMLStreamReader: getElementText()

  
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());
  }
}





XMLStreamReader: getEventType()

  
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());
  }
}





XMLStreamReader.getLocalName()

  

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());
      }
    }
  }
}





XMLStreamReader: getLocation()

  

import java.io.File;
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
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();
    try {
      reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name");
    } catch (XMLStreamException e) {
      System.out.println("Assertion failed. " + e.getMessage() + " at "
          + reader.getLocation().getLineNumber() + ":" + reader.getLocation().getColumnNumber());
    }
    System.out.println(reader.getElementText());
  }
}





XMLStreamReader: getName()

  

import java.io.FileReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
public class Main {
  public static void main(String args[]) throws Exception {
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLStreamReader xmlsr = xmlif.createXMLStreamReader(new FileReader("points.xml"));
    int eventType;
    while (xmlsr.hasNext()) {
      eventType = xmlsr.next();
      switch (eventType) {
      case XMLEvent.START_ELEMENT:
        System.out.println(xmlsr.getName());
        break;
      case XMLEvent.CHARACTERS:
        System.out.println(xmlsr.getText());
        break;
      default:
        break;
      }
    }
  }
}





XMLStreamReader: getText()

  
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());
  }
}





XMLStreamReader: hasNext()

  
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;
  }
}





XMLStreamReader: next()

  
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());
  }
}





XMLStreamReader: nextTag()

  

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("text.xml");
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(file));
    int eventTypeID = reader.nextTag();
    eventTypeID = reader.nextTag();
    eventTypeID = reader.next();
    System.out.println("Hello " + reader.getText());
  }
}





XMLStreamReader: require(int type, String namespaceURI, String localName)

  

import java.io.File;
import java.io.FileInputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
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();
    try {
      reader.require(XMLStreamConstants.START_ELEMENT, null, "first_name");
    } catch (XMLStreamException e) {
      System.out.println("Assertion failed. " + e.getMessage() + " at "
          + reader.getLocation().getLineNumber() + ":" + reader.getLocation().getColumnNumber());
    }
    System.out.println(reader.getElementText());
  }
}