/** * 读取XML */ public void readXML(String xmlFile) { if ("".equals(xmlFile)) { customXml = "NothingNeed"; setTempshow(false); } else { InputStream in = null; try { in = new ByteArrayInputStream(xmlFile.getBytes("utf-8")); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(xmlFile.getBytes("utf-8"))); NodeList nodeList = doc.getElementsByTagName("parameter"); listCustom = new ArrayList<Custom>(); for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); String value = ""; NodeList valueNode = element.getElementsByTagName("value"); for (int j = 0; j < valueNode.getLength(); j++) { if (valueNode.item(j).getFirstChild() != null) { value = valueNode.item(j).getFirstChild().getNodeValue(); break; } } String description = "服务名称"; NodeList descriptionNode = element.getElementsByTagName("description"); /** * 拿出value的取值 如果节点本身为空 不取值 * 如果他的孩子结点为空 循环至孩子不为空为止 */ for (int j = 0; j < valueNode.getLength(); j++) { if (descriptionNode.item(j) != null) { if (descriptionNode.item(j).getFirstChild() != null) { description = descriptionNode.item(j).getFirstChild().getNodeValue(); } } } String visible = element.getElementsByTagName("visible").item(0).getFirstChild().getNodeValue(); if (visible.equals("true")) { Custom cus = new Custom(); cus.setName(description); cus.setValue(value); listCustom.add(cus); } } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } } //将document转为String public String docToString(Document doc) { ByteArrayOutputStream baos = null; String str = "abc"; try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.ENCODING, "utf-8");// 处理汉字编码 baos = new ByteArrayOutputStream(); t.transform(new DOMSource(doc), new StreamResult(baos)); str = baos.toString("utf-8"); return str; } catch (Exception e) { e.printStackTrace(); } finally { try { if (baos != null) { baos.flush(); } } catch (IOException ex) { ex.printStackTrace(); } } return str; } //得到输入的自定义参数集合 public List getInputValue() { List templateValue = new ArrayList(); if (listCustom != null) { for (int i = 0; i < listCustom.size(); i++) { templateValue.add(listCustom.get(i).getName()); templateValue.add(listCustom.get(i).getValue()); } } else { tempHighLight = new Highlight(); } return templateValue; } /** * 填写xml文件 */ public String readAndWriteXml() { if ("".equals(customXml)) { //如果传过来的是空字符串 返回空字符串 return ""; } else if ("NothingNeed".equals(customXml)) { //本身没有认证参数的返回NothingNeed return "NothingNeed"; } else { List inputTemplate = getInputValue(); String bbb = customXml.replace("></value>", "> </value>"); InputStream in = null; try { in = new ByteArrayInputStream(bbb.getBytes("utf-8")); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(in); NodeList nodeList = doc.getElementsByTagName("parameter"); //一个存储可见性(visible属性)的列表 List visibleNum = new ArrayList(); //一个存储输入的值(即value值) List valueList = new ArrayList(); //一个存储输入的值(即value值) List descriptionList = new ArrayList(); //获取输入的值(即value值) for (int j = 0, k = 0; j < inputTemplate.size(); j += 2, k++) { valueList.add(inputTemplate.get(j + 1)); descriptionList.add(inputTemplate.get(j)); if ("".equals(valueList.get(k).toString())) { return "error"; } } for (int i = 0; i < nodeList.getLength(); i++) { Element element = (Element) nodeList.item(i); //获取原xml文件中的description属性值 String description = ""; NodeList descriptionNode = element.getElementsByTagName("description"); description = descriptionNode.item(0).getFirstChild().getNodeValue(); //获取原xml文件中的visible属性 String visible = ""; NodeList visibleNode = element.getElementsByTagName("visible"); visible = visibleNode.item(0).getFirstChild().getNodeValue(); visibleNum.add(visible); //获取原xml文件中的value属性 String formerlyValue = ""; NodeList formerlyValueNode = element.getElementsByTagName("value"); formerlyValue = formerlyValueNode.item(0).getFirstChild().getNodeValue(); //重新给value属性值赋值 String value = ""; int queueInput = inputTemplate.size() / 2; //长度相等说明所有节点属性都显示了 可以直接赋值 if (queueInput == nodeList.getLength()) { value = valueList.get(i).toString(); } else { //如果返回的visible为false 把结点的value直接赋给value if ("false".equals(visible)) { value = formerlyValue; } else { //为true 比较description节点属性值和页面显示的属性值 相等 即把页面输入栏的值赋给value for (int m = 0; m < queueInput; m++) { if (description.equals(descriptionList.get(m).toString())) { value = valueList.get(m).toString(); } } } } //最后刷新customXml文件数据 ((Element) doc.getElementsByTagName("parameter").item(i)).getElementsByTagName("value").item(0).getFirstChild().setNodeValue(value); } String strWrite = docToString(doc); return strWrite; } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } } return ""; }