javax.xml.stream.XMLEventWriter Java Examples

The following examples show how to use javax.xml.stream.XMLEventWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AnimalRepository.java    From JavaMainRepo with Apache License 2.0 6 votes vote down vote up
public static void createNode(XMLEventWriter eventWriter, String name, String value) throws XMLStreamException {
	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");
	XMLEvent tab = eventFactory.createDTD("\t");
	// Create Start node
	StartElement sElement = eventFactory.createStartElement("", "", name);
	eventWriter.add(tab);
	eventWriter.add(sElement);
	// Create Content
	Characters characters = eventFactory.createCharacters(value);
	eventWriter.add(characters);
	// Create End node
	EndElement eElement = eventFactory.createEndElement("", "", name);
	eventWriter.add(eElement);
	eventWriter.add(end);
}
 
Example #2
Source File: EntityRepository.java    From JavaMainRepo with Apache License 2.0 6 votes vote down vote up
public static void createNode(XMLEventWriter eventWriter, String name, String value) throws XMLStreamException {
	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");
	XMLEvent tab = eventFactory.createDTD("\t");

	// Create Start node
	StartElement sElement = eventFactory.createStartElement("", "", name);
	eventWriter.add(tab);
	eventWriter.add(sElement);

	// Create Content
	Characters characters = eventFactory.createCharacters(value);
	eventWriter.add(characters);

	// Create End node
	EndElement eElement = eventFactory.createEndElement("", "", name);
	eventWriter.add(eElement);
	eventWriter.add(end);

}
 
Example #3
Source File: EntityRepository.java    From JavaMainRepo with Apache License 2.0 6 votes vote down vote up
public static void createNode(XMLEventWriter eventWriter, String name, String value) throws XMLStreamException {
	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");
	XMLEvent tab = eventFactory.createDTD("\t");

	// Create Start node
	StartElement sElement = eventFactory.createStartElement("", "", name);
	eventWriter.add(tab);
	eventWriter.add(sElement);

	// Create Content
	Characters characters = eventFactory.createCharacters(value);
	eventWriter.add(characters);

	// Create End node
	EndElement eElement = eventFactory.createEndElement("", "", name);
	eventWriter.add(eElement);
	eventWriter.add(end);

}
 
Example #4
Source File: AnimalRepository.java    From JavaMainRepo with Apache License 2.0 6 votes vote down vote up
public static void createNode(XMLEventWriter eventWriter, String name, String value) throws XMLStreamException {

		XMLEventFactory eventFactory = XMLEventFactory.newInstance();
		XMLEvent end = eventFactory.createDTD("\n");
		XMLEvent tab = eventFactory.createDTD("\t");

		// Create start node
		StartElement sElement = eventFactory.createStartElement("", "", name);
		eventWriter.add(tab);
		eventWriter.add(sElement);

		// Create content

		Characters characters = eventFactory.createCharacters(value);
		eventWriter.add(characters);

		// Create end node

		EndElement eElement = eventFactory.createEndElement("", "", name);
		eventWriter.add(eElement);
		eventWriter.add(end);
	}
 
Example #5
Source File: StaxResult.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}.
 * @param eventWriter the {@code XMLEventWriter} to write to
 */
public StaxResult(XMLEventWriter eventWriter) {
	StaxEventHandler handler = new StaxEventHandler(eventWriter);
	super.setHandler(handler);
	super.setLexicalHandler(handler);
	this.eventWriter = eventWriter;
}
 
Example #6
Source File: AnimalRepository.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void save(ArrayList<Animal> animals) throws FileNotFoundException, XMLStreamException {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));

	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");

	StartDocument startDocument = eventFactory.createStartDocument();
	eventWriter.add(startDocument);

	StartElement configStartElement = eventFactory.createStartElement("", "", "content");
	eventWriter.add(configStartElement);
	eventWriter.add(end);

	for (XML_Parsable animal : animals) {
		StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.ANIMAL);
		eventWriter.add(sElement);
		eventWriter.add(end);

		animal.encodeToXml(eventWriter);

		EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.ANIMAL);
		eventWriter.add(eElement);
		eventWriter.add(end);
	}

	eventWriter.add(eventFactory.createEndElement("", "", "content"));
	eventWriter.add(eventFactory.createEndDocument());
	eventWriter.close();
}
 
Example #7
Source File: EntityRepository.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));

	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");

	StartDocument startDocument = eventFactory.createStartDocument();
	eventWriter.add(startDocument);

	StartElement configStartElement = eventFactory.createStartElement("", "", "content");
	eventWriter.add(configStartElement);
	eventWriter.add(end);

	for (XML_Parsable entity : entities) {
		StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
		eventWriter.add(sElement);
		eventWriter.add(end);

		entity.encodeToXml(eventWriter);

		EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
		eventWriter.add(eElement);
		eventWriter.add(end);
	}

	eventWriter.add(eventFactory.createEndElement("", "", "content"));
	eventWriter.add(eventFactory.createEndDocument());
	eventWriter.close();
}
 
Example #8
Source File: Caretaker.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {  
 
 super.encodeToXml(eventWriter);  
 createNode(eventWriter, "workingHours", String.valueOf(getWorkingHours()));  
 super.encodeToXml(eventWriter);  
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.EmployeesType.Caretakers); 
	  
}
 
Example #9
Source File: Employee.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
/**
 * @param eventWriter
 *            Text to encode.
 * @throws XMLStreamException
 *             .
 */
public void encodeToXml(final XMLEventWriter eventWriter) throws XMLStreamException {
	createNode(eventWriter, "name", String.valueOf(this.name));
	createNode(eventWriter, "isDead", String.valueOf(this.isDead));
	createNode(eventWriter, "ID", String.valueOf(this.id));
	createNode(eventWriter, "Salary", String.valueOf(this.salary));
}
 
Example #10
Source File: Animal.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	createNode(eventWriter, "nrOfLegs", String.valueOf(this.nrOfLegs));
	createNode(eventWriter, "name", String.valueOf(this.name));
	createNode(eventWriter, "maintenanceCost", String.valueOf(this.maintenanceCost));
	createNode(eventWriter, "dangerPerc", String.valueOf(this.dangerPerc));
	createNode(eventWriter, "takenCareOf", String.valueOf(this.takeCareOf));
}
 
Example #11
Source File: XMLUtilities.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
protected static XMLEventWriter getEventWriter(final OutputStream os) throws XMLStreamException {
  if (ofactory == null) {
    ofactory = XMLOutputFactory.newInstance();
  }

  return ofactory.createXMLEventWriter(os, "UTF-8");
}
 
Example #12
Source File: ArtifactsXmlAbsoluteUrlRemover.java    From nexus-repository-p2 with Eclipse Public License 1.0 5 votes vote down vote up
private void streamXmlToWriterAndRemoveAbsoluteUrls(
    final XMLEventReader reader,
    final XMLEventWriter writer) throws XMLStreamException
{
  final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
  XMLEvent previous = null;

  // We need to buffer events so that we can also update properties size when removing the mirrorsUrl property
  List<XMLEvent> buffer = new ArrayList<>();
  while (reader.hasNext()) {
    XMLEvent event = reader.nextEvent();

    // If xml tag is "properties" then start buffering. If started buffering then keep buffering until the buffer
    // is cleared
    if (!buffer.isEmpty() || isStartTagWithName(event, "properties")) {
      //Exclude the mirrorsURL property
      if (!(isMirrorsUrlProperty(event) || isMirrorsUrlProperty(previous))) {
        buffer.add(event);
      }
    }

    if (buffer.isEmpty()) {
      writer.add(event);
    }

    // When reached end of <properties> section, update count and flush buffer to writer.
    if (isEndTagWithName(event, "properties")) {
      for (XMLEvent xmlEvent : buffer) {
        if (isStartTagWithName(xmlEvent, "properties")) {
          xmlEvent = updateSize(xmlEvent.asStartElement(), countPropertyTags(buffer), eventFactory);
        }
        writer.add(xmlEvent);
      }
      buffer.clear();
    }

    previous = event;
  }
}
 
Example #13
Source File: Animal.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter)throws XMLStreamException{
	createNode(eventWriter, "nrOfLegs", String.valueOf(this.nrOfLegs));
	createNode(eventWriter, "name", String.valueOf(this.name));
	createNode(eventWriter, "maintenanceCost", String.valueOf(this.maintenanceCost));
	createNode(eventWriter, "dangerPerc", String.valueOf(this.dangerPerc));
	createNode(eventWriter, "takenCareOf", String.valueOf(this.takenCareOf));
}
 
Example #14
Source File: AbstractDOMDocumentParser.java    From jettison with Apache License 2.0 5 votes vote down vote up
public Document parse(InputStream input) throws IOException {
	try {
         XMLStreamReader streamReader = inputFactory.createXMLStreamReader(input);
         XMLInputFactory readerFactory = XMLInputFactory.newInstance();
         XMLEventReader eventReader = readerFactory.createXMLEventReader(streamReader);
         
         // Can not create a STaX writer for a DOMResult in woodstox-3.1.1
         /*XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
		 Document nsDom = getDocumentBuilder().newDocument();
		 DOMResult result = new DOMResult(nsDom);
         XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(result);*/
         
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
         XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
         XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(baos);
         
         eventWriter.add(eventReader);
         eventWriter.close();
         
         // This parsing step should not be necessary, if we could output to a DOMResult
         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
         return getDocumentBuilder().parse(bais);
         
         //return nsDom;
		
	} catch (Exception ex) {
		IOException ioex = new IOException("Cannot parse input stream");
		ioex.initCause(ex);
		throw ioex;
	}
}
 
Example #15
Source File: XMLUtilities.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public InputStream addAtomInlinecount(final InputStream feed, final int count) throws Exception {
  final XMLEventReader reader = getEventReader(feed);

  final ByteArrayOutputStream bos = new ByteArrayOutputStream();
  final XMLEventWriter writer = getEventWriter(bos);

  try {

    final XMLElement feedElement =
        extractElement(reader, writer, Collections.<String> singletonList("feed"), 0, 1, 1).getValue();

    writer.add(feedElement.getStart());
    addAtomElement(IOUtils.toInputStream(String.format("<m:count>%d</m:count>", count), Constants.ENCODING), writer);
    writer.add(feedElement.getContentReader());
    writer.add(feedElement.getEnd());

    while (reader.hasNext()) {
      writer.add(reader.nextEvent());
    }

  } finally {
    writer.flush();
    writer.close();
    reader.close();
    IOUtils.closeQuietly(feed);
  }

  return new ByteArrayInputStream(bos.toByteArray());
}
 
Example #16
Source File: StaxResult.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Construct a new instance of the {@code StaxResult} with the specified {@code XMLEventWriter}.
 * @param eventWriter the {@code XMLEventWriter} to write to
 */
public StaxResult(XMLEventWriter eventWriter) {
	StaxEventHandler handler = new StaxEventHandler(eventWriter);
	super.setHandler(handler);
	super.setLexicalHandler(handler);
	this.eventWriter = eventWriter;
}
 
Example #17
Source File: XStreamMarshallerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void marshalStaxResultXMLEventWriter() throws Exception {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	StringWriter writer = new StringWriter();
	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
	Result result = StaxUtils.createStaxResult(eventWriter);
	marshaller.marshal(flight, result);
	assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
}
 
Example #18
Source File: EntityRepository.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void save(ArrayList<T> entities) throws FileNotFoundException, XMLStreamException {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();

	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));

	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");

	StartDocument startDocument = eventFactory.createStartDocument();
	eventWriter.add(startDocument);

	StartElement configStartElement = eventFactory.createStartElement("", "", "content");
	eventWriter.add(configStartElement);
	eventWriter.add(end);

	for (XML_Parsable entity : entities) {
		StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
		eventWriter.add(sElement);
		eventWriter.add(end);

		entity.encodeToXml(eventWriter);

		EndElement eElement = eventFactory.createEndElement("", "", this.entityTag);
		eventWriter.add(eElement);
		eventWriter.add(end);
	}

	eventWriter.add(eventFactory.createEndElement("", "", "content"));
	eventWriter.add(eventFactory.createEndDocument());
	eventWriter.close();
}
 
Example #19
Source File: Animal.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	createNode(eventWriter, "nrOfLegs", String.valueOf(this.nrOfLegs));
	createNode(eventWriter, "name", String.valueOf(this.name));
	createNode(eventWriter, "maintenanceCost", String.valueOf(this.maintenanceCost));
	createNode(eventWriter, "dangerPerc", String.valueOf(this.dangerPerc));
	createNode(eventWriter, "takenCareOf", String.valueOf(this.takenCareOf));
}
 
Example #20
Source File: PathAndType2LowerCaseTransformer.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void convert( final XMLEvent event, final XMLEventWriter eventWriter ) throws XMLStreamException {
	// <attribute xsi:type="pathAttribute" key="path">Window/JRootPane_0/JPane_1</attribute>
	if ( isStartElementNamed( event, tagname ) && hasAttribute( event, "key", "path" ) ) {
		isInPath = true;
	}

	if ( isInPath && event.isCharacters() ) {
		path += event.asCharacters().getData().trim();
		return;
	}

	if ( isInPath && isEndElementNamed( event, tagname ) ) {
		eventWriter.add( characters( path.toLowerCase() ) );
		isInPath = false;
		path = "";
	}

	if ( isStartElementNamed( event, tagname ) && hasAttribute( event, "key", "type" ) ) {
		isInType = true;
	}

	if ( isInType && event.isCharacters() ) {
		type += event.asCharacters().getData().trim();
		return;
	}

	if ( isInType && isEndElementNamed( event, tagname ) ) {
		eventWriter.add( characters( type.toLowerCase() ) );
		isInType = false;
		type = "";
	}

	eventWriter.add( event );
}
 
Example #21
Source File: Spider.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	super.encodeToXml(eventWriter);
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.Animal.Insect.Spider);
}
 
Example #22
Source File: Aquatic.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	super.encodeToXml(eventWriter);
	createNode(eventWriter, "avgSwimDepth", String.valueOf(this.avgSwimDepth));
}
 
Example #23
Source File: Goldfish.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	super.encodeToXml(eventWriter);
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.Animals.Aquatics.Goldfish);
}
 
Example #24
Source File: Caretaker.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	super.encodeToXml(eventWriter);
	createNode(eventWriter, "workingHours", String.valueOf(this.workingHours));
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.Employees.Caretaker);
}
 
Example #25
Source File: Duck.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException  { 
	super.encodeToXml(eventWriter);  
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.Animals.Birds.Duck); 
}
 
Example #26
Source File: Whale.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	super.encodeToXml(eventWriter);
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.Animals.Aquatics.Whale);
}
 
Example #27
Source File: Employee.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	createNode(eventWriter, "id", String.valueOf(this.id));
	createNode(eventWriter, "name", String.valueOf(this.name));
	createNode(eventWriter, "salary", String.valueOf(this.salary));
	createNode(eventWriter, "isDead", String.valueOf(this.isDead));
}
 
Example #28
Source File: Viper.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	super.encodeToXml(eventWriter);
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.Animals.Reptiles.Viper);
}
 
Example #29
Source File: Dolphin.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	super.encodeToXml(eventWriter);
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.Animals.Aquatics.Dolphin);
}
 
Example #30
Source File: Cockroach.java    From JavaMainRepo with Apache License 2.0 4 votes vote down vote up
public void encodeToXml(XMLEventWriter eventWriter) throws XMLStreamException {
	super.encodeToXml(eventWriter);
	createNode(eventWriter, Constants.XML_TAGS.DISCRIMINANT, Constants.Animals.Insects.Cockroach);
}