add items from XML to arraylist - java

I have written the following code. However I would like to add the items from characters.getData() into a custom array as below so I can then use it to carry out mathematical graphing. How can I create my own custom array list containing settDate, publishingPeriodCommencingTime and publishingPeriodCommencingTime?
while(parser.hasNext()) {
XMLEvent event = parser.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("settDate")) {
bMarks = true;
} else if (qName.equalsIgnoreCase("publishingPeriodCommencingTime")) {
bLastName = true;
} else if (qName.equalsIgnoreCase("fuelTypeGeneration")) {
bNickName = true;
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if(bMarks) {
System.out.println("settDate: " + characters.getData());
bMarks = false;
}
if(bLastName) {
System.out.println("publishingPeriodCommencingTime: " + characters.getData());
bLastName = false;
}
if(bNickName) {
System.out.println("fuelTypeGeneration: " + characters.getData());
bNickName = false;
}
rollingD subAction= new RollingD(characters.getData(), characters.getData(), characters.getData());
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if(endElement.getName().getLocalPart().equalsIgnoreCase("item")) {
System.out.println();
}
break;
}
}
This is my custom class
public class RollingD {
private String settDate;
private String publishingPeriodCommencingTime;
private String fuelTypeGeneration;
RollingD(String bMarks, String bLastName, String bNickName) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public String getSettDate() {
return settDate;
}
public void setSettDate(String settDate) {
this.settDate = settDate;
}
public String getPublishingPeriodCommencingTime() {
return publishingPeriodCommencingTime;
}
public void setPublishingPeriodCommencingTime(String publishingPeriodCommencingTime) {
this.publishingPeriodCommencingTime = publishingPeriodCommencingTime;
}
public String getFuelTypeGeneration() {
return fuelTypeGeneration;
}
public void setFuelTypeGeneration(String fuelTypeGeneration) {
this.fuelTypeGeneration = fuelTypeGeneration;
}
}
This is a sample of the XML
<settDate>2020-02-29</settDate>
<publishingPeriodCommencingTime>09:55:00</publishingPeriodCommencingTime>
<fuelTypeGeneration>31891</fuelTypeGeneration>
<settDate>2020-02-29</settDate>
<publishingPeriodCommencingTime>10:00:00</publishingPeriodCommencingTime>
<fuelTypeGeneration>31743</fuelTypeGeneration>
..

Below is my implementation according to how I understood your question.
I created the following XML file that contains only the sample data from your question.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<settDate>2020-02-29</settDate>
<publishingPeriodCommencingTime>09:55:00</publishingPeriodCommencingTime>
<fuelTypeGeneration>31891</fuelTypeGeneration>
<settDate>2020-02-29</settDate>
<publishingPeriodCommencingTime>10:00:00</publishingPeriodCommencingTime>
<fuelTypeGeneration>31743</fuelTypeGeneration>
</root>
Through experimentation, I saw that the effective order of the XML events is:
START_ELEMENT
CHARACTERS
END_ELEMENT
The elements of interest are:
settDate
publishingPeriodCommencingTime
fuelTypeGeneration
The basic algorithm is therefore:
If event type is START_ELEMENT and event name is settDate, create a new instance of class RollingD.
If event type is CHARACTERS, then set the value of the member in RollingD according to the event name.
If event type is END_ELEMENT and event name is fuelTypeGeneration, add the RollingD object to the list.
Note that you need to replace path-to-XML-file in the below code with the actual path to your XML file.
Also, method toString() in class RollingD is purely for debugging purposes and therefore can safely be removed from the below code if you think you don't need it.
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
public class StaxTest {
private static final String SETT_DATE = "settDate";
private static final String PUBLISHING_PERIOD_COMMENCING_TIME = "publishingPeriodCommencingTime";
private static final String FUEL_TYPE_GENERATION = "fuelTypeGeneration";
public static void main(String[] args) {
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader reader = null;
Path path = Paths.get("path-to-XML-file");
try (FileInputStream fis = new FileInputStream(path.toFile())) {
reader = xmlInputFactory.createXMLEventReader(fis);
List<RollingD> list = new ArrayList<>();
RollingD rD = null;
String localName = null;
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
switch (event.getEventType()) {
case XMLEvent.START_ELEMENT:
StartElement elem = event.asStartElement();
QName qName = elem.getName();
localName = qName.getLocalPart();
switch (localName) {
case SETT_DATE:
rD = new RollingD();
break;
case PUBLISHING_PERIOD_COMMENCING_TIME:
break;
case FUEL_TYPE_GENERATION:
break;
default:
}
break;
case XMLEvent.END_ELEMENT:
EndElement endElem = event.asEndElement();
QName qNamEnd = endElem.getName();
String endName = qNamEnd.getLocalPart();
switch (endName) {
case SETT_DATE:
break;
case PUBLISHING_PERIOD_COMMENCING_TIME:
break;
case FUEL_TYPE_GENERATION:
list.add(rD);
break;
default:
}
break;
case XMLEvent.CHARACTERS:
Characters chars = event.asCharacters();
if (!chars.isWhiteSpace()) {
String data = chars.getData();
switch (localName) {
case SETT_DATE:
rD.setSettDate(data);
break;
case PUBLISHING_PERIOD_COMMENCING_TIME:
rD.setPublishingPeriodCommencingTime(data);
break;
case FUEL_TYPE_GENERATION:
rD.setFuelTypeGeneration(data);
break;
default:
}
}
break;
default:
System.out.println("Unhandled XML event: " + event.getEventType());
}
}
list.stream().forEach(System.out::println); // Prints the list.
}
catch (IOException | XMLStreamException x) {
x.printStackTrace();
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (XMLStreamException xXmlStream) {
System.out.println("WARNING (ignored): Failed to close XML event reader");
xXmlStream.printStackTrace();
}
}
}
}
}
class RollingD {
private String settDate;
private String publishingPeriodCommencingTime;
private String fuelTypeGeneration;
public RollingD() {
this(null, null, null);
}
public RollingD(String settDate,
String publishingPeriodCommencingTime,
String fuelTypeGeneration) {
this.settDate = settDate;
this.publishingPeriodCommencingTime = publishingPeriodCommencingTime;
this.fuelTypeGeneration = fuelTypeGeneration;
}
public void setSettDate(String settDate) {
this.settDate = settDate;
}
public void setPublishingPeriodCommencingTime(String publishingPeriodCommencingTime) {
this.publishingPeriodCommencingTime = publishingPeriodCommencingTime;
}
public void setFuelTypeGeneration(String fuelTypeGeneration) {
this.fuelTypeGeneration = fuelTypeGeneration;
}
public String toString() {
return String.format("%s,%s,%s",
settDate,
publishingPeriodCommencingTime,
fuelTypeGeneration);
}
}
Here is the output I get after running the above code.
2020-02-29,09:55:00,31891
2020-02-29,10:00:00,31743

Related

Combining attributes in a class

I have the following custom class
class rollingD {
private String settDate;
private String publishingPeriodCommencingTime;
private String fuelTypeGeneration;
public String getSettDate() {
return settDate;
}
public void setSettDate(String settDate) {
this.settDate = settDate;
}
public String getPublishingPeriodCommencingTime() {
return publishingPeriodCommencingTime;
}
public void setPublishingPeriodCommencingTime(String publishingPeriodCommencingTime) {
this.publishingPeriodCommencingTime = publishingPeriodCommencingTime ;
}
public String getFuelTypeGeneration() {
return fuelTypeGeneration;
}
public void setFuelTypeGeneration(String fuelTypeGeneration) {
this.fuelTypeGeneration = fuelTypeGeneration;
}
#Override
public String toString() {
return String.format("%s,%s,%s",
settDate,
publishingPeriodCommencingTime,
fuelTypeGeneration);
}}
The items add to my array list as below
ArrayList<rollingD> aList = new ArrayList<>();
The attributes
settDate
and
publishingPeriodCommencingTime
are separate entities
01/04/2020
and
21:34:26
What i would like to do is within my class i want to combine my attributes before adding them to the array list
so i would end up with
01/04/2020 21:34:26
as a single attribute
I would therefore only have 2 attributes in my arraylist which i can then sort etc
Can someone provide assistance please.
I have tried streaming a map afterwards and combining the streams after but i realised that is not an ideal solution as i need to then work with the arraylist further down the line and ideally with datetimestamp and the value as my only two items
This is the code for parsing the API
while(parser.hasNext()) {
XMLEvent event = parser.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("settDate")) {
rD = new rollingD(null,null,null);
bMarks = true;
} else if (qName.equalsIgnoreCase("publishingPeriodCommencingTime")) {
bLastName = true;
} else if (qName.equalsIgnoreCase("fuelTypeGeneration")) {
bNickName = true;
aList.add(rD);
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
if(bMarks) {
// System.out.println("settDate: " + characters.getData());
rD.getTimeStamp(characters.getData(),null,null);
bMarks = false;
}
if(bLastName) {
// System.out.println("publishingPeriodCommencingTime: " + characters.getData());
rD.getTimeStamp(null,characters.getData(),null);
bLastName = false;
}
if(bNickName) {
// System.out.println("fuelTypeGeneration: " + characters.getData());
rD.getTimeStamp(null,null,characters.getData());
bNickName = false;
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
//List<String> namesList = aList.stream()
//.map(x->x.getSettDate()+" "+ x.getPublishingPeriodCommencingTime()+","+ x.getFuelTypeGeneration())
//.collect(Collectors.toList());
//Collections.sort(namesList);
//
//for (String name : namesList) {
//System.out.println(name);
//}
if(endElement.getName().getLocalPart().equalsIgnoreCase("item")) {
System.out.println();
}
break;
}
}
Do it as follows:
class RollingD {
private String settDate;
private String publishingPeriodCommencingTime;
private String fuelTypeGeneration;
private String timeStamp;
public RollingD(String settDate, String publishingPeriodCommencingTime, String fuelTypeGeneration) {
this.settDate = settDate;
this.publishingPeriodCommencingTime = publishingPeriodCommencingTime;
this.fuelTypeGeneration = fuelTypeGeneration;
timeStamp = settDate != null && publishingPeriodCommencingTime != null && !settDate.isEmpty()
&& !publishingPeriodCommencingTime.isEmpty() ? settDate + " " + publishingPeriodCommencingTime : "";
}
public String getTimeStamp() {
return settDate != null && publishingPeriodCommencingTime != null && !settDate.isEmpty()
&& !publishingPeriodCommencingTime.isEmpty() ? settDate + " " + publishingPeriodCommencingTime : "";
}
// ..Do not create any setter for timeStamp
// ..Other getters and setters
#Override
public String toString() {
return "RollingD [settDate=" + settDate + ", publishingPeriodCommencingTime=" + publishingPeriodCommencingTime
+ ", fuelTypeGeneration=" + fuelTypeGeneration + ", timeStamp=" + timeStamp + "]";
}
}
Demo:
public class Main {
public static void main(String[] args) {
RollingD rd1 = new RollingD("", "", "G");
System.out.println(rd1);
RollingD rd2 = new RollingD("01/04/2020", "", "G");
System.out.println(rd2);
RollingD rd3 = new RollingD("", "21:34:26", "G");
System.out.println(rd3);
RollingD rd4 = new RollingD("01/04/2020", "21:34:26", "G");
System.out.println(rd4);
}
}
Output:
RollingD [settDate=, publishingPeriodCommencingTime=, fuelTypeGeneration=G, timeStamp=]
RollingD [settDate=01/04/2020, publishingPeriodCommencingTime=, fuelTypeGeneration=G, timeStamp=]
RollingD [settDate=, publishingPeriodCommencingTime=21:34:26, fuelTypeGeneration=G, timeStamp=]
RollingD [settDate=01/04/2020, publishingPeriodCommencingTime=21:34:26, fuelTypeGeneration=G, timeStamp=01/04/2020 21:34:26]
The are some miss-concepts on your approach.
Do not need to do any combinations of parameters inside base object(or before adding to).
Just added as you did the objects on List Data Structure
Your issues is maybe: How to process further the list, based on stored-objects
properties.(eg: sorting based on names, date_hours,etc)
Here I strongly advise to search on List methods capabilities.(using a comparator, etc)

Java SAX parser : read element of Parent Element when Child element also contains same element

<MyWorld>
<MyWorldId>SGSNPackage2</MyWorldId>
<MyNation>
<state>GROWING</state>
</MyNation>
<state>HAPPY</state>
</MyWorld>
My Parser class :
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
public class MyParser extends DefaultHandler {
private static final String EMPTY_VALUE = "";
private final StringBuilder characters = new StringBuilder();
private boolean read;
private boolean elementFound;
private boolean elementPropertyFound;
private boolean elementIdFound;
private boolean elementWithIdFound;
private boolean elementProperty;
private boolean elementPropertyIsList;
private List<String> properties;
private final Set<String> propertyAlreadyExist = new HashSet<String>();
private String elementType;
private List<String> primaryKeyAttributes;
private String elementIdValue;
private String elementIdName;
private Map<String, Object> elementPropertiesMap;
Map<String, Object> complexMapAttribute = new HashMap<>();
public void setAttributes(final List<String> attributes) {
this.properties = attributes;
}
public void setFdnType(final String fdnType) {
this.elementType = fdnType;
}
public void setPrimaryKeyAttributes(final List<String> primaryKeyAttributes) {
this.primaryKeyAttributes = primaryKeyAttributes;
}
public void setKeyValue(final String keyValue) {
this.elementIdValue = keyValue;
}
#Override
public void startElement(final String uri, final String localName, final String qName, final Attributes attribute) throws SAXException {
if (qName.equalsIgnoreCase(elementType)) {
if (!elementWithIdFound) {
elementFound = true;
} else {
elementWithIdFound = false;
}
} else if (primaryKeyAttributes.contains(qName) && elementFound) {
elementIdFound = true;
read = true;
} else if (properties.contains(qName) && elementWithIdFound) {
if (propertyAlreadyExist.contains(qName)) {
elementPropertyIsList = true;
}
propertyAlreadyExist.add(qName);
elementIdName = qName;
elementPropertyFound = true;
read();
read = true;
elementProperty = false;
} else if(elementFound && elementWithIdFound && elementPropertyFound){
elementProperty = true;
read();
read = true;
}
}
#SuppressWarnings("unchecked")
#Override
public void endElement(final String uri, final String localName, final String qName) throws SAXException {
final String text;
if (primaryKeyAttributes.contains(qName) || properties.contains(qName)) {
if (elementFound && elementWithIdFound && elementPropertyFound) {
text = readBuffer();
if (elementIdName != null) {
if(elementPropertyIsList) {
if(elementProperty){
if(null != elementPropertiesMap.get(elementIdName) && elementPropertiesMap.get(elementIdName)!= EMPTY_VALUE){
final List<Map<String,Object>> listOfComplexAttribute = new ArrayList<>();
if(elementPropertiesMap.get(elementIdName) instanceof List){
listOfComplexAttribute.addAll((List<Map<String, Object>>) elementPropertiesMap.get(elementIdName));
}else{
listOfComplexAttribute.add((Map<String, Object>) elementPropertiesMap.get(elementIdName));
}
listOfComplexAttribute.add(new HashMap<String, Object>(complexMapAttribute));
elementPropertiesMap.put(elementIdName, new ArrayList<Map<String, Object>>(listOfComplexAttribute));
}else{
elementPropertiesMap.put(elementIdName, new HashMap<String, Object>(complexMapAttribute));
}
complexMapAttribute.clear();
}else{
final List<Object> attributeValueList = new ArrayList<>();
if(null != elementPropertiesMap.get(elementIdName) && elementPropertiesMap.get(elementIdName)!= EMPTY_VALUE){
if(elementPropertiesMap.get(elementIdName) instanceof List){
attributeValueList.addAll((List<Object>) elementPropertiesMap.get(elementIdName));
}else{
attributeValueList.add(elementPropertiesMap.get(elementIdName));
}
attributeValueList.add(text);
elementPropertiesMap.put(elementIdName, new ArrayList<Object>(attributeValueList));
}else{
elementPropertiesMap.put(elementIdName,text);
}
}
} else {
if (elementProperty) {
elementPropertiesMap.put(elementIdName,new HashMap<String, Object>(complexMapAttribute));
complexMapAttribute.clear();
} else {
elementPropertiesMap.put(elementIdName,text);
}
}
elementIdName = null;
elementPropertyFound =false;
}
} else {
if (elementIdFound) {
if (readBuffer().equalsIgnoreCase(elementIdValue)) {
elementWithIdFound = true;
} else {
elementIdFound = false;
elementWithIdFound = false;
elementFound = false;
elementIdName = null;
}
}
}
}else if (elementFound && elementWithIdFound && elementPropertyFound && elementProperty){
text = readBuffer();
complexMapAttribute.put(qName, text);
}
}
#Override
public void characters(final char[] ch, final int start, final int length) throws SAXException {
if (read) {
characters.append(new String(ch, start, length));
}
}
public void parseData(final String data) {
elementPropertiesMap = new HashMap<>();
for (String attributeName : properties) {
elementPropertiesMap.put(attributeName, EMPTY_VALUE);
}
final SAXParserFactory parserFactor = SAXParserFactory.newInstance();
try {
final SAXParser parser = parserFactor.newSAXParser();
parser.parse(new InputSource(new StringReader(data)), this);
} catch (ParserConfigurationException | SAXException | IOException e) {
throw new RuntimeException((String) e.getMessage());
}
}
private void read() {
read = true;
characters.setLength(0);
}
private String readBuffer() {
final String chars = characters.toString();
characters.setLength(0);
read = false;
return chars;
}
}
I have above XML and I want to read the state of MyWorld. My SAX parser returns me the value of state as {GROWING. HAPPY} as attribures/element can occurs more than one so I handled accordingly.
What I want, I should get only "HAPPY" when I try to find state while parsing it ?
How I can achieve it ?
Thnaks a million for yur help!!

How do I deserialize Json with modifying the structure?

I have this Json content:
{
"people":[
{
"name":"test1",
"sirname":"test2",
"details":{
"social_no":1234567,
"creadit_card_no":34582342309
}
},
{
"name":"test3",
"sirname":"test4",
"details":{
"social_no":12345679,
"creadit_card_no":345823423090
}
}
]
}
and according to logic this Json should have 3 POJO classes: A class that will hold the list of People, People object and a Details object.
Now my question is, is it possible to deserialize this Json using Jackson or if not possible with Jackson, with GSON library? One that will contain the list of People, and another one, for example Human class, that will have the following structure:
public class Human{
String name;
String sirname;
String social_no;
String creadit_card_no;
//..getters and setters
//should correspond with this json fragment:
// {
// "name":"test1",
// "sirname":"test2",
// "details":{
// "social_no":1234567,
// "creadit_card_no":34582342309
// }
}
}
So if this is possible, how can I do this?
Update
My actuall json structure is different than the example given here, so here is the original json
So I've created a TypeAdapter on my own, here is the code from this class:
public class PlanTypeAdapter extends TypeAdapter<Plan> {
private final String TAG = PlanTypeAdapter.class.getSimpleName();
#Override
public void write(JsonWriter out, Plan value) throws IOException {
Log.d(TAG, "WRITE");
}
#Override
public Plan read(JsonReader reader) throws IOException {
Log.d(TAG, "READ");
Plan plan = new Plan();
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
reader.setLenient(false);
while (reader.hasNext()) {
Log.d(TAG, "PATH: " + reader.getPath());
Log.d(TAG, "PEEK: " + reader.peek());
if (reader.peek() == JsonToken.BEGIN_OBJECT) {
Log.d(TAG, "BEGIN object, path: " + reader.getPath());
reader.beginObject();
} else if (reader.peek() == JsonToken.NULL) {
Log.d(TAG, "NULL");
reader.skipValue();
} else if (reader.peek() == JsonToken.END_ARRAY) {
Log.d(TAG, "END ARRAY");
if (reader.getPath().contains("retailer")) {
reader.endObject();
} else {
reader.endArray();
}
} else if (reader.peek() == JsonToken.END_OBJECT) {
reader.endObject();
Log.d(TAG, "END object, path: " + reader.getPath());
} else if (reader.peek() == JsonToken.NUMBER) {
Log.d(TAG, "NUMBER " + reader.getPath());
} else if (reader.peek() == JsonToken.BOOLEAN) {
Log.d(TAG, "BOOLEAN " + reader.getPath());
} else if (reader.peek() == JsonToken.NAME) {
switch (reader.nextName()) {
case "retailer":
reader.beginObject();
Log.d(TAG, "RET");
break;
case "national_plan":
reader.beginObject();
Log.d(TAG, "NPlan");
break;
case "name":
if (reader.getPath().contains("retailer")) {
plan.setRetailer_name(reader.nextString());
reader.skipValue();
reader.skipValue();
reader.endObject();
} else {
reader.skipValue();
}
break;
case "contract_end":
plan.setContract_end(reader.nextString());
break;
case "data_level_gb":
plan.setData_level_gb(reader.nextString());
break;
case "data_level_id":
plan.setData_level_id(reader.nextInt());
break;
case "days_to_end":
plan.setDays_to_switch(reader.nextInt());
break;
case "direct_from_operator":
plan.setDirect_from_operator(reader.nextBoolean());
break;
case "calculation_amount":
plan.setCalculationAmount(reader.nextDouble());
break;
case "network_generation_name":
plan.setNetwork_generation_(reader.nextString());
break;
case "partner_plan_id":
plan.setPartner_plan_id(reader.nextString());
break;
case "payment_level":
plan.setPayment_level(reader.nextString());
break;
case "payment_level_id":
plan.setPayment_level_id(reader.nextInt());
break;
case "roaming_amount":
plan.setRoaming_amount(reader.nextDouble());
break;
case "savings_amount":
plan.setSavings_amount(reader.nextDouble());
break;
case "savings_avg":
plan.setSavings_avg(reader.nextDouble());
break;
case "savings_percents":
plan.setSavings_percents(reader.nextInt());
break;
default:
Log.d(TAG, "DEFAULT " + reader.peek() + "");
reader.skipValue();
break;
}
} else {
reader.skipValue();
}
}
return plan;
}
}
If you have a very, very large file, I recommend doing this with a custom deserializer using Gson, but I would not use the JsonDeserializer interface; use the TypeAdapter interface as it is more performant (source). I think that #codemonkey has a very good answer, but it is overly complicated and it can be done much more simply. Specifically, you should never be builidng these Strings yourself (with sb.append()) and you should stay away from JsonDeserializer.
First, create your custom TypeAdapter
public class PersonTypeAdapter extends TypeAdapter<Person> {
#Override
public void write(JsonWriter out, Person value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
out.beginObject();
out.name("name").value(value.name);
out.name("sirname").value(value.sirname);
out.name("details");
out.beginObject();
out.name("social_no").value(value.social_no);
out.name("creadit_card_no").value(value.creadit_card_no);
out.endObject();
out.endObject();
}
#Override
public Person read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
reader.beginObject();
validateName(reader, "name");
String name = reader.nextString();
validateName(reader, "sirname");
String sirname = reader.nextString();
validateName(reader, "details");
reader.beginObject();
validateName(reader, "social_no");
String social_no = reader.nextString();
validateName(reader, "creadit_card_no");
String creadit_card_no = reader.nextString();
reader.endObject();
reader.endObject();
return new Person(name, sirname, social_no, creadit_card_no);
}
private void validateName(JsonReader reader, String string) throws IOException {
String name = reader.nextName();
if(!string.equals(name)) {
throw new JsonSyntaxException("Expected: \"" + string + "\", got \"" + name + "\"");
}
}
}
And, your POJO, obviously:
public class Person {
public final String name;
public final String sirname;
public final String social_no;
public final String creadit_card_no;
public Person(String name, String sirname, String social_no,
String creadit_card_no) {
this.name = name;
this.sirname = sirname;
this.social_no = social_no;
this.creadit_card_no = creadit_card_no;
}
#Override
public String toString() {
return String.format(
"Person [name=%s, sirname=%s, social_no=%s, creadit_card_no=%s]", name,
sirname, social_no, creadit_card_no);
}
}
Then, you can parse the Json from your file using the method here. /test.json is just the example you gave in your question.
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
public class PersonExample {
public static void main(String... args) {
InputStreamReader streamReader = new InputStreamReader(
PersonExample.class.getResourceAsStream("/test.json"));
PeopleWrapper wrapper = parseJSON(streamReader);
System.out.println(wrapper.people);
}
public static class PeopleWrapper {
#SerializedName("people")
public List<Person> people;
}
public static PeopleWrapper parseJSON(Reader jsonInput) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Person.class, new PersonTypeAdapter());
Gson gson = builder.create();
PeopleWrapper peopleWrapper = gson.fromJson(jsonInput, PeopleWrapper.class);
return peopleWrapper;
}
}
This program outputs:
[Person [name=test1, sirname=test2, social_no=1234567, creadit_card_no=34582342309], Person [name=test3, sirname=test4, social_no=12345679, creadit_card_no=345823423090]]
So your actual problem is much more complicated than the one you originally described. I will show you a skeleton of the TypeAdapter you need, and you can figure out the rest. Basically, create the Plan object as you've done, and then for each of the outer JSON keys, handle the value.
If it's one line, you can just handle it in the switch statement.
If it's an array or an object, create a helper method to parse that section of the JSON.
You should assume the JSON is well formed and, if it's not, let Gson throw an Exception. Just tell it to expect what's going to come next.
Here's some code to show you the idea:
import java.io.IOException;
import com.google.gson.*;
import com.google.gson.stream.*;
public class PlanTypeAdapter extends TypeAdapter<Plan> {
private final String TAG = PlanTypeAdapter.class.getSimpleName();
#Override
public void write(JsonWriter out, Plan value) throws IOException {
Log.d(TAG, "WRITE");
}
#Override
public Plan read(JsonReader reader) throws IOException {
Log.d(TAG, "READ");
Plan plan = new Plan();
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
reader.setLenient(false);
reader.beginObject();
while (!(reader.peek() == JsonToken.END_OBJECT)) {
switch (reader.nextName()) {
case "national_plan":
handleNationalPlan(reader, plan);
break;
case "bill_total":
handleBillTotal(reader, plan);
break;
case "contract_end":
plan.setContract_end(reader.nextString());
break;
case "data_level_gb":
plan.setData_level_gb(reader.nextString());
break;
case "data_level_id":
plan.setData_level_id(reader.nextInt());
break;
case "days_to_end":
plan.setDays_to_switch(reader.nextInt());
break;
case "direct_from_operator":
plan.setDirect_from_operator(reader.nextBoolean());
break;
case "calculation_amount":
plan.setCalculationAmount(reader.nextDouble());
break;
case "network_generation_name":
plan.setNetwork_generation_(reader.nextString());
break;
case "partner_plan_id":
plan.setPartner_plan_id(reader.nextString());
break;
case "payment_level":
plan.setPayment_level(reader.nextString());
break;
case "payment_level_id":
plan.setPayment_level_id(reader.nextInt());
break;
case "roaming_amount":
plan.setRoaming_amount(reader.nextDouble());
break;
case "savings_amount":
plan.setSavings_amount(reader.nextDouble());
break;
case "savings_avg":
plan.setSavings_avg(reader.nextDouble());
break;
case "savings_percents":
plan.setSavings_percents(reader.nextInt());
break;
case "yearly_id":
case "handset":
case "internals":
case "consumer_id":
case "calculation_details":
case "operator":
case "total":
case "international_plan":
case "contract_length":
case "zone":
case "externals":
case "cancel_fee":
case "transformers":
case "one-offs":
case "flow":
case "roaming_plan":
case "_id":
// You can use this to ignore the keys you don't care about
default:
Log.d(TAG, "DEFAULT " + reader.peek() + "");
reader.skipValue();
break;
}
}
reader.endObject();
return plan;
}
private void handleNationalPlan(JsonReader reader, Plan plan) throws IOException {
reader.beginObject();
while (!(reader.peek() == JsonToken.END_OBJECT)) {
switch(reader.nextName()) {
case "contract_length":
break;
case "name":
break;
case "country":
// etc.
}
}
reader.endObject();
}
private void handleBillTotal(JsonReader reader, Plan plan) throws IOException {
}
// etc.
}
It seems that at the moment Jackson does not support out of the box such feature to map a field from a nested path.
There is an open issue asking for such feature, but it's a question when will it be done.
The opposite, serializing a nested object to the first level properties in json, is possible by using a #JsonUnwrapped annotation.
So, in order to overcome the problem, it seems that the only way is to write a custom deserializer, which you could map to your class, and use it to create an instance of the class as you need it.
There are different ways you can parse json using the gson library. I will give you two examples.
Method 1 - Write a custom deserializer. This technique uses a class to deserialize the person object. The custom deserializer allows you to create any object you want with the json data. Here are the classes needed to do this:
Group.java:
public class Group {
#SerializedName("people")
private List<Person> persons;
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
#Override
public String toString() {
String NEW_LINE = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder(this.getClass().getName());
sb.append("{");
sb.append(NEW_LINE);
for(Person p : persons){
sb.append(p.toString());
}
sb.append("}");
return sb.toString();
}
}
GsonTest.java:
public class GsonTest {
public static void main(String[] args) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Person.class, new PersonDeserializer());
Gson gson = gsonBuilder.create();
try {
JsonParser parser = new JsonParser();
Object obj = parser.parse(new FileReader("C://data.json"));
JsonObject jsonObject = (JsonObject) obj;
Group group = gson.fromJson(jsonObject, Group.class);
System.out.println(group.toString());
} catch (JsonIOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonSyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Person.java:
public class Person {
public Person(String name, String sirname, Long social_no, Long creadit_card_no) {
this.name = name;
this.sirname = sirname;
this.social_no = social_no;
this.creadit_card_no = creadit_card_no;
}
private String name;
private String sirname;
private Long social_no;
private Long creadit_card_no;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSirname() {
return sirname;
}
public void setSirname(String sirname) {
this.sirname = sirname;
}
public Long getSocial_no() {
return social_no;
}
public void setSocial_no(Long social_no) {
this.social_no = social_no;
}
public Long getCreadit_card_no() {
return creadit_card_no;
}
public void Long(Long creadit_card_no) {
this.creadit_card_no = creadit_card_no;
}
#Override
public String toString() {
String NEW_LINE = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder(this.getClass().getName());
sb.append("{");
sb.append(NEW_LINE);
sb.append("name: ");
sb.append(name);
sb.append(NEW_LINE);
sb.append("sirname: ");
sb.append(sirname);
sb.append(NEW_LINE);
sb.append("social_no: ");
sb.append(social_no);
sb.append(NEW_LINE);
sb.append("creadit_card_no: ");
sb.append(creadit_card_no);
sb.append(NEW_LINE);
sb.append("}");
sb.append(NEW_LINE);
return sb.toString();
}
}
PersonDeserializer.java
public class PersonDeserializer implements JsonDeserializer<Person> {
public Person deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String name = jsonObject.get("name").getAsString();
String sirname = jsonObject.get("sirname").getAsString();
JsonObject details = jsonObject.get("details").getAsJsonObject();
Long social_no = details.get("social_no").getAsLong();
Long creadit_card_no = details.get("creadit_card_no").getAsLong();
Person person = new Person(name, sirname, social_no, creadit_card_no );
return person;
}
}
Method 2 - Use the JsonReader class to parse the json data. You do not have to load the entire json file at once with this technique. This is a better way to parse a large amount of data on devices that have limited resources. This code will be harder to maintain if the json structure changes though. My example code was inspired by this article http://developer.android.com/reference/android/util/JsonReader.html. Use the Person class above with this new GsonTest class:
public class GsonTest {
List<Person> people = null;
public GsonTest() {
people = new ArrayList<Person>();
}
public static void main(String[] args) {
GsonTest gt = new GsonTest();
gt.doGson();
}
void doGson() {
try {
InputStream is = GsonTest.class.getResourceAsStream("data.json");
JsonReader jsonReader = new JsonReader(new InputStreamReader(is, "UTF-8"));
jsonReader.beginObject();
while (jsonReader.hasNext()) {
String name = jsonReader.nextName();
if (name.equals("people")) {
readPeopleArray(jsonReader);
}
}
jsonReader.endObject();
for(Person p : people){
System.out.println(p.toString());
}
}
catch (NullPointerException e){
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void readPeopleArray(JsonReader jsonReader) throws IOException {
jsonReader.beginArray();
while (jsonReader.hasNext()) {
readPersonObject(jsonReader);
}
jsonReader.endArray();
}
private void readPersonObject(JsonReader jsonReader) throws IOException {
String name = null;
String sirname = null;
Long social_no = null;
Long creadit_card_no = null;
jsonReader.beginObject();
while(jsonReader.hasNext()){
String key = jsonReader.nextName();
if(key.equals("details")){
jsonReader.beginObject();
while(jsonReader.hasNext()){
String detailKey = jsonReader.nextName();
if(detailKey.equals("social_no")){
social_no = jsonReader.nextLong();
}
else if(detailKey.equals("creadit_card_no")){
creadit_card_no = jsonReader.nextLong();
}
else{
jsonReader.skipValue();
}
}
jsonReader.endObject();
}
else if(key.equals("name")){
name = jsonReader.nextString();
}
else if(key.equals("sirname")){
sirname = jsonReader.nextString();
}
}
jsonReader.endObject();
people.add(new Person(name, sirname, social_no, creadit_card_no));
}
}

Implementing Iterator interface with exception handling

I need to create a class library which enables me to read different files (.dat-files with different data representations inside them) and create objects with their content (for every line one object).
I also have to create a unit test which starts the reading of the file, so I dont have to read to whole file first and save the content in an array. I want to use the factory pattern.
Here is my implementation of the class that implements the Iterator-Interface
package klassenbibliothek;
public class MyReader implements Iterator<Object>
{
BufferedReader reader;
MyReader(BufferedReader myReader)
{
reader = myReader;
}
#Override
public boolean hasNext() // aus Stackoverflow, von mir abgeändert
{
try {
return reader.ready();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
throw new NoSuchElementException();
}
}
#Override
public String next()
{
//return SubstancesFileObjectCreator(reader.readLine());
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
// return null;
throw new NoSuchElementException();
}
}
}
My question is: why do I get this error message "finally block does not complete normally"? I am not returning something, I am just throwing an exception.
I want to use the methods hasNext() and next() in my unit test, so that the unit test can controll when it starts to read the file. The unit test is in a different package.
Here are my other classes:
class AbstractFileObjectCreator
package klassenbibliothek;
public abstract class AbstractFileObjectCreator
{
public abstract AbstractFileObject createFileObject(String line);
}
class SubstancesFileObjectCreator
package klassenbibliothek;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SubstancesFileObjectCreator extends AbstractFileObjectCreator
{
MyReader myReader;
public void makeReader() throws IOException
{
String dataFileName = "C:/temp/Substances.dat";
BufferedReader bReader = new BufferedReader(new FileReader(dataFileName));
myReader = new MyReader(bReader);
}
#SuppressWarnings("null")
public AbstractFileObject createFileObject(String line)
{
AbstractFileObject mySubstance = null;
String lineValues[] = myReader.next().split("\t");
if(lineValues[0].equals("R"))
{
boolean dutyToDeclare_local;
boolean isUnwanted_local;
boolean isProhibited_local;
boolean isReach_local;
boolean isDeleted_local;
boolean isHidden_local;
String nodeidRaw = lineValues[1];
float nodeid = Float.parseFloat(nodeidRaw);
String casNrRaw = lineValues[2];
String euIndexCodeRaw = lineValues[3];
String einecsCodeRaw = lineValues[4];
String dutyToDeclareRaw = lineValues[5];
if(dutyToDeclareRaw.equals(1))
{
dutyToDeclare_local = true;
}
else
{
dutyToDeclare_local = false;
}
String isUnwantedRaw = lineValues[6];
if(isUnwantedRaw.equals("1"))
{
isUnwanted_local = true;
}
else
{
isUnwanted_local = false;
}
String isProhibitedRaw = lineValues[7];
if(isProhibitedRaw.equals("1"))
{
isProhibited_local = true;
}
else
{
isProhibited_local = false;
}
String isReachRaw = lineValues[8];
if(isReachRaw.equals("1"))
{
isReach_local = true;
}
else
{
isReach_local = false;
}
String isDeletedRaw = lineValues[9];
if(isDeletedRaw.equals("1"))
{
isDeleted_local = true;
}
else
{
isDeleted_local = false;
}
String isHiddenRaw = lineValues[10];
if(isHiddenRaw.equals("1"))
{
isHidden_local = true;
}
else
{
isHidden_local = false;
}
mySubstance = new Substance(nodeid, casNrRaw, euIndexCodeRaw, einecsCodeRaw, dutyToDeclare_local, isUnwanted_local, isProhibited_local, isReach_local, isDeleted_local, isHidden_local);
// und weiter...
}
else
{
String languageCode = lineValues[1];
String name = lineValues[2];
// Synonym-Objekt erzeugen und zu Substance-Objekt hinzufügen
Synonym newSynonym = new Synonym(languageCode, name);
mySubstance.addAppendix(newSynonym);
while(myReader.hasNext())
{
String lineValues_synonyms[] = myReader.next().split("\t");
String lineValuesZero = lineValues_synonyms[0];
if(lineValuesZero.equals("R"))
{
break; // nicht so gut glaube ich!!!
}
String languageCode_next = lineValues_synonyms[1];
String name_next = lineValues_synonyms[2];
Synonym newSynonym_next = new Synonym(languageCode_next, name_next);
mySubstance.addAppendix(newSynonym_next);
}
}
return mySubstance;
}
}
class AbstractFileObject
package klassenbibliothek;
public abstract class AbstractFileObject
{
boolean isDeleted;
public AbstractFileObject(boolean isDeleted)
{
this.isDeleted = isDeleted;
}
public boolean getIsDeleted()
{
return isDeleted;
}
public abstract void addAppendix(Object newAppendix);
}
class Substance
public class Substance extends AbstractFileObject
{
private float nodeid;
private String casNr;
private String euIndexCode;
private String einecsCode;
private boolean dutyToDeclare;
private boolean isUnwanted;
private boolean isProhibited;
private boolean isReach;
private boolean isDeleted;
private boolean isHidden;
private ArrayList<Synonym> synonymList;
public Substance(float nodeid, String casNr, String euIndexCode, String einecsCode,
boolean dutyToDeclare, boolean isUnwanted, boolean isProhibited, boolean isReach,
boolean isDeleted, boolean isHidden)
{
super(isDeleted);
this.nodeid = nodeid;
this.casNr = casNr;
this.euIndexCode = euIndexCode;
this.einecsCode = einecsCode;
this.dutyToDeclare = dutyToDeclare;
this.isUnwanted = isUnwanted;
this.isProhibited = isProhibited;
this.isReach = isReach;
//this.isDeleted = isDeleted;
this.isHidden = isHidden;
}
// getter and setter
}
class Synonym
package klassenbibliothek;
public class Synonym
{
private String languageCode;
private String name;
public Synonym(String languageCode, String name)
{
this.languageCode = languageCode;
this.name = name;
}
public String getLanguageCode()
{
return languageCode;
}
public String getName()
{
return name;
}
}
unit test
package klassenbibliothek.test;
import static org.junit.Assert.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class SubstancesTest {
#Test
public void test() {
//fail("Not yet implemented");
long startTimeNanos = System.nanoTime();
/*
* While... iterator over data file
*/
}
}
Am I using the factory pattern in the right way? I'm very confused.
A finally block always executes if there is a try-block before it. So yours always throws a NoSuchElementException().
finally
{
// return null;
throw new NoSuchElementException();
}
You should do something in it and not throw an Exception.
Finally blocks are for cleanup. They should not specifically throw exceptions like that. Move the exception throwing out of the finally block.
Remove the throw exception from the finally block and put it in catch block or some other place. Finally block is to release resources that you might be using in your program.

Reading multiple xml documents from a socket in java

I'm writing a client which needs to read multiple consecutive small XML documents over a socket. I can assume that the encoding is always UTF-8 and that there is optionally delimiting whitespace between documents. The documents should ultimately go into DOM objects. What is the best way to accomplish this?
The essense of the problem is that the parsers expect a single document in the stream and consider the rest of the content junk. I thought that I could artificially end the document by tracking the element depth, and creating a new reader using the existing input stream. E.g. something like:
// Broken
public void parseInputStream(InputStream inputStream) throws Exception
{
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = documentBuilder.newDocument();
XMLEventWriter domWriter = xof.createXMLEventWriter(new DOMResult(doc));
XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(inputStream);
XMLEventReader reader = factory.createXMLEventReader(xmlStreamReader);
int depth = 0;
while (reader.hasNext()) {
XMLEvent evt = reader.nextEvent();
domWriter.add(evt);
switch (evt.getEventType()) {
case XMLEvent.START_ELEMENT:
depth++;
break;
case XMLEvent.END_ELEMENT:
depth--;
if (depth == 0)
{
domWriter.add(eventFactory.createEndDocument());
System.out.println(doc);
reader.close();
xmlStreamReader.close();
xmlStreamReader = factory.createXMLStreamReader(inputStream);
reader = factory.createXMLEventReader(xmlStreamReader);
doc = documentBuilder.newDocument();
domWriter = xof.createXMLEventWriter(new DOMResult(doc));
domWriter.add(eventFactory.createStartDocument());
}
break;
}
}
}
However running this on input such as <a></a><b></b><c></c> prints the first document and throws an XMLStreamException. Whats the right way to do this?
Clarification: Unfortunately the protocol is fixed by the server and cannot be changed, so prepending a length or wrapping the contents would not work.
Length-prefix each document (in bytes).
Read the length of the first document from the socket
Read that much data from the socket, dumping it into a ByteArrayOutputStream
Create a ByteArrayInputStream from the results
Parse that ByteArrayInputStream to get the first document
Repeat for the second document etc
IIRC, XML documents can have comments and processing-instructions at the end, so there's no real way of telling exactly when you have come to the end of the file.
A couple of ways of handling the situation have already been mentioned. Another alternative is to put in an illegal character or byte into the stream, such as NUL or zero. This has the advantage that you don't need to alter the documents and you never need to buffer an entire file.
just change to whatever stream
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
public class LogParser {
private XMLInputFactory inputFactory = null;
private XMLStreamReader xmlReader = null;
InputStream is;
private int depth;
private QName rootElement;
private static class XMLStream extends InputStream
{
InputStream delegate;
StringReader startroot = new StringReader("<root>");
StringReader endroot = new StringReader("</root>");
XMLStream(InputStream delegate)
{
this.delegate = delegate;
}
public int read() throws IOException {
int c = startroot.read();
if(c==-1)
{
c = delegate.read();
}
if(c==-1)
{
c = endroot.read();
}
return c;
}
}
public LogParser() {
inputFactory = XMLInputFactory.newInstance();
}
public void read() throws Exception {
is = new XMLStream(new FileInputStream(new File(
"./myfile.log")));
xmlReader = inputFactory.createXMLStreamReader(is);
while (xmlReader.hasNext()) {
printEvent(xmlReader);
xmlReader.next();
}
xmlReader.close();
}
public void printEvent(XMLStreamReader xmlr) throws Exception {
switch (xmlr.getEventType()) {
case XMLStreamConstants.END_DOCUMENT:
System.out.println("finished");
break;
case XMLStreamConstants.START_ELEMENT:
System.out.print("<");
printName(xmlr);
printNamespaces(xmlr);
printAttributes(xmlr);
System.out.print(">");
if(rootElement==null && depth==1)
{
rootElement = xmlr.getName();
}
depth++;
break;
case XMLStreamConstants.END_ELEMENT:
System.out.print("</");
printName(xmlr);
System.out.print(">");
depth--;
if(depth==1 && rootElement.equals(xmlr.getName()))
{
rootElement=null;
System.out.println("finished element");
}
break;
case XMLStreamConstants.SPACE:
case XMLStreamConstants.CHARACTERS:
int start = xmlr.getTextStart();
int length = xmlr.getTextLength();
System.out
.print(new String(xmlr.getTextCharacters(), start, length));
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
System.out.print("<?");
if (xmlr.hasText())
System.out.print(xmlr.getText());
System.out.print("?>");
break;
case XMLStreamConstants.CDATA:
System.out.print("<![CDATA[");
start = xmlr.getTextStart();
length = xmlr.getTextLength();
System.out
.print(new String(xmlr.getTextCharacters(), start, length));
System.out.print("]]>");
break;
case XMLStreamConstants.COMMENT:
System.out.print("<!--");
if (xmlr.hasText())
System.out.print(xmlr.getText());
System.out.print("-->");
break;
case XMLStreamConstants.ENTITY_REFERENCE:
System.out.print(xmlr.getLocalName() + "=");
if (xmlr.hasText())
System.out.print("[" + xmlr.getText() + "]");
break;
case XMLStreamConstants.START_DOCUMENT:
System.out.print("<?xml");
System.out.print(" version='" + xmlr.getVersion() + "'");
System.out.print(" encoding='" + xmlr.getCharacterEncodingScheme()
+ "'");
if (xmlr.isStandalone())
System.out.print(" standalone='yes'");
else
System.out.print(" standalone='no'");
System.out.print("?>");
break;
}
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
new LogParser().read();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void printName(XMLStreamReader xmlr) {
if (xmlr.hasName()) {
System.out.print(getName(xmlr));
}
}
private static String getName(XMLStreamReader xmlr) {
if (xmlr.hasName()) {
String prefix = xmlr.getPrefix();
String uri = xmlr.getNamespaceURI();
String localName = xmlr.getLocalName();
return getName(prefix, uri, localName);
}
return null;
}
private static String getName(String prefix, String uri, String localName) {
String name = "";
if (uri != null && !("".equals(uri)))
name += "['" + uri + "']:";
if (prefix != null)
name += prefix + ":";
if (localName != null)
name += localName;
return name;
}
private static void printAttributes(XMLStreamReader xmlr) {
for (int i = 0; i < xmlr.getAttributeCount(); i++) {
printAttribute(xmlr, i);
}
}
private static void printAttribute(XMLStreamReader xmlr, int index) {
String prefix = xmlr.getAttributePrefix(index);
String namespace = xmlr.getAttributeNamespace(index);
String localName = xmlr.getAttributeLocalName(index);
String value = xmlr.getAttributeValue(index);
System.out.print(" ");
System.out.print(getName(prefix, namespace, localName));
System.out.print("='" + value + "'");
}
private static void printNamespaces(XMLStreamReader xmlr) {
for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
printNamespace(xmlr, i);
}
}
private static void printNamespace(XMLStreamReader xmlr, int index) {
String prefix = xmlr.getNamespacePrefix(index);
String uri = xmlr.getNamespaceURI(index);
System.out.print(" ");
if (prefix == null)
System.out.print("xmlns='" + uri + "'");
else
System.out.print("xmlns:" + prefix + "='" + uri + "'");
}
}
A simple solution is to wrap the documents on the sending side in a new root element:
<?xml version="1.0"?>
<documents>
... document 1 ...
... document 2 ...
</documents>
You must make sure that you don't include the XML header (<?xml ...?>), though. If all documents use the same encoding, this can be accomplished with a simple filter which just ignores the first line of each document if it starts with <?xml
Found this forum message (which you probably already saw), which has a solution by wrapping the input stream and testing for one of two ascii characters (see post).
You could try an adaptation on this by first converting to use a reader (for proper character encoding) and then doing element counting until you reach the closing element, at which point you trigger the EOM.
Hi
I also had this problem at work (so won't post resulting the code). The most elegant solution that I could think of, and which works pretty nicely imo, is as follows
Create a class for example DocumentSplittingInputStream which extends InputStream and takes the underlying inputstream in its constructor (or gets set after construction...).
Add a field with a byte array closeTag containing the bytes of the closing root node you are looking for.
Add a field int called matchCount or something, initialised to zero.
Add a field boolean called underlyingInputStreamNotFinished, initialised to true
On the read() implementation:
Check if matchCount == closeTag.length, if it does, set matchCount to -1, return -1
If matchCount == -1, set matchCount = 0, call read() on the underlying inputstream until you get -1 or '<' (the xml declaration of the next document on the stream) and return it. Note that for all I know the xml spec allows comments after the document element, but I knew I was not going to get that from the source so did not bother handling it - if you can not be sure you'll need to change the "gobble" slightly.
Otherwise read an int from the underlying inputstream (if it equals closeTag[matchCount] then increment matchCount, if it doesn't then reset matchCount to zero) and return the newly read byte
Add a method which returns the boolean on whether the underlying stream has closed.
All reads on the underlying input stream should go through a separate method where it checks if the value read is -1 and if so, sets the field "underlyingInputStreamNotFinished" to false.
I may have missed some minor points but i'm sure you get the picture.
Then in the using code you do something like, if you are using xstream:
DocumentSplittingInputStream dsis = new DocumentSplittingInputStream(underlyingInputStream);
while (dsis.underlyingInputStreamNotFinished()) {
MyObject mo = xstream.fromXML(dsis);
mo.doSomething(); // or something.doSomething(mo);
}
David
I had to do something like this and during my research on how to approach it, I found this thread that even though it is quite old, I just replied (to myself) here wrapping everything in its own Reader for simpler use
I was faced with a similar problem. A web service I'm consuming will (in some cases) return multiple xml documents in response to a single HTTP GET request. I could read the entire response into a String and split it, but instead I implemented a splitting input stream based on user467257's post above. Here is the code:
public class AnotherSplittingInputStream extends InputStream {
private final InputStream realStream;
private final byte[] closeTag;
private int matchCount;
private boolean realStreamFinished;
private boolean reachedCloseTag;
public AnotherSplittingInputStream(InputStream realStream, String closeTag) {
this.realStream = realStream;
this.closeTag = closeTag.getBytes();
}
#Override
public int read() throws IOException {
if (reachedCloseTag) {
return -1;
}
if (matchCount == closeTag.length) {
matchCount = 0;
reachedCloseTag = true;
return -1;
}
int ch = realStream.read();
if (ch == -1) {
realStreamFinished = true;
}
else if (ch == closeTag[matchCount]) {
matchCount++;
} else {
matchCount = 0;
}
return ch;
}
public boolean hasMoreData() {
if (realStreamFinished == true) {
return false;
} else {
reachedCloseTag = false;
return true;
}
}
}
And to use it:
String xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<root>first root</root>" +
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<root>second root</root>";
ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
SplittingInputStream splitter = new SplittingInputStream(is, "</root>");
BufferedReader reader = new BufferedReader(new InputStreamReader(splitter));
while (splitter.hasMoreData()) {
System.out.println("Starting next stream");
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println("line ["+line+"]");
}
}
I use JAXB approach to unmarshall messages from multiply stream:
MultiInputStream.java
public class MultiInputStream extends InputStream {
private final Reader source;
private final StringReader startRoot = new StringReader("<root>");
private final StringReader endRoot = new StringReader("</root>");
public MultiInputStream(Reader source) {
this.source = source;
}
#Override
public int read() throws IOException {
int count = startRoot.read();
if (count == -1) {
count = source.read();
}
if (count == -1) {
count = endRoot.read();
}
return count;
}
}
MultiEventReader.java
public class MultiEventReader implements XMLEventReader {
private final XMLEventReader reader;
private boolean isXMLEvent = false;
private int level = 0;
public MultiEventReader(XMLEventReader reader) throws XMLStreamException {
this.reader = reader;
startXML();
}
private void startXML() throws XMLStreamException {
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
return;
}
}
}
public boolean hasNextXML() {
return reader.hasNext();
}
public void nextXML() throws XMLStreamException {
while (reader.hasNext()) {
XMLEvent event = reader.peek();
if (event.isStartElement()) {
isXMLEvent = true;
return;
}
reader.nextEvent();
}
}
#Override
public XMLEvent nextEvent() throws XMLStreamException {
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
level++;
}
if (event.isEndElement()) {
level--;
if (level == 0) {
isXMLEvent = false;
}
}
return event;
}
#Override
public boolean hasNext() {
return isXMLEvent;
}
#Override
public XMLEvent peek() throws XMLStreamException {
XMLEvent event = reader.peek();
if (level == 0) {
while (event != null && !event.isStartElement() && reader.hasNext()) {
reader.nextEvent();
event = reader.peek();
}
}
return event;
}
#Override
public String getElementText() throws XMLStreamException {
throw new NotImplementedException();
}
#Override
public XMLEvent nextTag() throws XMLStreamException {
throw new NotImplementedException();
}
#Override
public Object getProperty(String name) throws IllegalArgumentException {
throw new NotImplementedException();
}
#Override
public void close() throws XMLStreamException {
throw new NotImplementedException();
}
#Override
public Object next() {
throw new NotImplementedException();
}
#Override
public void remove() {
throw new NotImplementedException();
}
}
Message.java
#XmlAccessorType(XmlAccessType.FIELD)
#XmlRootElement(name = "Message")
public class Message {
public Message() {
}
#XmlAttribute(name = "ID", required = true)
protected long id;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
#Override
public String toString() {
return "Message{id=" + id + '}';
}
}
Read multiply messages:
public static void main(String[] args) throws Exception{
StringReader stringReader = new StringReader(
"<Message ID=\"123\" />\n" +
"<Message ID=\"321\" />"
);
JAXBContext context = JAXBContext.newInstance(Message.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
MultiInputStream multiInputStream = new MultiInputStream(stringReader);
XMLEventReader xmlEventReader = inputFactory.createXMLEventReader(multiInputStream);
MultiEventReader multiEventReader = new MultiEventReader(xmlEventReader);
while (multiEventReader.hasNextXML()) {
Object message = unmarshaller.unmarshal(multiEventReader);
System.out.println(message);
multiEventReader.nextXML();
}
}
results:
Message{id=123}
Message{id=321}

Categories