Java receives XML byte data and reads the text content of the modified XML file

zengwenhe1987 2022-05-22 13:06:24 阅读数:646

javareceivesxmlbytedata
 public String receivePdfFile(byte[] fileByte) {
if(null == fileByte || fileByte.length == 0){
return " The parameter cannot be null ";
}else {
try {
InputStream input = new ByteArrayInputStream(fileByte);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(input);
// Method 1: take xml File to String
StringWriter sw = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(doc), new StreamResult(sw));
System.out.println(sw.toString());
return sw.toString();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
}

copyright:author[zengwenhe1987],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/142/202205211911212225.html