public void testSerializeXPath() throws Exception {
Map uris = new HashMap();
uris.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
uris.put("m", "urn:xmethodsBabelFish");
DocumentFactory factory = new DocumentFactory();
factory.setXPathNamespaceURIs(uris);
// now parse a document using my factory
SAXReader reader = new SAXReader();
reader.setDocumentFactory(factory);
Document doc = getDocument("/xml/soap.xml", reader);
// now lets use the prefixes
String expr = "/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish";
Node element = doc.selectSingleNode(expr);
assertTrue("Found valid element", element != null);
XPath xpath = factory
.createXPath("/SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish");
element = xpath.selectSingleNode(doc);
assertTrue("Found valid element", element != null);
// now serialize
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytesOut);
out.writeObject(xpath);
out.close();
byte[] data = bytesOut.toByteArray();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(
data));
XPath xpath2 = (XPath) in.readObject();
in.close();
element = xpath2.selectSingleNode(doc);
assertTrue("Found valid element", element != null);
}
|