Process modules, i.e. instances of an adapter, for example, can read and output numerous document types: XML documents, but also text or binary data. Adapters can access documents via the X4Document interface or use the X4DocumentFactory to create an output document as a result of the adapter operation. This can be an X4Document instance of a dom4j tree, a java.io.InputStream, or a byte array.
In X4 processes, when XSL transformations receive DOM-based input, character encoding problems can be avoided. For byte-based documents, the byte array is passed to the XML parser without additional information.
Methods of X4Document
|
|
||
|---|---|---|
|
public byte[] getAsByteArray() |
Returns the content as a byte array. |
|
|
public byte[] getAsByteArray(String encoding) |
Returns the content as a byte array with character encoding specified in encoding. |
|
|
public java.io.InputStream getAsInputStream() |
Returns the InputStream from accessing the document content. |
|
|
public org.dom4j.Document getAsDocument() throws org.dom4j.DocumentException |
Returns the content as a dom4J DOM tree. |
|
|
public org.dom4j.Document getAsDocument(String encoding) throws org.dom4j.DocumentException, java.io.UnsupportedEncodingException |
Returns the content as a dom4J-DOM tree with character encoding specified in encoding. |
|
|
public void parseWithHandler (ContentHandler chandler) throws SAXException, IOException |
Passes the content to the org.sax.ContentHandler instance provided (flush). |
|
|
public String getMimeType() |
Returns the MIME type of the document. |
|
|
public String getEncoding() |
Returns the character encoding of the document. |
|
|
public void setMimeType(String mimeType) |
Sets the MIME type of the document. |
|
|
public void setEncoding(String encoding) |
Sets the character encoding of the document. |
|
Methods of the X4DocumentFactory
|
|
||
|---|---|---|
|
public static X4Document createX4Document(byte[] bytes, String encoding, String mimeType) |
Returns the |
|
|
public static X4Document createDocument(Document doc, String encoding, String mimeType) |
Returns the |
|
|
public static X4Document createX4Document(InputStream is, String encoding, String mimeType) throws X4DocumentException |
Returns the |
|
Example
public class X4DocumentTest {
public static void main(String[] args) {
InputStream is = null;
try {
is = new FileInputStream("Sample.xml");
// create a new X4Document
X4Document x4doc = X4DocumentFactory.createX4Document(is,
"cp-1252", "text/xml");
// get content as a dom4j document
System.out.println(x4doc.getAsDocument().asXML() + "\n");
// get content as a byte array
System.out.println(new String(x4doc.getAsByteArray())
+ "\n");
// parse the x4document's content
SAXContentHandler chandler = new SAXContentHandler();
x4doc.parseWithHandler(chandler);
System.out.println(chandler.getDocument().asXML() + "\n");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (is != null)
try {
is.close();
} catch (IOException ioex) {
}
}
}
}