Wiki
Clone wikiXLIFF Toolkit / Creating
Creating a Document
Here is an example of creating a document using the XLIFFWriter class:
#!java try ( XLIFFWriter writer = new XLIFFWriter() ) { // Create a new document (with English as the source language) writer.create(new File("myDocument1.xlf"), "en"); // Create a unit Unit unit = new Unit("u1"); // Add a segment and get the source content Fragment content = unit.appendSegment().getSource(); content.append("Hello "); content.openCodeSpan("1", "<B>"); content.append("World"); content.closeCodeSpan("1", "</B>"); content.append("!"); // Write the unit // Enclosing elements are created with defaults if you don't write // them explicitly writer.writeUnit(unit); // If you forget to write the ends of elements, close() will do it. // And if you use a try-with-resources like here it will call close() for you. }
A few things to remember:
- Use try-with-resources or do not forget to call
close()
to avoid resource leaks. - Do not forget to specify a target language if you have any target content.
The code above produces the following XLIFF document:
#!java <xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="en"> <file id="f1"> <unit id="u1"> <originalData> <data id="d1"><B></data> <data id="d2"></B></data> </originalData> <segment> <source>Hello <pc id="1" dataRefEnd="d2" dataRefStart="d1">World</pc>!</source> </segment> </unit> </file> </xliff>
Once your document is created you can easily read it back, update it and write it back.
Updated