Improved demo. Fixed some string building.
This commit is contained in:
parent
8cb6250cde
commit
a65197d100
3 changed files with 121 additions and 61 deletions
|
@ -327,7 +327,7 @@ public class XMLTools
|
|||
result = "CDATA";
|
||||
break;
|
||||
case XMLEvent.CHARACTERS:
|
||||
result = "CHARACTERS [" + source.asCharacters().getData() + "]";
|
||||
result = String.format("CHARACTERS [%s]", source.asCharacters().getData());
|
||||
break;
|
||||
case XMLEvent.COMMENT:
|
||||
result = "COMMENT";
|
||||
|
@ -339,7 +339,7 @@ public class XMLTools
|
|||
result = "END_DOCUMENT";
|
||||
break;
|
||||
case XMLEvent.END_ELEMENT:
|
||||
result = "END_ELEMENT " + source.asEndElement().getName();
|
||||
result = String.format("END_ELEMENT [%s]", source.asEndElement().getName());
|
||||
break;
|
||||
case XMLEvent.ENTITY_DECLARATION:
|
||||
result = "ENTITY_DECLARATION";
|
||||
|
@ -363,8 +363,8 @@ public class XMLTools
|
|||
result = "START_DOCUMENT";
|
||||
break;
|
||||
case XMLEvent.START_ELEMENT:
|
||||
result = "START_ELEMENT [name=" + source.asStartElement().getName() + "][namespaceURI=" + source.asStartElement().getName().getNamespaceURI() + "][prefix="
|
||||
+ source.asStartElement().getName().getPrefix() + "][localPart=" + source.asStartElement().getName().getLocalPart() + "]";
|
||||
result = String.format("START_ELEMENT [name=%s][namespaceURI=%s][prefix=%s][localPart=%s]", source.asStartElement().getName(), source.asStartElement().getName().getNamespaceURI(),
|
||||
source.asStartElement().getName().getPrefix(), source.asStartElement().getName().getLocalPart());
|
||||
break;
|
||||
default:
|
||||
result = null;
|
||||
|
@ -391,7 +391,7 @@ public class XMLTools
|
|||
}
|
||||
else
|
||||
{
|
||||
result = "[label=" + source.getLabel() + "][type=" + source.getType().toString() + "][content=" + source.getContent() + "]";
|
||||
result = String.format("[label=%s][type=%s][content=%s]", source.getLabel(), source.getType().toString(), source.getContent());
|
||||
}
|
||||
|
||||
//
|
||||
|
|
116
src/fr/devinsy/util/xml/demo/XMLDemo.java
Normal file
116
src/fr/devinsy/util/xml/demo/XMLDemo.java
Normal file
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Copyright (C) 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/>
|
||||
*/
|
||||
package fr.devinsy.util.xml.demo;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
|
||||
import org.apache.log4j.BasicConfigurator;
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import fr.devinsy.util.xml.XMLBadFormatException;
|
||||
import fr.devinsy.util.xml.XMLReader;
|
||||
import fr.devinsy.util.xml.XMLTag;
|
||||
import fr.devinsy.util.xml.XMLWriter;
|
||||
|
||||
/**
|
||||
* The Class XmlDemo.
|
||||
*/
|
||||
public class XMLDemo
|
||||
{
|
||||
|
||||
/**
|
||||
* The main method of XMLDemo.
|
||||
*
|
||||
* @param args
|
||||
* the arguments
|
||||
* @throws IOException
|
||||
* Signals that an I/O exception has occurred.
|
||||
* @throws XMLStreamException
|
||||
* the XML stream exception
|
||||
* @throws XMLBadFormatException
|
||||
* the XML bad format exception
|
||||
*/
|
||||
public static void main(final String[] args) throws IOException, XMLStreamException, XMLBadFormatException
|
||||
{
|
||||
//
|
||||
BasicConfigurator.configure();
|
||||
Logger.getRootLogger().setLevel(Level.ERROR);
|
||||
|
||||
//
|
||||
String xml;
|
||||
{
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
|
||||
XMLWriter out = new XMLWriter(stream);
|
||||
|
||||
out.writeXMLHeader();
|
||||
|
||||
//
|
||||
out.writeStartTag("account", "id", String.valueOf(77));
|
||||
{
|
||||
out.writeTag("email", "christian.momon@devinsy.fr");
|
||||
out.writeTag("first_names", "Christian Pierre");
|
||||
out.writeTag("last_name", "MOMON");
|
||||
out.writeTag("comment", "Foo character <moo> \" \'");
|
||||
}
|
||||
out.writeEndTag("account");
|
||||
|
||||
//
|
||||
out.close();
|
||||
xml = stream.toString();
|
||||
}
|
||||
|
||||
System.out.println("XML generated:");
|
||||
System.out.println(xml);
|
||||
System.out.println();
|
||||
|
||||
{
|
||||
ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes());
|
||||
XMLReader in = new XMLReader(stream);
|
||||
|
||||
in.readXMLHeader();
|
||||
|
||||
//
|
||||
XMLTag tag = in.readStartTag("account");
|
||||
long id = Long.parseLong(tag.attributes().getByLabel("id").getValue());
|
||||
{
|
||||
String email = in.readContentTag("email").getContent();
|
||||
String firstName = in.readNullableContentTag("first_names").getContent();
|
||||
String lastName = in.readNullableContentTag("last_name").getContent();
|
||||
String comment = in.readNullableContentTag("comment").getContent();
|
||||
|
||||
System.out.println("Data read:");
|
||||
System.out.println("Id=" + id);
|
||||
System.out.println("email=" + email);
|
||||
System.out.println("firstName=" + firstName);
|
||||
System.out.println("lastName=" + lastName);
|
||||
System.out.println("comment=" + comment);
|
||||
}
|
||||
in.readEndTag("account");
|
||||
|
||||
//
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,56 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 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/>
|
||||
*/
|
||||
package fr.devinsy.util.xml.demo;
|
||||
|
||||
import fr.devinsy.util.strings.StringList;
|
||||
|
||||
/**
|
||||
* The Class XmlDemo.
|
||||
*/
|
||||
public class XmlDemo
|
||||
{
|
||||
/**
|
||||
* The main method.
|
||||
*
|
||||
* @param args
|
||||
* the arguments
|
||||
*/
|
||||
public static void main(final String[] args)
|
||||
{
|
||||
// #1
|
||||
{
|
||||
StringList strings = new StringList();
|
||||
|
||||
strings.appendln("========== DEMO #1;");
|
||||
strings.appendln();
|
||||
|
||||
System.out.println(strings.toString());
|
||||
}
|
||||
|
||||
// #2
|
||||
{
|
||||
StringList strings = new StringList();
|
||||
|
||||
strings.appendln("========== DEMO #2;");
|
||||
strings.appendln();
|
||||
|
||||
System.out.println(strings.toString());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue