解析xml格式的string内容并修改节点内容

前端之家收集整理的这篇文章主要介绍了解析xml格式的string内容并修改节点内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如下解析方式使用了:org.xml.sax.InputSource ;org.w3c.dom.Document ;org.w3c.dom.Element;

缺点:DOM API的缺点之一是消耗大量的内存,因为在该XML文档可以被导航之前,必须创建一个完整的XML文档的内存结构。而SAX API的缺点在于,它实例了一种推分析模型API,其中分析事件是由分析器生成的。比较之下,StAX则是基于一种拉分析模型。

cdaDocument : 是得到的string格式的xml内容


						StringReader str = new StringReader(cdaDocument);
						InputSource is = new InputSource(str);
						DocumentBuilderFactory factory = DocumentBuilderFactory
								.newInstance();
						DocumentBuilder builder;
						String fileName_pdf = hrid + diid + heventcode + ".pdf";
						builder = factory.newDocumentBuilder();
						Document document = builder.parse(is);
						Element rootElement = document.getDocumentElement();
						// start
						NodeList list1 = rootElement
								.getElementsByTagName("text");
						for (int i = 0; i < list1.getLength(); i++) {
							Element element = (Element) list1.item(i);
							String mediaType = element
									.getAttribute("mediaType").toString()
									.trim();
							if (mediaType != null && !"".equals(mediaType)) {
								if ("application/pdf".equals(mediaType)) {
									NodeList list = rootElement
											.getElementsByTagName("reference");
									Element elementRef = (Element) list.item(i);
									String brandName1 = elementRef
											.getAttribute("value").toString()
											.trim();
									BASE64Decoder base64Decoder = new BASE64Decoder();
									byte[] data = base64Decoder
											.decodeBuffer(brandName1);
									byte[] Buffer = new byte[1024];
									int size = 0;
									try {
										String directory_pdf = getWebRoot()
												.substring(1).replaceAll("//",File.separator)
												+ "cdapdf"
												+ File.separator
												+ connectIp + File.separator;
										BufferedInputStream bis = new BufferedInputStream(
												new ByteArrayInputStream(data));
										// FileOutputStream fos1 = new
										// FileOutputStream
										// ("D:\\copyjava11.pdf") ;
										FileOutputStream fos1 = new FileOutputStream(
												directory_pdf + fileName_pdf);
										BufferedOutputStream bos1 = new BufferedOutputStream(
												fos1,1024);

										while ((size = bis.read(Buffer)) != -1) {
											bos1.write(Buffer,size);
										}
										bos1.flush();
										bos1.close();
										fos1.close();
										bis.close();
									} catch (Exception e) {
										log.debug("存储PDF流操作失败:"
												+ e.getMessage());
									}
									// 成功end
									// 修改属性值
									elementRef.setAttribute("value","cdapdf\\"
											+ connectIp + "\\" + fileName_pdf);
								} else {
									NodeList list = rootElement
											.getElementsByTagName("reference");
									Element elementRef = (Element) list.item(i);
									// 修改属性值
									elementRef.setAttribute("value","");
								}
							}

						}
						TransformerFactory transformerFactory = TransformerFactory
								.newInstance();
						Transformer transformer = transformerFactory
								.newTransformer();
						DOMSource domSource = new DOMSource(document);
						ByteArrayOutputStream baos = new ByteArrayOutputStream();
						// 设置编码类型
						transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
						transformer
								.transform(domSource,new StreamResult(baos));
						cdaDocument = baos.toString();
					

猜你在找的XML相关文章