Hej! Wykonywałem projekt na studia, natomiast spotkałem się z wymaganiem zapisania danych do formatu XML. Nigdy wcześniej nie robiłem tego, więc postanowiłem trochę poczytać w pierwszej kolejności, pooglądać tutoriali i spróbować samemu. Natomiast nie udało się. Wysypywały się błędy. I w ten sposób znalazłem się tutaj :) przychodzę do Was z pytaniem jak mogę zapisać wartości konstruktora (ostatnia klasa DataAdress) do .xml? Mam takie 4 klasy jak poniżej: Ta ostatnia miała tworzyć obiekt potrzebny do zrobienia pliku xml, ale coś mi nie pykło. Jest tutaj ktoś zorientowany w temacie, kto znalazłby chwilę żeby mi wytłumaczyć co jest źle? :/ nie daję linku do githuba, bo jest w powijakach jeszcze >_<
Dzięki wielkie za wszelką pomoc!
Pozdrawiam
package pl.AdressCalculator;
import java.util.Scanner;
@XmlRootElement
public class AdressCalculator {
public AdressCalculator() { }
public static void main(String[] args){
String adressIP;
System.out.print("Enter IP Adress with short version of mask. For example: 190.230.32.120/23\n");
Scanner scanner = new Scanner(System.in);
adressIP = scanner.next();
System.out.println("\n---- IS THE IP ADRESS VALID?");
AdressFunctions checkIP = new AdressFunctions();
checkIP.checkIfAdressIPisCorrect(adressIP);
ExtrasFunctions split = new ExtrasFunctions();
split.breakAdressIP(adressIP);
}
}
package pl.AdressCalculator;
// ZMIENNE POSIADAJĄ KOŃCÓWKI NAZW BIN I DEC W ZALEŻNOŚCI OD TEGO CZY PRZECHOWUJĄ WARTOŚCI
//BINARNE CZY DZIESIĘTNE
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import static pl.AdressCalculator.JavaXml.DataAdress.objectToXml;
@XmlRootElement
public class ExtrasFunctions {
public ExtrasFunctions() { }
// 0. Rozbija informacje o adresie ip i je porządkuje
// JAKO ARGUMENT PRZYJMUJE ADRES IP NP. 120.43.233.120/32
public void breakAdressIP(String adressIP) {
String[] ip = splitAdressIP(adressIP);
String ipadressdec = linkAdress(ip);
String ipadressbin = makeBin(ip);
String shortmask = ip[4];
String maskbin = makeBinMask(ip[4]);
String rmaskbin = makeReverseBinMask(ip[4]);
String maskdec = makeDecMask(maskbin);
String classadress = adressClass(makeDecMask(makeBinMask(ip[4])));
String networkadressbin = adressNetwork(ipadressbin, maskbin);
String networkadressdec = makeDecMask(networkadressbin);
String broadcastadress = adressBroadcast(networkadressdec, rmaskbin);
String firsthost = firstAdressOfHost(networkadressdec);
String lasthost = lastAdressOfHost(broadcastadress);
String allhosts = allHosts(ip[4]);
JavaXml.DataAdress dataAdress = new JavaXml.DataAdress(ipadressdec, networkadressdec,
broadcastadress, classadress, maskdec,
firsthost, lasthost, allhosts);
try {
FileOutputStream fos = new FileOutputStream(new File("./ipadress.xml"));
XMLEncoder encoder = new XMLEncoder(fos);
encoder.writeObject(dataAdress);
encoder.close();
fos.close();
System.out.println(objectToXml(dataAdress));
} catch (javax.xml.bind.JAXBException | FileNotFoundException ex) {
Logger.getLogger(JavaXml.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException e) {
e.printStackTrace();
}
}
public String[] splitAdressIP(String adressIP) { // jakies instrukcje }
public String linkAdress(String[] adress) { // jakies instrukcje }
public String makeBin(String[] dec) { // jakies instrukcje }
public String decToBin(String adressIP) { // jakies instrukcje }
public int binToDec(String adressIP) { // jakies instrukcje } makeBinMask(String shortmask) { // jakies instrukcje }
public String makeReverseBinMask(String shortmask) { // jakies instrukcje }
public String makeDecMask(String mask) { // jakies instrukcje }
public String adressClass(String adressIP) { // jakies instrukcje }
public String adressNetwork(String ipbin, String maskabin) { // jakies instrukcje }
public String adressBroadcast(String adressNetwork, String reverseMaskBin) { // jakies instrukcje }
public String firstAdressOfHost(String adressNetwork) { // jakies instrukcje }
public String lastAdressOfHost(String adressBroadcast) { // jakies instrukcje }
public String allHosts(String shortmask) { // jakies instrukcje }
}
package pl.AdressCalculator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@XmlRootElement
public class AdressFunctions {
public AdressFunctions() { }
public void checkIfAdressIPisCorrect(String adressIP) {
Pattern compiledPattern = Pattern.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\/([1-9]|[1-2][0-9]|3[0-2])$");
Matcher matcher = compiledPattern.matcher(adressIP);
if (matcher.matches() == true) {
System.out.print("IP Adress: " + adressIP + " is CORRECT");
} else {
System.out.print("IP Adress: " + adressIP + " is INCORRECT!\n");
}
}
}
package pl.AdressCalculator;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.StringWriter;
public class JavaXml {
@XmlRootElement
static class DataAdress {
private String ipadress;
private String networkadress;
private String broadcastadress;
private String classadress;
private String fullmask;
private String firsthost;
private String lasthost;
private String maxhosts;
public DataAdress() {}
public DataAdress(String ipadress, String networkadress,
String broadcastadress, String classadress,
String fullmask, String firsthost, String lasthost,
String maxhosts) {
this.ipadress = ipadress;
this.networkadress = networkadress;
this.broadcastadress = broadcastadress;
this.classadress = classadress;
this.fullmask = fullmask;
this.firsthost = firsthost;
this.lasthost = lasthost;
this.maxhosts = maxhosts;
}
public String getIpadress() {
return ipadress;
}
public void setIpadress(String ipadress) {
this.ipadress = ipadress;
}
public String getNetworkadress() {
return networkadress;
}
public void setNetworkadress(String networkadress) {
this.networkadress = networkadress;
}
public String getBroadcastadress() {
return broadcastadress;
}
public void setBroadcastadress(String broadcastadress) {
this.broadcastadress = broadcastadress;
}
public String getClassadress() {
return classadress;
}
public void setClassadress(String classadress) {
this.classadress = classadress;
}
public String getFullmask() {
return fullmask;
}
public void setFullmask(String fullmask) {
this.fullmask = fullmask;
}
public String getFirsthost() {
return firsthost;
}
public void setFirsthost(String firsthost) {
this.firsthost = firsthost;
}
public String getLasthost() {
return lasthost;
}
public void setLasthost(String lasthost) {
this.lasthost = lasthost;
}
public String getMaxhosts() {
return maxhosts;
}
public void setMaxhosts(String maxhosts) {
this.maxhosts = maxhosts;
}
public static <T> String objectToXml(T object) throws PropertyException, JAXBException {
JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter writter = new StringWriter();
marshaller.marshal(object, writter);
return writter.toString();
}
}
}