15 Ağustos 2018 Çarşamba

StaX XMLEvent Arayüzü

constructor
Şöyle yaparız.
XMLEventReader xmlEventReader = ...;

while (xmlEventReader.hasNext()) {

   XMLEvent xmlEvent = xmlEventReader.nextEvent();
   ...
}
isEndElement metodu
Şöyle yaparız.
if (xmlEvent.isEndElement()) {
  EndElement endElement = xmlEvent.asEndElement();
  if(endElement.getName().getLocalPart().equalsIgnoreCase("customer")){
    // all data for the current Customer has been read
    // do something with the customer, like logging it or storing it in a database
    // after this the customer variable will be re-assigned to the next customer
  }
}
isStartElement metodu
Şöyle yaparız.
XMLEvent xmlEvent = xmlEventReader.nextEvent();
if (xmlEvent.isStartElement()) {

  StartElement startElement = xmlEvent.asStartElement();

  if (startElement.getName().getLocalPart().equalsIgnoreCase("customer")) {
    // start populating a new customer
    customer = new Customer();

    // read an attribute for example <customer number="42">
    Attribute attribute = startElement.getAttributeByName(new QName("number"));
    if (attribute != null) {
      customer.setNumber(attribute.getValue());
    }
  }

  // read a nested element for example:
  // <customer>
  //    <name>John Doe</name>
  if(startElement.getName().getLocalPart().equals("name")){
    xmlEvent = xmlEventReader.nextEvent();
    customer.setName(xmlEvent.asCharacters().getData());
  }
}

Hiç yorum yok:

Yorum Gönder