Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "XDI4j Tutorial 2"
Line 5: | Line 5: | ||
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. | 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 / | + | == Serialization == |
+ | |||
+ | This example app shows how to output a graph in different serialization formats. | ||
+ | |||
+ | <pre> | ||
+ | /******************************************************************************* | ||
+ | * Copyright (c) 2008 Parity Communications, Inc. | ||
+ | * All rights reserved. This program and the accompanying materials | ||
+ | * are made available under the terms of the Eclipse Public License v1.0 | ||
+ | * which accompanies this distribution, and is available at | ||
+ | * http://www.eclipse.org/legal/epl-v10.html | ||
+ | * | ||
+ | * Contributors: | ||
+ | * Markus Sabadello - Initial API and implementation | ||
+ | *******************************************************************************/ | ||
+ | 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. | ||
+ | * | ||
+ | * @author msabadello at parityinc dot not | ||
+ | * | ||
+ | */ | ||
+ | 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("=markus"), new XRI3Segment("+name"), "Markus"); | ||
+ | graph.createStatement(new XRI3Segment("=markus"), new XRI3Segment("+friend"), new XRI3Segment("=giovanni")); | ||
+ | |||
+ | // 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(); | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | == Serialization == | ||
This example app shows how to output a graph in different serialization formats. | This example app shows how to output a graph in different serialization formats. |
Revision as of 07:56, 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
This example app shows how to output a graph in different serialization formats.
/******************************************************************************* * Copyright (c) 2008 Parity Communications, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Markus Sabadello - Initial API and implementation *******************************************************************************/ 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. * * @author msabadello at parityinc dot not * */ 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("=markus"), new XRI3Segment("+name"), "Markus"); graph.createStatement(new XRI3Segment("=markus"), new XRI3Segment("+friend"), new XRI3Segment("=giovanni")); // 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(); } }
Serialization
This example app shows how to output a graph in different serialization formats.
/******************************************************************************* * Copyright (c) 2008 Parity Communications, Inc. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Markus Sabadello - Initial API and implementation *******************************************************************************/ 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. * * @author msabadello at parityinc dot not * */ 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("=markus"), new XRI3Segment("+name"), "Markus"); graph.createStatement(new XRI3Segment("=markus"), new XRI3Segment("+friend"), new XRI3Segment("=giovanni")); // 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(); } }