Introduction to XPath
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class DefaultParserTest {
|
||||
|
||||
final String fileName = "src/test/resources/example.xml";
|
||||
|
||||
final String fileNameSpace = "src/test/resources/example_namespace.xml";
|
||||
|
||||
DefaultParser parser;
|
||||
|
||||
@Test
|
||||
public void getFirstLevelNodeListTest() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
NodeList list = parser.getFirstLevelNodeList();
|
||||
|
||||
assertNotNull(list);
|
||||
assertTrue(list.getLength() == 4);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListByTitle() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
NodeList list = parser.getNodeListByTitle("XML");
|
||||
|
||||
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());
|
||||
assertEquals("XML", nod.getFirstChild().getTextContent());
|
||||
assertEquals("title", nod.getFirstChild().getNodeName());
|
||||
assertEquals("description", nod.getChildNodes().item(1).getNodeName());
|
||||
assertEquals("Introduction to XPath", nod.getChildNodes().item(1).getTextContent());
|
||||
assertEquals("author", nod.getLastChild().getNodeName());
|
||||
assertEquals("XMLAuthor", nod.getLastChild().getTextContent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeById() {
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
Node node = parser.getNodeById("03");
|
||||
|
||||
String type = node.getAttributes().getNamedItem("type").getNodeValue();
|
||||
assertEquals("android", type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListByDate(){
|
||||
parser = new DefaultParser(new File(fileName));
|
||||
NodeList list = parser.getNodeListByTitle("04022016");
|
||||
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());
|
||||
assertEquals("Spring", nod.getFirstChild().getTextContent());
|
||||
assertEquals("title", nod.getFirstChild().getNodeName());
|
||||
assertEquals("description", nod.getChildNodes().item(1).getNodeName());
|
||||
assertEquals("Introduction to Spring", nod.getChildNodes().item(1).getTextContent());
|
||||
assertEquals("author", nod.getLastChild().getNodeName());
|
||||
assertEquals("SpringAuthor", nod.getLastChild().getTextContent());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNodeListWithNamespace(){
|
||||
parser = new DefaultParser(new File(fileNameSpace));
|
||||
NodeList list = parser.getAllTutorials();
|
||||
assertNotNull(list);
|
||||
assertTrue(list.getLength() == 4);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0"?>
|
||||
<Tutorials>
|
||||
<Tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</Tutorial>
|
||||
</Tutorials>
|
||||
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0"?>
|
||||
<Tutorials xmlns="http://www.baeldung.com/full_archive">
|
||||
<Tutorial tutId="01" type="java">
|
||||
<title>Guava</title>
|
||||
<description>Introduction to Guava</description>
|
||||
<date>04/04/2016</date>
|
||||
<author>GuavaAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="02" type="java">
|
||||
<title>XML</title>
|
||||
<description>Introduction to XPath</description>
|
||||
<date>04/05/2016</date>
|
||||
<author>XMLAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="03" type="android">
|
||||
<title>Android</title>
|
||||
<description>Introduction to Android</description>
|
||||
<date>04/03/2016</date>
|
||||
<author>AndroidAuthor</author>
|
||||
</Tutorial>
|
||||
<Tutorial tutId="04" type="java">
|
||||
<title>Spring</title>
|
||||
<description>Introduction to Spring</description>
|
||||
<date>04/02/2016</date>
|
||||
<author>SpringAuthor</author>
|
||||
<sections>
|
||||
<section name="core">Spring Core</section>
|
||||
<section name="mvc">Spring MVC</section>
|
||||
<section name="batch">Spring Batch</section>
|
||||
</sections>
|
||||
</Tutorial>
|
||||
</Tutorials>
|
||||
Reference in New Issue
Block a user