Java Code Examples for javax.xml.stream.XMLEventReader

Following code examples demonstrate how to use javax.xml.stream.XMLEventReaderfrom java. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use javax.xml.stream.XMLEventReaderand various code implementation of this class.

    @Test
    public default void verifyGenericType()
    throws Exception {
        final Class<SUT> sut = createNewSUT();
        assertTrue("This j8unit test interface is used with a generic type that is illegaly not assignable to XMLEventReader.class!",
                   XMLEventReader.class.isAssignableFrom(sut));
    } 


	private XMLEventReader processComments(XMLEventWriter writer, Comment comment ) throws XMLStreamException, IOException {
		
		
		String text = comment.getText().trim();
		if( ! text.startsWith("<jxon:"))
			return null ;
		
		
		
		String xml = 
			"<xs:annotation xmlns:xs=\"" + kSCHEMA_NS + "\" xmlns:jxon=\""  + kJXON_NS + "\"> " + 
				"<xs:appinfo>" + 
					text + 
				"</xs:appinfo>" +
			"</xs:annotation>" ;
				
		
		
		InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
		XMLEventReader	reader = new NoDocXMLEventReader( mInputFactory.createXMLEventReader(is));
		
		return reader ;
		
		
	} 

	public int run(List<XValue> args) throws Exception {
		
		Options opts = new Options( SerializeOpts.getOptionDefs() );
		opts.parse(args);
		args = opts.getRemainingArgs();
		
		
		InputPort stdin = null;
		if( args.size() > 0 )
			stdin = getInput( args.get(0));
		else
			stdin = getStdin();
		if( stdin == null )
			throw new InvalidArgumentException("Cannot open input");
		
			
		try {
			
			SerializeOpts sopts = getSerializeOpts(opts);
			
			XMLEventReader	reader = stdin.asXMLEventReader(sopts);
			org.xmlsh.core.io.OutputPort stdout = getStdout();
			XMLEventWriter  writer = stdout.asXMLEventWriter(sopts);
			
			stdout.setSystemId(stdin.getSystemId());
			XMLEvent e;
			
			writer.setPrefix("jxon", kJXON_NS );
			
			XMLEventReader 	commentReader = null ;
			while( reader.hasNext() ){
				e = (XMLEvent) reader.next();
				
				
				if( e.getEventType() == XMLStreamConstants.COMMENT){
					commentReader =  processComments( writer , (Comment) e ) ;
					if( commentReader != null )
						continue ;
				}
				
				
				writer.add(e);
				if( commentReader != null && e.isStartElement()){
					writer.add(commentReader);
					commentReader.close();
					commentReader = null ;
					
				}

			
			}
			reader.close();
			writer.close();
		} 
		finally {
			
			stdin.close();
		}
		return 0;
		
		
	} 

Advertisement
Javadoc
This is the top level interface for parsing XML Events. It provides the ability to peek at the next

event and returns configuration information through the property interface. @version 1.0 @author Copyright (c) 2009 by Oracle Corporation. All Rights Reserved. @see XMLInputFactory @see XMLEventWriter @since 1.6

Read More
Advertisement