Last changes (#468)

* dom4j

* added more parsers

* StaxParser

* Jaxb binding

* Jaxb binding

* Finish article

* apply some changes

* Organize imports
This commit is contained in:
Raquel Garrido 2016-06-28 21:11:04 +02:00 committed by Grzegorz Piwowarek
parent a79e7b7ea2
commit 0ab62700ca
10 changed files with 61 additions and 113 deletions

View File

@ -2,7 +2,6 @@ package com.baeldung.xml;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
@ -40,7 +39,7 @@ public class JaxbParser {
tut.setType("XML");
tut.setTitle("XML with Jaxb");
tut.setDescription("XML Binding with Jaxb");
tut.setDate(new Date());
tut.setDate("04/02/2015");
tut.setAuthor("Jaxb author");
tutorials.getTutorial().add(tut);

View File

@ -17,7 +17,7 @@ import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import com.baeldung.xml.model.Tutorial;
import com.baeldung.xml.binding.Tutorial;
public class StaxParser {

View File

@ -1,7 +1,5 @@
package com.baeldung.xml.binding;
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
@ -11,7 +9,7 @@ public class Tutorial {
private String type;
private String title;
private String description;
private Date date;
private String date;
private String author;
@ -44,11 +42,11 @@ public class Tutorial {
public void setDescription(String description) {
this.description = description;
}
public Date getDate() {
public String getDate() {
return date;
}
@XmlElement
public void setDate(Date date) {
public void setDate(String date) {
this.date = date;
}
public String getAuthor() {

View File

@ -1,49 +0,0 @@
package com.baeldung.xml.model;
public class Tutorial {
private String tutId;
private String type;
private String title;
private String description;
private String date;
private String author;
public String getTutId() {
return tutId;
}
public void setTutId(String tutId) {
this.tutId = tutId;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}

View File

@ -1,22 +1,20 @@
package com.baeldung.xml;
import static org.junit.Assert.*;
import java.io.File;
import org.junit.Test;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.File;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
public class DefaultParserTest {
private final String fileName = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
private final String fileNameSpace = "src/test/resources/example_namespace.xml";
final String fileNameSpace = "src/test/resources/example_namespace.xml";
private DefaultParser parser;
DefaultParser parser;
@Test
public void getFirstLevelNodeListTest() {
@ -32,7 +30,7 @@ public class DefaultParserTest {
parser = new DefaultParser(new File(fileName));
NodeList list = parser.getNodeListByTitle("XML");
for (int i = 0; i < list.getLength(); i++) {
for (int i = 0; null != list && i < list.getLength(); i++) {
Node nod = list.item(i);
assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent());
assertEquals("02", nod.getAttributes().getNamedItem("tutId").getTextContent());
@ -58,7 +56,7 @@ public class DefaultParserTest {
public void getNodeListByDateTest(){
parser = new DefaultParser(new File(fileName));
NodeList list = parser.getNodeListByTitle("04022016");
for (int i = 0; i < list.getLength(); i++) {
for (int i = 0; null != list && i < list.getLength(); i++) {
Node nod = list.item(i);
assertEquals("java", nod.getAttributes().getNamedItem("type").getTextContent());
assertEquals("04", nod.getAttributes().getNamedItem("tutId").getTextContent());

View File

@ -1,25 +1,25 @@
package com.baeldung.xml;
import org.dom4j.Element;
import org.dom4j.Node;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import org.dom4j.Element;
import org.dom4j.Node;
import org.junit.Test;
public class Dom4JParserTest {
private static final String FILE_NAME = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
private Dom4JParser parser;
Dom4JParser parser;
@Test
public void getRootElementTest() {
parser = new Dom4JParser(new File(FILE_NAME));
parser = new Dom4JParser(new File(fileName));
Element root = parser.getRootElement();
assertNotNull(root);
@ -28,7 +28,7 @@ public class Dom4JParserTest {
@Test
public void getFirstElementListTest() {
parser = new Dom4JParser(new File(FILE_NAME));
parser = new Dom4JParser(new File(fileName));
List<Element> firstList = parser.getFirstElementList();
assertNotNull(firstList);
@ -38,7 +38,7 @@ public class Dom4JParserTest {
@Test
public void getElementByIdTest() {
parser = new Dom4JParser(new File(FILE_NAME));
parser = new Dom4JParser(new File(fileName));
Node element = parser.getNodeById("03");
String type = element.valueOf("@type");
@ -47,7 +47,7 @@ public class Dom4JParserTest {
@Test
public void getElementsListByTitleTest() {
parser = new Dom4JParser(new File(FILE_NAME));
parser = new Dom4JParser(new File(fileName));
Node element = parser.getElementsListByTitle("XML");
assertEquals("java", element.valueOf("@type"));
@ -58,7 +58,7 @@ public class Dom4JParserTest {
@Test
public void generateModifiedDocumentTest() {
parser = new Dom4JParser(new File(FILE_NAME));
parser = new Dom4JParser(new File(fileName));
parser.generateModifiedDocument();
File generatedFile = new File("src/test/resources/example_updated.xml");
@ -73,7 +73,7 @@ public class Dom4JParserTest {
@Test
public void generateNewDocumentTest() {
parser = new Dom4JParser(new File(FILE_NAME));
parser = new Dom4JParser(new File(fileName));
parser.generateNewDocument();
File newFile = new File("src/test/resources/example_new.xml");

View File

@ -1,20 +1,20 @@
package com.baeldung.xml;
import org.jdom2.Element;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import org.jdom2.Element;
import org.junit.Test;
public class JDomParserTest {
private final String fileName = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
private JDomParser parser;
JDomParser parser;
@Test
public void getFirstElementListTest() {

View File

@ -1,19 +1,20 @@
package com.baeldung.xml;
import com.baeldung.xml.binding.Tutorials;
import org.junit.Test;
import java.io.File;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import org.junit.Test;
import com.baeldung.xml.binding.Tutorials;
public class JaxbParserTest {
private final String fileName = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
private JaxbParser parser;
JaxbParser parser;
@Test
public void getFullDocumentTest(){

View File

@ -1,18 +1,18 @@
package com.baeldung.xml;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import org.junit.Test;
public class JaxenDemoTest {
private final String fileName = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
private JaxenDemo jaxenDemo;
JaxenDemo jaxenDemo;
@Test
public void getFirstLevelNodeListTest() {

View File

@ -1,19 +1,20 @@
package com.baeldung.xml;
import com.baeldung.xml.model.Tutorial;
import org.junit.Test;
import java.io.File;
import java.util.List;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import org.junit.Test;
import com.baeldung.xml.binding.Tutorial;
public class StaxParserTest {
private final String fileName = "src/test/resources/example.xml";
final String fileName = "src/test/resources/example.xml";
private StaxParser parser;
StaxParser parser;
@Test
public void getAllTutorialsTest(){