| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,12 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <classpath> | ||
| 3 | + <classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/> | ||
| 4 | + <classpathentry kind="src" path="src/main/java" including="**/*.java"/> | ||
| 5 | + <classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/> | ||
| 6 | + <classpathentry kind="output" path="target/classes"/> | ||
| 7 | + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
| 8 | + <classpathentry kind="var" path="M2_REPO/org/codehaus/castor/castor/1.2/castor-1.2.jar"/> | ||
| 9 | + <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/> | ||
| 10 | + <classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.8.1/xercesImpl-2.8.1.jar"/> | ||
| 11 | + <classpathentry kind="var" path="M2_REPO/xml-apis/xml-apis/1.3.03/xml-apis-1.3.03.jar"/> | ||
| 12 | + </classpath> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <article> | ||
| 3 | + <categories xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Java</categories> | ||
| 4 | + <tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Java</tags> | ||
| 5 | + <tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Castor</tags> | ||
| 6 | + <tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">XML</tags> | ||
| 7 | + <tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Marshalling</tags> | ||
| 8 | + <tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:java.lang.String">Unmarshalling</tags> | ||
| 9 | + <url>http://hmkcode.com/castor-java-object-xml</url> | ||
| 10 | + <title>Castor - Java Object to XML & XML to Object Mapping</title> | ||
| 11 | + </article> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + <?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | + <article title="Castor - Java Object to XML & XML to Object Mapping"> | ||
| 3 | + <url>http://hmkcode.com/castor-java-object-xml</url> | ||
| 4 | + <category>Java</category> | ||
| 5 | + <tag>Java</tag> | ||
| 6 | + <tag>Castor</tag> | ||
| 7 | + <tag>XML</tag> | ||
| 8 | + <tag>Marshalling</tag> | ||
| 9 | + <tag>Unmarshalling</tag> | ||
| 10 | + </article> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + <?xml version="1.0"?><!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Mapping DTD Version 1.0//EN" | ||
| 2 | + "http://castor.org/mapping.dtd"> | ||
| 3 | + <mapping> | ||
| 4 | + <class name="com.hmkcode.vo.Article"> | ||
| 5 | + <map-to xml="article" /> | ||
| 6 | + <field name="title" type="string"> | ||
| 7 | + <bind-xml name="title" node="attribute"/> | ||
| 8 | + </field> | ||
| 9 | + <field name="url" type="string"> | ||
| 10 | + <bind-xml name="url" node="element"/> | ||
| 11 | + </field> | ||
| 12 | + <field name="categories" type="string" collection="arraylist"> | ||
| 13 | + <bind-xml name="category" /> | ||
| 14 | + </field> | ||
| 15 | + <field name="tags" type="string" collection="arraylist"> | ||
| 16 | + <bind-xml name="tag" /> | ||
| 17 | + </field> | ||
| 18 | + </class> | ||
| 19 | + </mapping> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,31 @@ | |||
| 1 | + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 2 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 3 | + <modelVersion>4.0.0</modelVersion> | ||
| 4 | + | ||
| 5 | + <groupId>com.hmkcode</groupId> | ||
| 6 | + <artifactId>castor-xml-object</artifactId> | ||
| 7 | + <version>1.0-SNAPSHOT</version> | ||
| 8 | + <packaging>jar</packaging> | ||
| 9 | + | ||
| 10 | + <name>castor-xml-object</name> | ||
| 11 | + <url>http://maven.apache.org</url> | ||
| 12 | + | ||
| 13 | + <properties> | ||
| 14 | + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| 15 | + </properties> | ||
| 16 | + | ||
| 17 | + <dependencies> | ||
| 18 | + | ||
| 19 | + <dependency> | ||
| 20 | + <groupId>org.codehaus.castor</groupId> | ||
| 21 | + <artifactId>castor</artifactId> | ||
| 22 | + <version>1.2</version> | ||
| 23 | + </dependency> | ||
| 24 | + <dependency> | ||
| 25 | + <groupId>xerces</groupId> | ||
| 26 | + <artifactId>xercesImpl</artifactId> | ||
| 27 | + <version>2.8.1</version> | ||
| 28 | + </dependency> | ||
| 29 | + | ||
| 30 | + </dependencies> | ||
| 31 | + </project> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,59 @@ | |||
| 1 | + package com.hmkcode; | ||
| 2 | + | ||
| 3 | + import java.io.FileReader; | ||
| 4 | + import java.io.FileWriter; | ||
| 5 | + import java.io.IOException; | ||
| 6 | + import java.util.LinkedList; | ||
| 7 | + | ||
| 8 | + import org.exolab.castor.xml.MarshalException; | ||
| 9 | + import org.exolab.castor.xml.Marshaller; | ||
| 10 | + import org.exolab.castor.xml.Unmarshaller; | ||
| 11 | + import org.exolab.castor.xml.ValidationException; | ||
| 12 | + | ||
| 13 | + import com.hmkcode.vo.Article; | ||
| 14 | + | ||
| 15 | + public class App | ||
| 16 | + { | ||
| 17 | + public static void main( String[] args ) | ||
| 18 | + { | ||
| 19 | + | ||
| 20 | + try { | ||
| 21 | + | ||
| 22 | + //( 1 ) OBJECT --> XML | ||
| 23 | + FileWriter writer = new FileWriter("article.xml"); | ||
| 24 | + Marshaller.marshal(createArticle(), writer); | ||
| 25 | + writer.close(); | ||
| 26 | + | ||
| 27 | + | ||
| 28 | + //( 2 ) XML --> OBJECT | ||
| 29 | + FileReader reader = new FileReader("article.xml"); | ||
| 30 | + Article article = (Article) Unmarshaller.unmarshal(Article.class, reader); | ||
| 31 | + | ||
| 32 | + System.out.println(article); | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + } catch (IOException e) { | ||
| 36 | + e.printStackTrace(); | ||
| 37 | + } catch (MarshalException e) { | ||
| 38 | + e.printStackTrace(); | ||
| 39 | + } catch (ValidationException e) { | ||
| 40 | + e.printStackTrace(); | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + public static Article createArticle(){ | ||
| 46 | + Article article = new Article(); | ||
| 47 | + | ||
| 48 | + article.setTitle("Castor - Java Object to XML & XML to Object Mapping"); | ||
| 49 | + article.setUrl("http://hmkcode.com/castor-java-object-xml"); | ||
| 50 | + article.addCategory("Java"); | ||
| 51 | + article.addTag("Java"); | ||
| 52 | + article.addTag("Castor"); | ||
| 53 | + article.addTag("XML"); | ||
| 54 | + article.addTag("Marshalling"); | ||
| 55 | + article.addTag("Unmarshalling"); | ||
| 56 | + | ||
| 57 | + return article; | ||
| 58 | + } | ||
| 59 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,89 @@ | |||
| 1 | + package com.hmkcode; | ||
| 2 | + | ||
| 3 | + import java.io.FileReader; | ||
| 4 | + import java.io.FileWriter; | ||
| 5 | + import java.io.IOException; | ||
| 6 | + import java.io.Reader; | ||
| 7 | + import java.io.Writer; | ||
| 8 | + | ||
| 9 | + import org.exolab.castor.mapping.Mapping; | ||
| 10 | + import org.exolab.castor.mapping.MappingException; | ||
| 11 | + import org.exolab.castor.xml.MarshalException; | ||
| 12 | + import org.exolab.castor.xml.Marshaller; | ||
| 13 | + import org.exolab.castor.xml.Unmarshaller; | ||
| 14 | + import org.exolab.castor.xml.ValidationException; | ||
| 15 | + import org.exolab.castor.xml.XMLContext; | ||
| 16 | + | ||
| 17 | + import com.hmkcode.vo.Article; | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + public class AppMapping | ||
| 21 | + { | ||
| 22 | + public static void main( String[] args ) | ||
| 23 | + { | ||
| 24 | + | ||
| 25 | + try { | ||
| 26 | + //Load Mapping | ||
| 27 | + Mapping mapping = new Mapping(); | ||
| 28 | + mapping.loadMapping("mapping.xml"); | ||
| 29 | + XMLContext context = new XMLContext(); | ||
| 30 | + context.addMapping(mapping); | ||
| 31 | + | ||
| 32 | + //( 1 ) OBJECT --> XML | ||
| 33 | + | ||
| 34 | + //1.1 Prepare file writer | ||
| 35 | + Writer writer = new FileWriter("mapped_article.xml"); | ||
| 36 | + | ||
| 37 | + //1.2 Create a new Marshaller | ||
| 38 | + Marshaller marshaller = context.createMarshaller(); | ||
| 39 | + marshaller.setWriter(writer); | ||
| 40 | + | ||
| 41 | + //1.3 Marshal "map" to xml | ||
| 42 | + marshaller.marshal(createArticle()); | ||
| 43 | + | ||
| 44 | + //1.4 | ||
| 45 | + writer.close(); | ||
| 46 | + | ||
| 47 | + | ||
| 48 | + //( 2 ) XML --> OBJECT | ||
| 49 | + | ||
| 50 | + //2.1 Prepare file writer | ||
| 51 | + Reader reader = new FileReader("mapped_article.xml"); | ||
| 52 | + | ||
| 53 | + //2.2 Create a new Unmarshaller | ||
| 54 | + Unmarshaller unmarshaller = context.createUnmarshaller(); | ||
| 55 | + unmarshaller.setClass(Article.class); | ||
| 56 | + | ||
| 57 | + //2.3 Unmarshal "map" to Object | ||
| 58 | + Article article = (Article) unmarshaller.unmarshal(reader); | ||
| 59 | + | ||
| 60 | + //2.4 | ||
| 61 | + reader.close(); | ||
| 62 | + | ||
| 63 | + System.out.println(article); | ||
| 64 | + | ||
| 65 | + } catch (IOException e1) { | ||
| 66 | + e1.printStackTrace(); | ||
| 67 | + } catch (MappingException e1) { | ||
| 68 | + e1.printStackTrace(); | ||
| 69 | + } catch (MarshalException e) { | ||
| 70 | + e.printStackTrace(); | ||
| 71 | + } catch (ValidationException e) { | ||
| 72 | + e.printStackTrace(); | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + public static Article createArticle(){ | ||
| 76 | + Article article = new Article(); | ||
| 77 | + | ||
| 78 | + article.setTitle("Castor - Java Object to XML & XML to Object Mapping"); | ||
| 79 | + article.setUrl("http://hmkcode.com/castor-java-object-xml"); | ||
| 80 | + article.addCategory("Java"); | ||
| 81 | + article.addTag("Java"); | ||
| 82 | + article.addTag("Castor"); | ||
| 83 | + article.addTag("XML"); | ||
| 84 | + article.addTag("Marshalling"); | ||
| 85 | + article.addTag("Unmarshalling"); | ||
| 86 | + | ||
| 87 | + return article; | ||
| 88 | + } | ||
| 89 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,57 @@ | |||
| 1 | + package com.hmkcode.vo; | ||
| 2 | + | ||
| 3 | + import java.util.LinkedList; | ||
| 4 | + import java.util.List; | ||
| 5 | + | ||
| 6 | + public class Article { | ||
| 7 | + | ||
| 8 | + private String title; | ||
| 9 | + private String url; | ||
| 10 | + private List<String> categories; | ||
| 11 | + private List<String> tags; | ||
| 12 | + | ||
| 13 | + public String getTitle() { | ||
| 14 | + return title; | ||
| 15 | + } | ||
| 16 | + public void setTitle(String title) { | ||
| 17 | + this.title = title; | ||
| 18 | + } | ||
| 19 | + public String getUrl() { | ||
| 20 | + return url; | ||
| 21 | + } | ||
| 22 | + public void setUrl(String url) { | ||
| 23 | + this.url = url; | ||
| 24 | + } | ||
| 25 | + public List<String> getCategories() { | ||
| 26 | + return categories; | ||
| 27 | + } | ||
| 28 | + public void setCategories(List<String> categories) { | ||
| 29 | + this.categories = categories; | ||
| 30 | + } | ||
| 31 | + public List<String> getTags() { | ||
| 32 | + return tags; | ||
| 33 | + } | ||
| 34 | + public void setTags(List<String> tags) { | ||
| 35 | + this.tags = tags; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public void addCategory(String category){ | ||
| 39 | + if(this.categories == null) | ||
| 40 | + this.categories = new LinkedList<String>(); | ||
| 41 | + this.categories.add(category); | ||
| 42 | + } | ||
| 43 | + public void addTag(String tag){ | ||
| 44 | + if(this.tags == null) | ||
| 45 | + this.tags = new LinkedList<String>(); | ||
| 46 | + | ||
| 47 | + this.tags.add(tag); | ||
| 48 | + } | ||
| 49 | + @Override | ||
| 50 | + public String toString() { | ||
| 51 | + return "Article [title=" + title + ", url=" + url + ", categories=" | ||
| 52 | + + categories + ", tags=" + tags + "]"; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments