|
Short Manual 1. Creating a binary XSD file using Ant: <taskdef name="lxbParser" classname="com.radialfield.lxb.parser.LXBParserTask"> <classpath refid="[The classpath]" /> </taskdef> The classpath needs to point to the following jar files:
Call the new task: <lxbParser outputdir="some/output/dir"> <schemaFile location="my.xsd" /> <schemaFile location="your.xsd" /> <schemaFile location="our.xsd" /> </lxbParser> Several schema files can be used here, for each schema, the binary format file will be generated with the default For the above example, 3 files will be created: 2. Marshalling an XML document // The schema resource name is relative to the classpath String schemaResource = "my.xsd.ser"; // Create a BoundedDocument using the schema resource BoundedDocument doc = BoundedDocumentFactory.newDocument(schemaResource); // Add a root element BoundedElement root = doc.addRootElement("root"); // Add some children BoundedElement e1 = root.addChild("e1") BoundedElement e11 = e1.addChild("e11"); BoundedElement e111 = e11.addChild("e111"); // Can also use the short version root.addChild("e1").addChild("e11").addChild("e111"); // Add some members (attributes or simple elements) root.addMember("att1","val1"); root.addMember("att2","val2"); root.addMember("e2","e2val"); e11.addMember("e112","e112val"); e111.addMember("att3","val3"); // Or use the short version root.addMember("att1","val1").addMember("att2","val2").addMember("e2","e2val"); // create the XML document File output = new File("my.xml"); doc.marshall(output); This code will produce the following XML file: <root att1="val1" att2="val2"> <e1> <e11> <e111 att3="val3"/> <e112>e112val</e112> </e11> </e1> <e2>e2val</e2> </root> 3. Unmarshalling an XML document
// A BoundedDocument is created String schemaResource = "my.xsd.ser"; BoundedDocument doc = BoundedDocumentFactory.newDocument(schemaResource); // This time we unmarshall (read) an existing XML document File input = new File("my.xml"); doc.unmarshall(input); // Now browse the content BoundedElement root = doc.getRootElement(); List<BoundedElement> e1s = root.getChildren("e1"); for (BoundedElement e1 : e1s) { String att3 = e1.getFirstChild("e11").getFirstChild("e111").getMemberValue("att3"); } String e2 = root.getMemberValue("e2"); More usage examples can be found in the sample that comes with the project's release. |
LXB4J is hosted on sourceforge.net |