Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "XDI4j Tutorial 2"
(9 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}} | {{#eclipseproject:technology.higgins|eclipse_custom_style.css}} | ||
[[Image:Higgins_logo_76Wx100H.jpg|right]] | [[Image:Higgins_logo_76Wx100H.jpg|right]] | ||
− | + | This tutorial explains how to serialize and deserialize an XDI graph in any of the XDI serialization formats. | |
− | + | A typical application is to read an XDI graph from a file into memory, perform a few operations on it, and then store it again to file. | |
+ | |||
+ | == Serialization == | ||
+ | |||
+ | '''Example Code:''' | ||
<pre> | <pre> | ||
Line 17: | Line 21: | ||
/** | /** | ||
* This example app shows how to output | * This example app shows how to output | ||
− | * | + | * a graph in different serialization formats. |
− | + | ||
− | + | ||
− | + | ||
*/ | */ | ||
public class Tutorial2 { | public class Tutorial2 { | ||
Line 31: | Line 32: | ||
// let's create a simple graph | // let's create a simple graph | ||
− | graph.createStatement(new XRI3Segment("= | + | graph.createStatement(new XRI3Segment("=alice"), new XRI3Segment("+name"), "Alice X."); |
− | + | graph.createStatement(new XRI3Segment("=alice"), new XRI3Segment("+friend"), new XRI3Segment("=bob")); | |
− | graph.createStatement(new XRI3Segment("= | + | |
− | // output the graph in X3 and XDI/XML | + | // output the graph in X3 Simple and XDI/XML |
XDIWriter writer; | XDIWriter writer; | ||
− | writer = XDIWriterRegistry.forFormat("X3"); | + | writer = XDIWriterRegistry.forFormat("X3 Simple"); |
writer.write(graph, System.out, null); | writer.write(graph, System.out, null); | ||
writer = XDIWriterRegistry.forFormat("XDI/XML"); | writer = XDIWriterRegistry.forFormat("XDI/XML"); | ||
+ | writer.write(graph, System.out, null); | ||
+ | |||
+ | graph.close(); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | == Deserialization == | ||
+ | |||
+ | '''Example Code:''' | ||
+ | |||
+ | <pre> | ||
+ | package org.eclipse.higgins.xdi4j.tutorial; | ||
+ | |||
+ | import java.io.File; | ||
+ | import java.io.FileInputStream; | ||
+ | |||
+ | import org.eclipse.higgins.xdi4j.Graph; | ||
+ | import org.eclipse.higgins.xdi4j.GraphFactory; | ||
+ | import org.eclipse.higgins.xdi4j.impl.memory.MemoryGraphFactory; | ||
+ | import org.eclipse.higgins.xdi4j.io.XDIReader; | ||
+ | import org.eclipse.higgins.xdi4j.io.XDIReaderRegistry; | ||
+ | import org.eclipse.higgins.xdi4j.io.XDIWriter; | ||
+ | import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry; | ||
+ | |||
+ | /** | ||
+ | * This example app shows how to read a graph in one format | ||
+ | * and store it in another. | ||
+ | */ | ||
+ | public class Tutorial2A { | ||
+ | |||
+ | public static void main(String[] args) throws Exception { | ||
+ | |||
+ | GraphFactory factory = MemoryGraphFactory.getInstance(); | ||
+ | Graph graph = factory.openGraph(); | ||
+ | |||
+ | // read a graph from file | ||
+ | |||
+ | File file = new File("test.graph"); | ||
+ | XDIReader reader = XDIReaderRegistry.getAuto(); | ||
+ | |||
+ | reader.read(graph, new FileInputStream(file), null); | ||
+ | |||
+ | // output the graph in X3 Simple | ||
+ | |||
+ | XDIWriter writer; | ||
+ | |||
+ | writer = XDIWriterRegistry.forFormat("X3 Simple"); | ||
writer.write(graph, System.out, null); | writer.write(graph, System.out, null); | ||
Latest revision as of 08:57, 29 January 2010
{{#eclipseproject:technology.higgins|eclipse_custom_style.css}}
This tutorial explains how to serialize and deserialize an XDI graph in any of the XDI serialization formats.
A typical application is to read an XDI graph from a file into memory, perform a few operations on it, and then store it again to file.
Serialization
Example Code:
package org.eclipse.higgins.xdi4j.tutorial; import org.eclipse.higgins.xdi4j.Graph; import org.eclipse.higgins.xdi4j.GraphFactory; import org.eclipse.higgins.xdi4j.impl.memory.MemoryGraphFactory; import org.eclipse.higgins.xdi4j.io.XDIWriter; import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry; import org.eclipse.higgins.xdi4j.xri3.impl.XRI3Segment; /** * This example app shows how to output * a graph in different serialization formats. */ public class Tutorial2 { public static void main(String[] args) throws Exception { GraphFactory factory = MemoryGraphFactory.getInstance(); Graph graph = factory.openGraph(); // let's create a simple graph graph.createStatement(new XRI3Segment("=alice"), new XRI3Segment("+name"), "Alice X."); graph.createStatement(new XRI3Segment("=alice"), new XRI3Segment("+friend"), new XRI3Segment("=bob")); // output the graph in X3 Simple and XDI/XML XDIWriter writer; writer = XDIWriterRegistry.forFormat("X3 Simple"); writer.write(graph, System.out, null); writer = XDIWriterRegistry.forFormat("XDI/XML"); writer.write(graph, System.out, null); graph.close(); } }
Deserialization
Example Code:
package org.eclipse.higgins.xdi4j.tutorial; import java.io.File; import java.io.FileInputStream; import org.eclipse.higgins.xdi4j.Graph; import org.eclipse.higgins.xdi4j.GraphFactory; import org.eclipse.higgins.xdi4j.impl.memory.MemoryGraphFactory; import org.eclipse.higgins.xdi4j.io.XDIReader; import org.eclipse.higgins.xdi4j.io.XDIReaderRegistry; import org.eclipse.higgins.xdi4j.io.XDIWriter; import org.eclipse.higgins.xdi4j.io.XDIWriterRegistry; /** * This example app shows how to read a graph in one format * and store it in another. */ public class Tutorial2A { public static void main(String[] args) throws Exception { GraphFactory factory = MemoryGraphFactory.getInstance(); Graph graph = factory.openGraph(); // read a graph from file File file = new File("test.graph"); XDIReader reader = XDIReaderRegistry.getAuto(); reader.read(graph, new FileInputStream(file), null); // output the graph in X3 Simple XDIWriter writer; writer = XDIWriterRegistry.forFormat("X3 Simple"); writer.write(graph, System.out, null); graph.close(); } }