Created
April 12, 2017 18:07
-
-
Save 13andrew13/f72c1a335fdbc1b95e5e0cc9620d3b99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package task1; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.SAXException; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import java.io.File; | |
import java.io.IOException; | |
/** | |
* Created by andrew on 08.04.17. | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
try { | |
Trains trainsList = new Trains(); | |
File xmlFile = new File("/home/andrew/IdeaProjects/XML&&JSON/src/task1/trains.xml"); | |
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); | |
Document document = documentBuilder.parse(xmlFile); | |
Element trains = document.getDocumentElement(); | |
NodeList nodeList = trains.getChildNodes(); | |
for(int i = 0; i< nodeList.getLength();i++){ | |
Node node = nodeList.item(i); | |
if(node.getNodeType()==Node.ELEMENT_NODE){ | |
Element element = (Element) node; | |
Train train = new Train(); | |
train.setId(element.getAttributes().getNamedItem("id").getNodeValue()); | |
train.setFrom(element.getElementsByTagName("from").item(0).getChildNodes().item(0).getNodeValue()); | |
train.setTo(element.getElementsByTagName("to").item(0).getChildNodes().item(0).getNodeValue()); | |
train.setDate(element.getElementsByTagName("date").item(0).getChildNodes().item(0).getNodeValue()); | |
train.setDeparture(element.getElementsByTagName("departure").item(0).getChildNodes().item(0).getNodeValue()); | |
trainsList.add(train); | |
} | |
} | |
System.out.println(trainsList); | |
} catch (ParserConfigurationException e) { | |
e.printStackTrace(); | |
} catch (SAXException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package task1; | |
import javax.xml.bind.*; | |
import java.io.File; | |
/** | |
* Created by andrew on 08.04.17. | |
*/ | |
public class Main2 { | |
public static void main(String[] args) { | |
Trains trains = getTrains(); | |
trains.add(new Train("England","sdaasd","sdadsad","sdsadasd")); | |
writeTrains(trains); | |
System.out.println(getTrains()); | |
} | |
private static void writeTrains(Trains trains) { | |
try{ File xmlFile = new File("/home/andrew/IdeaProjects/XML&&JSON/src/task1/trains.xml"); | |
JAXBContext jaxbContext = JAXBContext.newInstance(Trains.class); | |
Marshaller marshaller = jaxbContext.createMarshaller(); | |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); | |
marshaller.marshal(trains,xmlFile); | |
marshaller.marshal(trains,System.out); | |
} catch (PropertyException e) { | |
e.printStackTrace(); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static Trains getTrains() { | |
try{ | |
File xmlFile = new File("/home/andrew/IdeaProjects/XML&&JSON/src/task1/trains.xml"); | |
JAXBContext jaxbContext = JAXBContext.newInstance(Trains.class); | |
Marshaller marshaller = jaxbContext.createMarshaller(); | |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true); | |
Unmarshaller unmarshaller= jaxbContext.createUnmarshaller(); | |
Trains trains = (Trains) unmarshaller.unmarshal(xmlFile); | |
return trains; | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package task1; | |
import javax.xml.bind.annotation.XmlAttribute; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
/** | |
* Created by andrew on 08.04.17. | |
*/ | |
@XmlRootElement(name= "train") | |
public class Train { | |
private String id; | |
private String from; | |
private String to; | |
private String date; | |
private String departure; | |
public Train(){} | |
public Train(String from, String to, String date, String departure) { | |
this.from = from; | |
this.to = to; | |
this.date = date; | |
this.departure = departure; | |
} | |
@XmlAttribute | |
public void setId(String id) { | |
this.id = id; | |
} | |
@XmlElement | |
public void setFrom(String from) { | |
this.from = from; | |
} | |
@XmlElement | |
public void setTo(String to) { | |
this.to = to; | |
} | |
@XmlElement | |
public void setDate(String date) { | |
this.date = date; | |
} | |
@XmlElement | |
public void setDeparture(String departure) { | |
this.departure = departure; | |
} | |
@Override | |
public String toString() { | |
return "task1.Train{" + | |
"id = " + id + | |
", from='" + from + '\'' + | |
", to='" + to + '\'' + | |
", date='" + date + '\'' + | |
", departure='" + departure + '\'' + | |
'}'; | |
} | |
public String getId() { | |
return id; | |
} | |
public String getFrom() { | |
return from; | |
} | |
public String getTo() { | |
return to; | |
} | |
public String getDate() { | |
return date; | |
} | |
public String getDeparture() { | |
return departure; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package task1; | |
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* Created by andrew on 08.04.17. | |
*/ | |
@XmlRootElement(name = "trains") | |
public class Trains { | |
public List<Train> getTrains() { | |
return trains; | |
} | |
@XmlElement(name = "train") | |
private List<Train> trains = new ArrayList<>(); | |
public void add(Train train){ | |
trains.add(train); | |
} | |
@Override | |
public String toString() { | |
return "Trains{" + | |
"trains=" + trains + | |
'}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<trains> | |
<train id="1"> | |
<date>19.12.2013</date> | |
<departure>15:05</departure> | |
<from>Kyiv</from> | |
<to>Donetsk</to> | |
</train> | |
<train id="2"> | |
<date>19.12.2013</date> | |
<departure>19:05</departure> | |
<from>Lviv</from> | |
<to>Donetsk</to> | |
</train> | |
<train/> | |
<train> | |
<date>sdadsad</date> | |
<departure>sdsadasd</departure> | |
<from>England</from> | |
<to>sdaasd</to> | |
</train> | |
<train> | |
<date>sdadsad</date> | |
<departure>sdsadasd</departure> | |
<from>England</from> | |
<to>sdaasd</to> | |
</train> | |
<train> | |
<date>sdadsad</date> | |
<departure>sdsadasd</departure> | |
<from>England</from> | |
<to>sdaasd</to> | |
</train> | |
</trains> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment