我需要使用 XsltTransformer 将给定的 xml 转换为 json,XsltTransformer 编译 xslt,然后转换 xml。我希望 XsltTransformer 的目的地是字符串或天蓝色服务总线队列。
尝试创建 BlobOutputStream 并将其传递到 XsltTransformer 目标,仍然无法在 azure 中看到 blob 中的输出。
Serializer out = processor.newSerializer();
out.setOutputProperty(Serializer.Property.METHOD, "text");
out.setOutputProperty(Serializer.Property.INDENT, "yes");
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("blob endpoint") //https://xslttesting.blob.core.windows.net/sample
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("sample");
BlobClient blobClient = blobContainerClient.getBlobClient("sample.txt");
// blobContainerClient.createIfNotExistsWithResponse();
BlobOutputStream blobOS = blobClient.getBlockBlobClient().getBlobOutputStream();
out.setOutputStream(blobOS);
XsltTransformer transformer = executable.load();
transformer.setInitialContextNode(source);
transformer.setDestination(out);
transformer.transform();
尝试创建 BlobOutputStream 并将其传递到 XsltTransformer 目标,仍然无法在 azure 中看到 blob 中的输出。
您可以使用下面的代码使用 Saxon 处理
XML
到 XSLT
转换,并使用 Azure 的 SDK 将转换后的输出写入 Azure Blob 存储。
代码:
public class App {
public static void main(String[] args) throws Exception {
Processor processor = new Processor(false);
XsltCompiler compiler = processor.newXsltCompiler();
XsltExecutable executable = compiler.compile(new StreamSource("C:\\sample.xsl"));
Source source = new StreamSource("C:\\sample.xml");
writeToBlobStorage(processor, executable, source);
}
public static void writeToBlobStorage(Processor processor, XsltExecutable executable, Source source) throws Exception {
Serializer out = processor.newSerializer();
out.setOutputProperty(Serializer.Property.METHOD, "text");
out.setOutputProperty(Serializer.Property.INDENT, "yes"); // Set up Blob storage client
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
.endpoint("blob endpoint") //https://xslttesting.blob.core.windows.net/sample
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("test");
BlobClient blobClient = blobContainerClient.getBlobClient("sample.txt");
BlobOutputStream blobOS = blobClient.getBlockBlobClient().getBlobOutputStream();
out.setOutputStream(blobOS);
XsltTransformer transformer = executable.load();
transformer.setInitialContextNode(processor.newDocumentBuilder().build(source));
transformer.setDestination(out);
transformer.transform();
blobOS.flush();
blobOS.close();
System.out.println("Transformation written to Azure Blob Storage successfully.");
}
}
在上面的代码中,
BlobOutputStream
被正确刷新并关闭,以确保所有数据都写入到blob中。您可以通过显式调用 blobOS.flush()
和 blobOS.close()
来完成此操作。
输出:
Transformation written to Azure Blob Storage successfully.
传送门: