get value from xml in method with specific parameters JAVA - java

I'm newbie in XML parsing and try to understand JAXB. Have the following task:
Implemented the following method, to got the személy object by id parameter,but it returns null:
public Személy getSzemélyById(String id) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Személy.class);
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
InputSource inputSource;
inputSource = new InputSource(new FileReader("C:\\Users\\zbocskay.TS-EU\\Documents\\NetBeansProjects\\prt2014levzh\\people.xml"));
SAXSource source = new SAXSource(xmlReader, inputSource);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Személy személy = (Személy) unmarshaller.unmarshal(source);
System.out.println(személy.toString());
return new Személy(személy.getId(), személy.getVezetéknév(), személy.getKeresztnév(), személy.getÉletkor(), személy.getCím(), személy.státusz.DIÁK);
} catch (JAXBException e) {
} catch (FileNotFoundException | ParserConfigurationException ex) {
Logger.getLogger(SzemélyDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXNotRecognizedException ex) {
Logger.getLogger(SzemélyDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXNotSupportedException ex) {
Logger.getLogger(SzemélyDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(SzemélyDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
Here is Személy Class:
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Személy {
public enum Státusz {
FELNŐTT, DIÁK, NYUGDÍJAS, GYERMEK
}
#XmlElement
protected String id;
#XmlElement
protected String vezetéknév;
#XmlElement
protected String keresztnév;
protected Integer életkor;
#XmlElement
protected String cím;
protected Státusz státusz;
public Személy(String id) {
super();
this.id = id;
}
public void setÉletkor(Integer életkor) {
this.életkor = életkor;
}
public String getVezetéknév() {
return vezetéknév;
}
public void setVezetéknév(String vezetéknév) {
this.vezetéknév = vezetéknév;
}
public String getKeresztnév() {
return keresztnév;
}
public void setKeresztnév(String keresztnév) {
this.keresztnév = keresztnév;
}
public String getCím() {
return cím;
}
public void setCím(String cím) {
this.cím = cím;
}
public String getId() {
return id;
}
public Személy(String id, String vezetéknév, String keresztnév,
Integer életkor, String cím, Státusz státusz) {
this(id);
this.vezetéknév = vezetéknév;
this.keresztnév = keresztnév;
this.életkor = életkor;
this.cím = cím;
this.státusz = státusz;
}
public Személy(String id, String vezetéknév, String keresztnév,
String születésiDátum, String cím, String diákigazolványszám,
Státusz státusz) throws ParseException {
this(id);
this.vezetéknév = vezetéknév;
this.keresztnév = keresztnév;
this.életkor = meghatározÉletkort(születésiDátum);
this.cím = cím;
this.státusz = státusz;
}
public Integer getÉletkor() {
return életkor;
}
#Override
public String toString() {
return "Személy [id=" + id + ", vezetéknév=" + vezetéknév
+ ", keresztnév=" + keresztnév + ", életkor=" + életkor
+ ", cím=" + cím + ", státusz=" + státusz + "]";
}
}
And people.xml file with data:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE személyek SYSTEM "people.dtd">
<személyek>
<személy id="micimacko">
<vezetéknév>Mici</vezetéknév>
<keresztnév>Mackó</keresztnév>
<születésidátum>1921.08.21</születésidátum>
<cím>Százholdas Pagony</cím>
<fotó>http://upload.wikimedia.org/wikipedia/en/1/10/Winniethepooh.png
</fotó>
</személy>
</személyek>
Running in the main class by the following:
public static void main(String args[]) {
SzemélyDAO ddd = new SzemélyDAOImpl();
System.out.println(ddd.getSzemélyById("micimacko"));
}
Could anybody help me, what am I doing wrong?Thanks.

As the exception states you need to add a no-arg constructor to your Személy class. This means a constructor that doesn't take any parameters. Currently the class has a single constructor that takes a String. Adding the following would work:
private Személy() {
}

Related

JAXb not populating object during unmarshal

I am a bit lost at this point. I am by no means a SOAP/JAXb expert, however, I am trying to create a generic class that will marshal/call/unmarshal for any service. I am using the Weather Service wsdl as a starting point to prove out the concept.
I have finally gotten the marshalling, call and unmarshalling to execute without error, however, the response object is not being populated. Can anyone assist in identifying what I am doing incorrectly? I am also looking for a good explanation to the answer if possible so I can learn from this experience.
Again, there is no error while excuting. The issue is that the value of GetCityWeatherByZIPResponse.GetCityWeatherByZIPResult comes out to be null. I know the document is returning the correct results as the result printout is as follows:
Result printout:
<?xml version="1.0" encoding="UTF-8"?><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
<GetCityWeatherByZIPResult>
<Success>true</Success>
<ResponseText>City Found</ResponseText>
<State>MO</State>
<City>Saint Charles</City>
<WeatherStationCity>Farmington</WeatherStationCity>
<WeatherID>4</WeatherID>
<Description>Sunny</Description>
<Temperature>79</Temperature>
<RelativeHumidity>47</RelativeHumidity>
<Wind>CALM</Wind>
<Pressure>30.00S</Pressure>
<Visibility/>
<WindChill/>
<Remarks/>
</GetCityWeatherByZIPResult>
</GetCityWeatherByZIPResponse>
Response: GetCityWeatherByZIPResult: null
Test Web Service:
http://wsf.cdyne.com/WeatherWS/Weather.asmx
Initial call (done via JBehave):
#Given("I call the weather soap service")
public void givenICallTheWeatherSoapService() {
GetCityWeatherByZIP weather = new GetCityWeatherByZIP();
weather.setZIP("63304");
try {
new WeatherTools();
WeatherSoap weatherSoap = new WeatherSoap();
GetCityWeatherByZIPResponse response = weatherSoap.getCityWeatherByZip("63304");
System.out.println("Response: " + response);
} catch (JAXBException | ParserConfigurationException | SOAPException | IOException e) {
Assert.fail(e.getMessage());
}
}
Soap Service Class:
public class WeatherSoap extends PTFSoapClient {
public WeatherSoap() throws JAXBException, ParserConfigurationException, SOAPException {
super(PTFApplication.getConfig(Environment.executionEnv.getEnv(), "Weather SOAP endpoint"));
}
public GetCityWeatherByZIPResponse getCityWeatherByZip(String zip) throws JAXBException, SOAPException, IOException {
GetCityWeatherByZIP weatherByZip = new GetCityWeatherByZIP();
weatherByZip.setZIP(zip);
try {
sendRequest(weatherByZip);
return (GetCityWeatherByZIPResponse) unmarshallResponse(GetCityWeatherByZIPResponse.class);
} catch (ParserConfigurationException | XMLStreamException e) {
e.printStackTrace();
return null;
}
}
}
Base Framework Class genericizing the call (usable for all SOAP calls):
public class PTFSoapClient {
private JAXBContext context;
private Marshaller marshaller;
private Object object;
private SOAPMessage message;
private String endpoint;
private SOAPMessage response;
public PTFSoapClient(String endpoint) {
this.endpoint = endpoint;
}
public void toConsole() throws JAXBException, SOAPException, IOException {
message.writeTo(System.out);
System.out.print("\n");
}
public SOAPMessage sendRequest(Object obj) throws JAXBException, ParserConfigurationException, SOAPException {
object = obj;
context = JAXBContext.newInstance(obj.getClass());
marshaller = context.createMarshaller();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
Document doc = dbf.newDocumentBuilder().newDocument();
marshaller.marshal(object,doc);
MessageFactory factory = MessageFactory.newInstance();
message = factory.createMessage();
message.getSOAPBody().addDocument(doc);
message.saveChanges();
SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
response = connection.call(message, endpoint);
connection.close();
try {
System.out.println("Response:");
response.writeTo(System.out);
System.out.println("");
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
public Object unmarshallResponse(Class<?> classname) throws JAXBException, XMLStreamException, SOAPException, IOException {
Document doc = response.getSOAPBody().extractContentAsDocument();
try {
System.out.println("Document: ");
printDocument(doc, System.out);
System.out.println("");
} catch (TransformerException e) {
e.printStackTrace();
}
Unmarshaller unmarshaller = JAXBContext.newInstance(classname).createUnmarshaller();
return unmarshaller.unmarshal(doc);
}
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc),
new StreamResult(new OutputStreamWriter(out, "UTF-8")));
}
}
Base unmarshal object:
#XmlRootElement(name = "GetCityWeatherByZIPResponse",
namespace = "http://ws.cdyne.com/WeatherWS/")
public class GetCityWeatherByZIPResponse {
GetCityWeatherByZIPResult GetCityWeatherByZIPResult;
public GetCityWeatherByZIPResult getGetCityWeatherByZIPResult() {
return GetCityWeatherByZIPResult;
}
public void setGetCityWeatherByZIPResult(GetCityWeatherByZIPResult GetCityWeatherByZIPResult) {
this.GetCityWeatherByZIPResult = GetCityWeatherByZIPResult;
}
#Override
public String toString() {
return "GetCityWeatherByZIPResult: " + GetCityWeatherByZIPResult;
}
}
Sub umarshal object:
public class GetCityWeatherByZIPResult {
boolean Success;
String ResponseText;
String State;
String City;
String WeatherStationCity;
String WeatherID;
String Description;
int Temperature;
int RelativeHumidity;
String Wind;
String Pressure;
String Visibility;
String WindChill;
String Remarks;
public boolean isSuccess() {
return Success;
}
public void setSuccess(boolean success) {
Success = success;
}
public String getResponseText() {
return ResponseText;
}
public void setResponseText(String responseText) {
ResponseText = responseText;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getWeatherStationCity() {
return WeatherStationCity;
}
public void setWeatherStationCity(String weatherStationCity) {
WeatherStationCity = weatherStationCity;
}
public String getWeatherID() {
return WeatherID;
}
public void setWeatherID(String weatherID) {
WeatherID = weatherID;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public int getTemperature() {
return Temperature;
}
public void setTemperature(int temperature) {
Temperature = temperature;
}
public int getRelativeHumidity() {
return RelativeHumidity;
}
public void setRelativeHumidity(int relativeHumidity) {
RelativeHumidity = relativeHumidity;
}
public String getWind() {
return Wind;
}
public void setWind(String wind) {
Wind = wind;
}
public String getPressure() {
return Pressure;
}
public void setPressure(String pressure) {
Pressure = pressure;
}
public String getVisibility() {
return Visibility;
}
public void setVisibility(String visibility) {
Visibility = visibility;
}
public String getWindChill() {
return WindChill;
}
public void setWindChill(String windChill) {
WindChill = windChill;
}
public String getRemarks() {
return Remarks;
}
public void setRemarks(String remarks) {
Remarks = remarks;
}
}
Your Current Mapping
When you specify the namespace property on the #XmlRootElement annotation, it only applies to that one element.
#XmlRootElement(name = "GetCityWeatherByZIPResponse",
namespace = "http://ws.cdyne.com/WeatherWS/")
public class GetCityWeatherByZIPResponse {
Your XML Document
Your XML document specifies a default namespace. This means that all elements without another explicit namespace mapping are also part of the http://ws.cdyne.com/WeatherWS/ namespace.
<?xml version="1.0" encoding="UTF-8"?><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/">
<GetCityWeatherByZIPResult>
<Success>true</Success>
The Namespace Fix
You are going to want to specify the namespace mapping at the package level so that it applies to all your element mappings. This is done using the package level #XmlSchema annotation on a speciial class called package-info.
#XmlSchema(
namespace = "http://ws.cdyne.com/WeatherWS/",
elementFormDefault = XmlNsForm.QUALIFIED)
package example;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
For More Information
I have written more about JAXB and namespace qualification on my blog:
http://blog.bdoughan.com/2010/08/jaxb-namespaces.html
Update
Default Element Names
The default elements for your properties don't match your XML. for the property below the expected element name will be getCityWeatherByZIPResult so you will need to override the default using the #XmlElement annotation.
#XmlElement(name="GetCityWeatherByZIPResult")
public GetCityWeatherByZIPResult getGetCityWeatherByZIPResult() {
return GetCityWeatherByZIPResult;
}
Debugging Tip
When you encounter problems unmarshalling, populate your object model and marshal it to see what the expected XML is based on your current mappings.

Json returns empty in android

I created this class and I created my data in json. It seems that doesn't work and returns empty when I try to insert the data in a database sqLite. This is the class with the creation of the Json:
public class Corsa
{
public Corsa corsaData = null;
public int id = 0;
public String oraPartenza;
public String oraArrivo;
public String partenza;
public String arrivo;
public String codiceLinea;
public String codiceCorsa;
public String tipoCorsa;
public String linkDettaglio;
#Override
public String toString() {
return partenza + " / "+ oraPartenza + "\n" + arrivo + " / " +oraArrivo;
}
public Corsa() {
}
/** Json **/
public static Corsa LoadJson(String jsonStr)
{
Corsa corsaJson = new Corsa();
try
{
JSONObject jObj = new JSONObject(jsonStr);
corsaJson.oraPartenza = HtmlUtils.getJSonString(jObj, "oraPartenza");
corsaJson.oraArrivo = HtmlUtils.getJSonString(jObj, "oraArrivo");
corsaJson.partenza = HtmlUtils.getJSonString(jObj, "partenza");
corsaJson.arrivo = HtmlUtils.getJSonString(jObj, "arrivo");
corsaJson.linkDettaglio = HtmlUtils.getJSonString(jObj, "linkDettaglio");
corsaJson.codiceCorsa = HtmlUtils.getJSonString(jObj, "codiceCorsa");
corsaJson.codiceLinea = HtmlUtils.getJSonString(jObj, "codiceLinea");
}
catch (Exception e) { e.printStackTrace(); }
return corsaJson;
}
public static JSONObject CreateJSon(Corsa corsaJson)
{
JSONObject jObj = new JSONObject();
try
{
jObj.put("oraPartenza", corsaJson.oraPartenza);
jObj.put("oraArrivo", corsaJson.oraArrivo);
jObj.put("partenza", corsaJson.partenza);
jObj.put("arrivo", corsaJson.arrivo);
jObj.put("linkDettaglio", corsaJson.linkDettaglio);
jObj.put("codiceCorsa", corsaJson.codiceCorsa);
jObj.put("codiceLinea", corsaJson.codiceLinea);
} catch (Exception e) { e.printStackTrace(); }
return jObj;
}
}
This is the saved json:
public static void SaveCorsa(Context context, Corsa c)
{
DeleteCorsa(context, c);
SqlHelper dbHelper = null;
try {
dbHelper = new SqlHelper(context);
SQLiteDatabase database = dbHelper.getWritableDatabase();
ContentValues initialValues = new ContentValues();
initialValues.put("CORSA_DATA", Corsa.CreateJSon(c.corsaData).toString());
database.insert(TABLE_PREFERITI, null, initialValues);
Log.d("Insert preferiti", initialValues.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dbHelper!=null) dbHelper.close();
}
}
and the logCat returns CORSA_DATA{}..So it is empty.. But it's strange beacause is created in the activity..Any idea what's wrong? Thanks

Implementing JAXB without POJO classes and with linkedHashmaps

I am trying to use JAXB implementation to convert my class objects to XML and vice versa.
I implemented this by observing a tutorial.
My XML looks like this
<Alphabet>
<a>Apple</a>
<b>Ball</b>
<c>Cat</c>
<d>Dog</d>
<e>Elephant</e>
<f>Fox</f>
</Alphabet>
I wrote the following classes
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
public class Alph {
#XmlElement
private LinkedHashMap<String, String> cLinkedHashMap = new
LinkedHashMap<String, String>();
protected void put(String theKey, String theValue) {
cLinkedHashMap.put(theKey, theValue);
}
protected String get(String theKey) {
return (String) cLinkedHashMap.get(theKey);
}
protected Set<Entry<String,String>> getCEntrySet() {
return cLinkedHashMap.entrySet();
}
protected LinkedHashMap<String, String> getCLinkedHashMap() {
return cLinkedHashMap;
}
public String toCXML() throws XMLHandlingException {
return null;
}
}
#XmlRootElement
public class Alphabet extends Alph {
public static Alphabet getInstance(String theAlphabetXML) throws
XMLHandlingException {
return XMLUtils.parseAlphabetXML(theAlphabetXML);
}
public String toCXML() throws XMLHandlingException {
return XMLUtils.getAlphabetXML(this);
}
}
I created the following XMLUtil Methods for marshalling and unmarshalling
public static String getAlphabetXML(Alph theAlphabet) throws XMLHandlingException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Writer writer = null;
try {
writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" );
} catch (IOException e) {
e.printStackTrace();
}
try {
JAXBContext JContext = JAXBContext.newInstance(Alphabet.class);
Marshaller JMarshaller = JContext.createMarshaller();
JMarshaller.marshal(theAlphabet, writer);
} catch (Throwable e) {
e.printStackTrace();
}
String theAlphabetXML = byteArrayOutputStream.toString();
return theAlphabetXML;
}
public static Alphabet parseAlphabetXML(String theAlphabetXML) throws
XMLHandlingException {
if(null == theAlphabetXML) {
return null;
}
try {
InputStream IPStream = new ByteArrayInputStream(theALphabetXML.getBytes());
JAXBContext JContext = JAXBContext.newInstance(Alphabet.class);
Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
Alphabet alphabet = (Alphabet) JUnmarshaller.unmarshal(IPStream);
return alphabet;
} catch(Throwable t) {
t.printStackTrace();
}
}
My problem is that when ever I try to marshal or unmarshal I am getting the follwoing error
"javax.xml.bind.UnmarshalException: unexpected element (a:"", local:"Alphabet"). Expected elements are <{}Alphabet>"
Did I miss something? Any help is appreciated.
Thanks
Update
I followed http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-using-maps-key-as.html and updated my code as follows
Request and response XML
<AlphabetReq>
<a>Apple</a>
<b>Ball</b>
<c>Cat</c>
<d>Dog</d>
<e>Elephant</e>
<f>Fox</f>
</AlphabetReq>
<AlphabetResp>
<a>Apple</a>
<b>Ball</b>
<c>Cat</c>
<d>Dog</d>
<e>Elephant</e>
<f>Fox</f>
</AlphabetResp>
AlphabetReq and AlphabetResp classes
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class Alph {
#XmlPath(".")
#XmlJavaTypeAdapter(AlphAdapter.class)
private LinkedHashMap<String, String> cLinkedHashMap = new
LinkedHashMap<String, String>();
#XmlPath(".")
#XmlJavaTypeAdapter(AlphAdapter.class)
private LinkedHashMap<String, String> gLinkedHashMap = new
LinkedHashMap<String, String>();
protected void put(String theKey, String theValue) {
cLinkedHashMap.put(theKey, theValue);
gLinkedHashMap.put(theKey, theValue);
}
protected String get(String theKey) {
return (String) cLinkedHashMap.get(theKey);
}
protected Set<Entry<String,String>> getCEntrySet() {
return cLinkedHashMap.entrySet();
}
protected Set<Entry<String,String>> getGEntrySet() {
return gLinkedHashMap.entrySet();
}
protected LinkedHashMap<String, String> getCLinkedHashMap() {
return cLinkedHashMap;
}
protected LinkedHashMap<String, String> getGLinkedHashMap() {
return gLinkedHashMap;
}
public String toCXML() throws XMLHandlingException {
return null;
}
public String toGXML() throws XMLHandlingException {
return null;
}
}
#XmlRootElement(name="AlphReq")
#XmlDiscriminatorValue("AlphabetReq")
#XmlAccessorType(XmlAccessType.FIELD)
public class AlphabetReq extends Alph {
public static AlphabetReq getInstance(String theAlphabetReqXML) throws
XMLHandlingException {
return XMLUtils.parseAlphabetReqXML(theAlphabetReqXML);
}
public String toCXML() throws XMLHandlingException {
return XMLUtils.getAlphabetReqXML(this);
}
}
#XmlRootElement(name="AlphResp")
#XmlDiscriminatorValue("AlphabetResp")
#XmlAccessorType(XmlAccessType.FIELD)
public class AlphabetResp extends Alph {
public static AlphabetResp getInstance(String theAlphabetRespXML) throws
XMLHandlingException {
return XMLUtils.parseAlphabetRespXML(theAlphabetRespXML);
}
public String toCXML() throws XMLHandlingException {
return XMLUtils.getAlphabetRespXML(this);
}
}
I created the following XMLUtil Methods for marshalling and unmarshalling
public static String getAlphabetReqXML(Alph theAlphabet) throws XMLHandlingException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Writer writer = null;
try {
writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" );
} catch (IOException e) {
e.printStackTrace();
}
try {
JAXBContext JContext = JAXBContext.newInstance(AlphabetReq.class);
Marshaller JMarshaller = JContext.createMarshaller();
JMarshaller.marshal(theAlphabet, writer);
} catch (Throwable e) {
e.printStackTrace();
}
String theAlphabetReqXML = byteArrayOutputStream.toString();
return theAlphabetReqXML;
}
public static AlphabetReq parseAlphabetReqXML(String theAlphabetReqXML) throws
XMLHandlingException {
if(null == theAlphabetReqXML) {
return null;
}
try {
InputStream IPStream = new ByteArrayInputStream(theAlphabetReqXML.getBytes());
JAXBContext JContext = JAXBContext.newInstance(AlphabetReq.class);
Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
AlphabetReq alphabetreq = (AlphabetReq) JUnmarshaller.unmarshal(IPStream);
return alphabetreq;
} catch(Throwable t) {
t.printStackTrace();
}
}
public static String getAlphabetRespXML(Alph theAlphabet) throws XMLHandlingException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Writer writer = null;
try {
writer = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" );
} catch (IOException e) {
e.printStackTrace();
}
try {
JAXBContext JContext = JAXBContext.newInstance(AlphabetResp.class);
Marshaller JMarshaller = JContext.createMarshaller();
JMarshaller.marshal(theAlphabet, writer);
} catch (Throwable e) {
e.printStackTrace();
}
String theAlphabetRespXML = byteArrayOutputStream.toString();
return theAlphabetRespXML;
}
public static AlphabetResp parseAlphabetReqXML(String theAlphabetRespXML) throws
XMLHandlingException {
if(null == theAlphabetRespXML) {
return null;
}
try {
InputStream IPStream = new ByteArrayInputStream(theAlphabetRespXML.getBytes());
JAXBContext JContext = JAXBContext.newInstance(AlphabetResp.class);
Unmarshaller JUnmarshaller = JContext.createUnmarshaller();
AlphabetResp alphabetresp = (AlphabetResp) JUnmarshaller.unmarshal(IPStream);
return alphabetresp;
} catch(Throwable t) {
t.printStackTrace();
}
}
and introduced an Adapter class
import java.util.*;
import java.util.Map.Entry;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import org.eclipse.persistence.oxm.annotations.XmlVariableNode;
public class AlphAdapter extends XmlAdapter<AlphAdapter.AdaptedMap, LinkedHashMap<String, String>>{
public static class AdaptedMap {
#XmlVariableNode("key")
List<AdaptedEntry> entries = new ArrayList<AdaptedEntry>();
}
public static class AdaptedEntry {
#XmlTransient
public String key;
#XmlValue
public String value;
}
#Override
public AdaptedMap marshal(LinkedHashMap<String, String> map) throws Exception {
AdaptedMap adaptedMap = new AdaptedMap();
for(Entry<String, String> entry : map.entrySet()) {
AdaptedEntry adaptedEntry = new AdaptedEntry();
adaptedEntry.key = entry.getKey();
adaptedEntry.value = entry.getValue();
adaptedMap.entries.add(adaptedEntry);
}
return adaptedMap;
}
#Override
public LinkedHashMap<String, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
List<AdaptedEntry> adaptedEntries = adaptedMap.entries;
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(adaptedEntries.size());
for(AdaptedEntry adaptedEntry : adaptedMap.entries) {
map.put(adaptedEntry.key, adaptedEntry.value);
}
return map;
}
}
When I run this I am not getting any values into my adaptedmap. it has keys but values as null
for ex:
key: A, value: null
key: B, value: null
key: C, value: null
key: D, value: null
key: E, value: null
key: F, value: null
Any help is appreciated.
Thanks
You can achieve this simple XML creation (that you have given) as below :
#XmlRootElement
public class Alphabet {
private String a;
private String b;
private String c;
private String d;
private String e;
private String f;
public Alphabet() {
}
public Alphabet(String a, String b, String c, String d, String e, String f) {
super();
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.f = f;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
public String getE() {
return e;
}
public void setE(String e) {
this.e = e;
}
public String getF() {
return f;
}
public void setF(String f) {
this.f = f;
}
}
This class is to create the xml file :
public class AlphabetToXML {
public static void main(String[] args) {
Alphabet alpha = new Alphabet("Apple", "Ball", "Cat", "Dog",
"Elephant", "Fox");
try {
String filePath = "PATH_TO_SAVE_YOUR_FILE";
File file = new File(filePath);
JAXBContext jaxbContext = JAXBContext.newInstance(Alphabet.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(alpha, file);
jaxbMarshaller.marshal(alpha, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
And this is to read your XML back to Java object :
public class XMLToAplhabet {
public static void main(String[] args) {
try {
String filePath = "XML_FILE_PATH";
File file = new File(filePath);
JAXBContext jaxbContext = JAXBContext.newInstance(Alphabet.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Alphabet aplha = (Alphabet) jaxbUnmarshaller.unmarshal(file);
System.out.println(aplha);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
I don't see any use of LinkedHashMap to create a simple XML like this.
http://java.dzone.com/articles/jaxb-and-javautilmap might be helpful. Contains different ways for doing the same task.

Java warning and error (read object)

My goal: save one ArrayList to a .dat file, after read this file and in the end print this array.
To save the ArrayList, "equipas" is one ArrayList< Equipa>, I use this function:
saveMyFile("Equipas.dat", (Object) equipas);
To read:
public static ArrayList<Equipa> readMyFile(String s){
ArrayList<Equipa> novo = new ArrayList<Equipa>();
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(s));
novo = (ArrayList<Equipa>) ois.readObject();
ois.close();
}
catch(IOException er) { System.out.println(er.getMessage()); }
catch(ClassNotFoundException er) { System.out.println(er.getMessage()); }
return novo;}
In this read function, I have one Compilation Warning: "…uses unchecked or unsafe operations. Recompile with - Xlint:unchecked for details."
To save:
public static void saveMyFile(String s, Object o)
{
try {
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(s));
oos.writeObject(o);
oos.flush();
oos.close();
}
catch(IOException e) { System.out.println(e.getMessage()); }
}
Finally, I want to print the ArrayList's info:
ArrayList<Equipa> cena = new ArrayList<Equipa>();
cena=(ArrayList<Equipa>) readMyFile("Equipas.dat");
for(Equipa e:cena)
e.toString();
Error when I try to run:
" writing aborted; java.io.NotSerializableException: Equipa"
Equipa havs the Serializable:
import java.util.*;
import java.io.*;
public class Equipa implements Serializable
{
private String nome;
private Carro carro;
private ArrayList<Piloto> pilotos;
private double tempoDecorrido;
private int pontos;
private boolean desistiu;
private int voltaDesistencia;
private Piloto piloto;
/**
* Constructor for objects of class Equipa
*/
public Equipa()
{
this.nome = "NA";
this.carro = null;
this.pilotos = new ArrayList<Piloto>();
this.tempoDecorrido = 0;
this.pontos = 0;
this.desistiu = false;
this.voltaDesistencia = 0;
this.piloto = null;
}
public Equipa(String nome, Carro carro, ArrayList<Piloto> pilotos)
{
this.nome = nome;
this.carro = carro;
//this.pilotos = new ArrayList<Piloto>(pilotos);
this.pilotos = pilotos;
this.tempoDecorrido = 0;
this.pontos = 0;
this.desistiu = false;
this.voltaDesistencia = 0;
//this.piloto = pilotos.get(0);
}
public Equipa (Equipa e)
{
this.nome = e.getNome();
this.carro = e.getCarro();
this.pilotos = e.getPilotos();
this.tempoDecorrido = e.getTempoDecorrido();
this.pontos = e.getPontos();
this.desistiu = e.getDesistiu();
this.voltaDesistencia = e.getVoltaDesistencia();
//this.piloto = e.getPiloto();
}
/** Getters */
public String getNome()
{
return this.nome;
}
public Carro getCarro()
{
return this.carro;
}
public ArrayList<Piloto> getPilotos()
{
return new ArrayList<Piloto>(this.pilotos);
}
public double getTempoDecorrido()
{
return this.tempoDecorrido;
}
public int getPontos()
{
return this.pontos;
}
public boolean getDesistiu()
{
return this.desistiu;
}
public int getVoltaDesistencia()
{
return this.voltaDesistencia;
}
public Piloto getPiloto()
{
return this.piloto;
}
/** Setters */
public void setNome(String nome)
{
this.nome = nome;
}
public void setCarro(Carro carro)
{
this.carro = carro;
}
public void setPilotos(ArrayList<Piloto> pilotos)
{
this.pilotos = new ArrayList<Piloto>(pilotos);
}
public void setTempoDecorrido(double tempoDecorrido)
{
this.tempoDecorrido = tempoDecorrido;
}
public void setPontos(int pontos)
{
this.pontos = pontos;
}
public void setDesistiu(boolean desistiu)
{
this.desistiu = desistiu;
}
public void setVoltaDesistencia(int voltaDesistencia)
{
this.voltaDesistencia = voltaDesistencia;
}
public void setPiloto(Piloto piloto)
{
this.piloto = piloto;
}
/** Outros Métodos */
public Equipa clone()
{
return new Equipa(this);
}
public boolean equals(Equipa e)
{
if(this.nome == e.getNome())
return true;
else
return false;
}
public String getStringPilotos()
{
String s = new String();
for(Piloto p: this.pilotos)
s = (s + ", " + p.getNome());
return s;
}
public String toString()
{
return new String("Nome da equipa: " + nome + "; Categoria do carro: " + carro.getClass().getName() + "; Marca e modelo: " + carro.getMarca() + " " + carro.getModelo() + "; Pilotos: " + getStringPilotos())+"\n";
}
Implementing Serializable means that serialization is permitted, but not necessarily that it is possible. For it to work, everything referenced by Equipa must also be either primitive or Serializable (and so on, recursively). Is this the case?
Warning in the read function is the result of generics in java. You won't be able to suppress it, unless you use #SuppressWarnings("unchecked") to ignore it.
If you are sure you are reading an ArrayList<Equipa>, you can ignore it without any problem.
With the Equipa code, I can try to point to the Serializable problem: make sure that Carro and Piloto classes are also Serializables. You can add the code of theses classes if you are not sure.
The only type-safer way would be do a custom serialization, using writeObject(OutputStream) and readObjectInputStream say on a class ArrayListOfEquipa maybe using Equipa[] (ArrayList.toArray()).
Not really attractive, if the warning would be the only reason.

Update text file method

I have a jtable that can be edite and then saved (updated) to a text file.
User select a line (that contains a book record) and request to borrow that book,
I use this method to update, But now when update, the old data is not deleted.
user_AllBooks uAllBooks = new user_AllBooks();
#Override
public void actionPerformed(ActionEvent event) {
if (event.getSource() == borrowButton) {
borrowInitialize(bTable.getSelectedRow());
}
public void borrowInitialize(int row) {
if (uAllBooks.getValueAt(row, 3).equals("Yes")) {
JOptionPane.showMessageDialog(null, "This Book Was Borrowed");
} else {
uAllBooks.setValueAt("Yes", row, 3);
uAllBooks.fireTableRowsUpdated(row, row);
uAllBooks.updateFiles(uAllBooks.bData);
}
}
...
}
public class user_AllBooks extends AbstractTableModel {
...
public void updateFiles(ArrayList<BookInformation> data) {
PrintWriter Bpw = null;
try {
Bpw = new PrintWriter(new FileWriter("AllBookRecords.txt" , true));
for (BookInformation bookinfo : data) {
String line = bookinfo.getBookID()
+ " " + bookinfo.getBookName()
+ " " + bookinfo.getBookDate()
+ " " + bookinfo.getBorrowStatus();
Bpw.println(line);
}
Bpw.close();
} catch (FileNotFoundException e1) {
} catch (IOException ioe) {
}
}
...
}
My BookInformation Class:
public class BookInformation {
private String BookName;
private String BookDate;
private String BookID;
private String BorrowStatus;
public String getBookName() {
return BookName;
}
public void setBookName(String book_name) {
this.BookName = book_name;
}
public String getBookDate() {
return BookDate;
}
public void setBookDate(String book_date) {
this.BookDate = book_date;
}
public String getBookID() {
return BookID;
}
public void setBookID(String Book_id) {
this.BookID = Book_id;
}
#Override
public String toString() {
return BookID + " " + BookName + " "
+ BookDate + " " + BorrowStatus + "\n";
}
public String getBorrowStatus() {
return BorrowStatus;
}
public void setBorrowStatus(String borrowStat) {
BorrowStatus = borrowStat;
}
}
Thanks.
Change this line
Bpw = new PrintWriter(new FileWriter("AllBookRecords.txt" , true));
to
Bpw = new PrintWriter(new FileWriter("AllBookRecords.txt" , false));
The second parameter (boolean) changes whether it should append the text file (add to the end of it) or just rewrite everything.
Source: Javadoc constructor summary for FileWriter:
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean
indicating whether or not to append the data written.

Categories