devinsy-xml/test/foo/Foo.java

103 lines
3.1 KiB
Java

package foo;
/*
* Copyright (C) 2013-2014,2017 Christian Pierre MOMON
*
* This file is part of Devinsy-xml.
*
* Devinsy-xml is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Devinsy-xml is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Devinsy-xml. If not, see <http://www.gnu.org/licenses/>
*/
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;
/**
* The Class Foo contains study code.
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class Foo
{
/**
* The main method displays the XML event type reading a XML file
*
* @param args
* the arguments
* @throws Exception
* the exception
*/
public static void main(final String args[]) throws Exception
{
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader in = factory.createXMLEventReader(Foo.class.getResourceAsStream("/foo/foo.xml"));
XMLEvent event;
while (in.hasNext())
{
event = in.nextEvent();
switch (event.getEventType())
{
case XMLEvent.ATTRIBUTE:
System.out.println("ATTRIBUTE ");
break;
case XMLEvent.CDATA:
System.out.println("CDATA");
break;
case XMLEvent.CHARACTERS:
System.out.println("CHARACTERS [" + event.asCharacters().getData() + "]");
break;
case XMLEvent.COMMENT:
System.out.println("COMMENT");
break;
case XMLEvent.DTD:
System.out.println("DTD");
break;
case XMLEvent.END_DOCUMENT:
System.out.println("END_DOCUMENT");
break;
case XMLEvent.END_ELEMENT:
System.out.println("END_ELEMENT " + event.asEndElement().getName());
break;
case XMLEvent.ENTITY_DECLARATION:
System.out.println("ENTITY_DECLARATION");
break;
case XMLEvent.ENTITY_REFERENCE:
System.out.println("ENTITY_REFERENCE");
break;
case XMLEvent.NAMESPACE:
System.out.println("NAMESPACE");
break;
case XMLEvent.NOTATION_DECLARATION:
System.out.println("NOTATION_DECLARATION");
break;
case XMLEvent.PROCESSING_INSTRUCTION:
System.out.println("PROCESSING_INSTRUCTION");
break;
case XMLEvent.SPACE:
System.out.println("SPACE");
break;
case XMLEvent.START_DOCUMENT:
System.out.println("START_DOCUMENT");
break;
case XMLEvent.START_ELEMENT:
System.out.println("START_ELEMENT [name=" + event.asStartElement().getName() + "][namespaceURI=" + event.asStartElement().getName().getNamespaceURI() + "][prefix="
+ event.asStartElement().getName().getPrefix() + "][localPart=" + event.asStartElement().getName().getLocalPart() + "]");
break;
default:
System.out.println("DEFAULT");
}
}
}
}