Created
January 29, 2014 10:44
-
-
Save kunishi/8685534 to your computer and use it in GitHub Desktop.
AtomParser
This file contains hidden or 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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
/** | |
* | |
* @author kunishi | |
*/ | |
public class AtomFormatter { | |
public static void main(String[] args) { | |
try { | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
Document doc = builder.parse(args[0]); | |
NodeList nl = doc.getDocumentElement().getChildNodes(); | |
System.out.println("<html><head><title>AtomFormatter</title></head>"); | |
System.out.println("<body>"); | |
for (int i = 0; i < nl.getLength(); i ++) { | |
Node node = nl.item(i); | |
String nodename = node.getNodeName(); | |
if (nodename.equals("title")) { | |
System.out.println("<p><b>" + node.getTextContent() + "</b></p>"); | |
} else if (nodename.equals("updated")) { | |
System.out.println("<p>Last Updated: " + node.getTextContent() + "</p>"); | |
} else if (nodename.equals("author")) { | |
System.out.println("<p>Author: " + | |
((Element)node).getFirstChild().getTextContent() + "</p>"); | |
} else if (nodename.equals("entry")) { | |
System.out.println("<hr />"); | |
NodeList entrynl = node.getChildNodes(); | |
for (int j = 0; j < entrynl.getLength(); j ++) { | |
Node entrynode = entrynl.item(j); | |
String entryname = entrynode.getNodeName(); | |
if (entryname.equals("published")) { | |
System.out.println("<p>" + entrynode.getTextContent() + "</p>"); | |
} else if (entryname.equals("title")) { | |
System.out.println("<p><b>" + entrynode.getTextContent() + "</b></p>"); | |
} else if (entryname.equals("content")) { | |
System.out.println("<p>" + entrynode.getTextContent() + "</p>"); | |
} | |
} | |
} | |
} | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} |
This file contains hidden or 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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
/** | |
* | |
* @author kunishi | |
*/ | |
public class AtomParser { | |
public static void main(String[] args) { | |
try { | |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder builder = factory.newDocumentBuilder(); | |
Document doc = builder.parse(args[0]); | |
NodeList nl = doc.getDocumentElement().getChildNodes(); | |
for (int i = 0; i < nl.getLength(); i ++) { | |
Node node = nl.item(i); | |
String nodename = node.getNodeName(); | |
if (nodename.equals("title")) { | |
System.out.println("Blog title: " + node.getTextContent()); | |
} else if (nodename.equals("updated")) { | |
System.out.println("Last Updated: " + node.getTextContent()); | |
} else if (nodename.equals("author")) { | |
System.out.println("Author: " + | |
((Element)node).getFirstChild().getTextContent()); | |
} else if (nodename.equals("entry")) { | |
System.out.println("[entry]"); | |
NodeList entrynl = node.getChildNodes(); | |
for (int j = 0; j < entrynl.getLength(); j ++) { | |
Node entrynode = entrynl.item(j); | |
String entryname = entrynode.getNodeName(); | |
if (entryname.equals("published")) { | |
System.out.println("Published: " + entrynode.getTextContent()); | |
} else if (entryname.equals("title")) { | |
System.out.println("Title: " + entrynode.getTextContent()); | |
} else if (entryname.equals("content")) { | |
System.out.println("Content: " + entrynode.getTextContent()); | |
} | |
} | |
System.out.println(""); | |
} | |
} | |
} catch (Exception e) { | |
System.out.println(e); | |
} | |
} | |
} |
This file contains hidden or 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
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
import org.junit.After; | |
import org.junit.AfterClass; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
/** | |
* | |
* @author kunishi | |
*/ | |
public class AtomParserTest { | |
public AtomParserTest() { | |
} | |
@BeforeClass | |
public static void setUpClass() throws Exception { | |
} | |
@AfterClass | |
public static void tearDownClass() throws Exception { | |
} | |
@Before | |
public void setUp() { | |
} | |
@After | |
public void tearDown() { | |
} | |
/** | |
* Test of main method, of class AtomParser. | |
*/ | |
@Test | |
public void main() { | |
System.out.println("main"); | |
String[] args = {"test/GoogleBlogAtom.xml"}; | |
AtomParser.main(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment