Java Code Examples for javax.xml.stream.XMLEventWriter#add()

The following examples show how to use javax.xml.stream.XMLEventWriter#add() . 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: AbstractDOMDocumentSerializer.java    From jettison with Apache License 2.0 6 votes vote down vote up
public void serialize(Element el) throws IOException {
	if (output == null)
		throw new IllegalStateException("OutputStream cannot be null");

	try {
		DOMSource source = new DOMSource(el);
		XMLInputFactory readerFactory = XMLInputFactory.newInstance();
		XMLStreamReader streamReader = readerFactory
				.createXMLStreamReader(source);
		XMLEventReader eventReader = readerFactory
				.createXMLEventReader(streamReader);

		XMLEventWriter eventWriter = writerFactory
				.createXMLEventWriter(output);
		eventWriter.add(eventReader);
		eventWriter.close();
	} catch (XMLStreamException ex) {
		IOException ioex = new IOException("Cannot serialize: " + el);
		ioex.initCause(ex);
		throw ioex;
	}
}
 
Example 2
Source File: EmployeeRepository.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: XMLEventReaderHandle.java    From java-client-api with Apache License 2.0 6 votes vote down vote up
@Override
public void write(OutputStream out) throws IOException {
  try {
    XMLOutputFactory factory = XMLOutputFactory.newFactory();
    XMLEventWriter   writer  = factory.createXMLEventWriter(out, "UTF-8");

    writer.add(content);
    writer.flush();
    writer.close();

    content.close();
  } catch (XMLStreamException e) {
    logger.error("Failed to parse StAX events from input stream",e);
    throw new MarkLogicInternalException(e);
  }
}
 
Example 5
Source File: EmployeeRepository.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 6
Source File: StaxXMLWriter.java    From journaldev with MIT License 6 votes vote down vote up
private static void createNode(XMLEventWriter eventWriter, String element,
    String value) throws XMLStreamException {
  XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
  XMLEvent end = xmlEventFactory.createDTD("\n");
  XMLEvent tab = xmlEventFactory.createDTD("\t");
  //Create Start node
  StartElement sElement = xmlEventFactory.createStartElement("", "", element);
  eventWriter.add(tab);
  eventWriter.add(sElement);
  //Create Content
  Characters characters = xmlEventFactory.createCharacters(value);
  eventWriter.add(characters);
  // Create End node
  EndElement eElement = xmlEventFactory.createEndElement("", "", element);
  eventWriter.add(eElement);
  eventWriter.add(end);

}
 
Example 7
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 8
Source File: EmployeeRepository.java    From JavaMainRepo with Apache License 2.0 5 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");
	StartElement sElement = eventFactory.createStartElement("", "", name);
	eventWriter.add(tab);
	eventWriter.add(sElement);
	Characters characters = eventFactory.createCharacters(value);
	eventWriter.add(characters);
	EndElement eElement = eventFactory.createEndElement("", "", name);
	eventWriter.add(eElement);
	eventWriter.add(end);
}
 
Example 9
Source File: ExclusionFiles.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
private static void writeXmlEvents(
    XMLEventWriter writer, SymbolProblem symbolProblem, ClassFile classFile)
    throws XMLStreamException {
  writer.add(eventFactory.createStartElement(LINKAGE_ERROR_TAG, null, null));

  writer.add(eventFactory.createStartElement(TARGET_TAG, null, null));
  writeXmlElement(writer, symbolProblem.getSymbol());
  writer.add(eventFactory.createEndElement(TARGET_TAG, null));

  writer.add(eventFactory.createStartElement(SOURCE_TAG, null, null));
  writeXmlElement(writer, classFile);
  writer.add(eventFactory.createEndElement(SOURCE_TAG, null));

  writer.add(eventFactory.createEndElement(LINKAGE_ERROR_TAG, null));
}
 
Example 10
Source File: StaxWriter.java    From maven-framework-project with MIT License 5 votes vote down vote up
public void saveConfig() throws Exception {
	// Create a XMLOutputFactory
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	// Create XMLEventWriter
	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(configFile));
	// Create a EventFactory
	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	
	//XMLEvent end = eventFactory.createDTD("\n");
	
	// Create and write Start Tag
	StartDocument startDocument = eventFactory.createStartDocument();
	eventWriter.add(startDocument);

	// Create config open tag
	StartElement configStartElement = eventFactory.createStartElement("","", "config");
	eventWriter.add(configStartElement);
	
	//eventWriter.add(end);
	
	// Write the different nodes
	createNode(eventWriter, "mode", "1");
	createNode(eventWriter, "unit", "901");
	createNode(eventWriter, "current", "100");
	createNode(eventWriter, "interactive", "5");

	eventWriter.add(eventFactory.createEndElement("", "", "config"));
	
	//eventWriter.add(end);
	
	eventWriter.add(eventFactory.createEndDocument());
	eventWriter.close();
}
 
Example 11
Source File: ReplaceAttributeTransformer.java    From recheck with GNU Affero General Public License v3.0 5 votes vote down vote up
@SuppressWarnings( "unchecked" )
@Override
public void convert( final XMLEvent event, final XMLEventWriter eventWriter ) throws XMLStreamException {
	if ( isStartElementNamed( event, tagname ) ) {

		final StartElement startElement = event.asStartElement();
		eventWriter.add( newline() );
		eventWriter.add( eventFactory.createStartElement( startElement.getName(),
				convertAttributes( startElement.getAttributes() ), startElement.getNamespaces() ) );
		return;
	}

	eventWriter.add( event );
}
 
Example 12
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 13
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();
	// Create XMLEventWriter
	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(this.xmlFilename));
	// Create a EventFactory
	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");
	// Create and write Start Tag
	StartDocument startDocument = eventFactory.createStartDocument();
	eventWriter.add(startDocument);
	// Create content open tag
	StartElement configStartElement = eventFactory.createStartElement("", "", "content");
	eventWriter.add(configStartElement);
	eventWriter.add(end);
	for (XML_Parsable animal : entities) {
		StartElement sElement = eventFactory.createStartElement("", "", this.entityTag);
		eventWriter.add(sElement);
		eventWriter.add(end);
		animal.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 14
Source File: StaxXMLWriter.java    From journaldev with MIT License 5 votes vote down vote up
public void writeXML(String fileName, String rootElement, Map<String, String> elementsMap){
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        try {
//            XMLEventWriter xmlEventWriter = xmlOutputFactory
//                    .createXMLEventWriter(new FileOutputStream(fileName), "UTF-8");
            //For Debugging - below code to print XML to Console
            XMLEventWriter xmlEventWriter = xmlOutputFactory.createXMLEventWriter(System.out);
            XMLEventFactory eventFactory = XMLEventFactory.newInstance();
            XMLEvent end = eventFactory.createDTD("\n");
            StartDocument startDocument = eventFactory.createStartDocument();
            xmlEventWriter.add(startDocument);
            xmlEventWriter.add(end);
            StartElement configStartElement = eventFactory.createStartElement("",
                "", rootElement);
            xmlEventWriter.add(configStartElement);
            xmlEventWriter.add(end);
            // Write the element nodes
            Set<String> elementNodes = elementsMap.keySet();
            for(String key : elementNodes){
                createNode(xmlEventWriter, key, elementsMap.get(key));
            }
            
            xmlEventWriter.add(eventFactory.createEndElement("", "", rootElement));
            xmlEventWriter.add(end);
            xmlEventWriter.add(eventFactory.createEndDocument());
            xmlEventWriter.close();

        } catch ( XMLStreamException e) {
            e.printStackTrace();
        }
    }
 
Example 15
Source File: EmployeeRepository.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void save(ArrayList<Employee> employees) throws FileNotFoundException, XMLStreamException {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	// Create XMLEventWriter
	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
	// Create a EventFactory
	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");
	// Create and write Start Tag
	StartDocument startDocument = eventFactory.createStartDocument();
	eventWriter.add(startDocument);
	// Create content open tag
	StartElement configStartElement = eventFactory.createStartElement("", "", "content");
	eventWriter.add(configStartElement);
	eventWriter.add(end);
	for (XML_Parsable employee : employees) {
		StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.EMPLOYEE);
		eventWriter.add(sElement);
		eventWriter.add(end);
		employee.encodeToXml(eventWriter);
		EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.EMPLOYEE);
		eventWriter.add(eElement);
		eventWriter.add(end);
	}
	eventWriter.add(eventFactory.createEndElement("", "", "content"));
	eventWriter.add(eventFactory.createEndDocument());
	eventWriter.close();
}
 
Example 16
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 17
Source File: EmployeeRepository.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void save(ArrayList<Employee> employees) throws FileNotFoundException, XMLStreamException {
	XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
	// Create XMLEventWriter
	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
	// Create an EventFactory
	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");
	// Create and write Start Tag
	StartDocument startDocument = eventFactory.createStartDocument();
	eventWriter.add(startDocument);
	// Create content open tag
	StartElement configStartElement = eventFactory.createStartElement("", "", "content");
	eventWriter.add(configStartElement);
	eventWriter.add(end);

	for (XML_Parsable employee : employees) {
		StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.EMPLOYEE);
		eventWriter.add(sElement);
		eventWriter.add(end);

		employee.encodeToXml(eventWriter);

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

	}
	eventWriter.add(eventFactory.createEndElement("", "", "content"));
	eventWriter.add(eventFactory.createEndDocument());
	eventWriter.close();
}
 
Example 18
Source File: EmployeeRepository.java    From JavaMainRepo with Apache License 2.0 5 votes vote down vote up
public void save(ArrayList<Employee> employees) throws FileNotFoundException, XMLStreamException {
	XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
	// Create XMLEventWriter
	XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(XML_FILENAME));
	// Create a EventFactory
	XMLEventFactory eventFactory = XMLEventFactory.newInstance();
	XMLEvent end = eventFactory.createDTD("\n");
	// Create and write Start Tag
	StartDocument startDocument = eventFactory.createStartDocument();
	eventWriter.add(startDocument);
	// Create content open tag
	StartElement configStartElement = eventFactory.createStartElement("", "", "content");
	eventWriter.add(configStartElement);
	eventWriter.add(end);
	for (XML_Parsable employee : employees) {
		StartElement sElement = eventFactory.createStartElement("", "", Constants.XML_TAGS.EMPLOYEE);
		eventWriter.add(sElement);
		eventWriter.add(end);
		employee.encodeToXml(eventWriter);
		EndElement eElement = eventFactory.createEndElement("", "", Constants.XML_TAGS.EMPLOYEE);
		eventWriter.add(eElement);
		eventWriter.add(end);
	}
	eventWriter.add(eventFactory.createEndElement("", "", "content"));
	eventWriter.add(eventFactory.createEndDocument());
	eventWriter.close();
}
 
Example 19
Source File: EppXmlSanitizer.java    From nomulus with Apache License 2.0 4 votes vote down vote up
private static String sanitizeAndEncode(byte[] inputXmlBytes)
    throws XMLStreamException, UnsupportedEncodingException {
  XMLEventReader xmlEventReader =
      XML_INPUT_FACTORY.createXMLEventReader(new ByteArrayInputStream(inputXmlBytes));

  if (!xmlEventReader.hasNext()) {
    return "";
  }

  XMLEvent firstEvent = xmlEventReader.nextEvent();
  checkState(firstEvent.isStartDocument(), "Missing StartDocument");
  // Get input encoding for use in XMLEventWriter creation, so that sanitized XML preserves the
  // encoding declaration. According to XML spec, UTF-8 is to be used unless input declares
  // otherwise. Epp officially allows UTF-8 and UTF-16.
  String inputEncoding =
      Optional.ofNullable(((StartDocument) firstEvent).getCharacterEncodingScheme())
          .orElse(StandardCharsets.UTF_8.name());

  ByteArrayOutputStream outputXmlBytes = new ByteArrayOutputStream();
  XMLEventWriter xmlEventWriter =
      XML_OUTPUT_FACTORY.createXMLEventWriter(outputXmlBytes, inputEncoding);
  xmlEventWriter.add(firstEvent);

  while (xmlEventReader.hasNext()) {
    XMLEvent xmlEvent = xmlEventReader.nextEvent();
    xmlEventWriter.add(xmlEvent);
    if (isStartEventForSensitiveData(xmlEvent)) {
      QName startEventName = xmlEvent.asStartElement().getName();
      while (xmlEventReader.hasNext()) {
        XMLEvent event = xmlEventReader.nextEvent();
        if (event.isCharacters()) {
          Characters characters = event.asCharacters();
          event = XML_EVENT_FACTORY.createCharacters(maskSensitiveData(characters.getData()));
        }
        xmlEventWriter.add(event);
        if (isMatchingEndEvent(event, startEventName)) {
          // The inner while-loop is guaranteed to break here for any valid XML.
          // If matching event is missing, xmlEventReader will throw XMLStreamException.
          break;
        }
      }
    }
  }
  xmlEventWriter.flush();
  return outputXmlBytes.toString(inputEncoding);
}
 
Example 20
Source File: XMLUtilities.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Override
protected InputStream replaceLink(
    final InputStream toBeChanged, final String linkName, final InputStream replacement)
    throws Exception {
  final XMLEventReader reader = getEventReader(toBeChanged);

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

  final XMLEventFactory eventFactory = XMLEventFactory.newInstance();
  XMLEvent newLine = eventFactory.createSpace("\n");

  try {
    final XMLElement linkElement =
        extractElement(reader, writer,
            Collections.<String> singletonList(Constants.get(ConstantKey.LINK)),
            Collections.<Map.Entry<String, String>> singletonList(
                new SimpleEntry<String, String>("title", linkName)), false, 0, -1, -1).getValue();
    writer.add(linkElement.getStart());

    // ------------------------------------------
    // write inline ...
    // ------------------------------------------
    writer.add(newLine);
    writer.add(eventFactory.createStartElement("m", null, "inline"));

    addAtomElement(replacement, writer);

    writer.add(eventFactory.createEndElement("m", null, "inline"));
    writer.add(newLine);
    // ------------------------------------------

    writer.add(linkElement.getEnd());

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

  return new ByteArrayInputStream(bos.toByteArray());
}