import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class TestXML
{
static List<Boolean> result1 = new ArrayList<Boolean>();
/**
* 这个方法获取给定Node下的Text节点,如果不存在Text节点则返回null。 注意:是直接子节点,相差2层或2层以上不会被考虑。
*
* @param node
* a Node 一个Node。
* @return a Text 如果给定节点存在Text子节点,则返回第一个访问到的Text子节点,如果不存在则返回null。
*/
public static Text getTextNode(Node node)
{
if (node == null)
{
return null;
}
if (node.hasChildNodes())
{
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++)
{
if (list.item(i).getNodeType() == Node.TEXT_NODE)
{
return (Text) list.item(i);
}
}
}
return null;
}
public static String getNodeValue(Node node)
{
if (node == null)
{
return null;
}
Text text = getTextNode(node);
if (text != null)
{
return text.getNodeValue();
}
return null;
}
public static boolean checkExist(Node root, String nodeName)
{
// 检查XML中是否存在
if (root.hasChildNodes())
{
List<Boolean> r1 = new ArrayList<Boolean>();
NodeList nodeList = root.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++)
{
boolean ret = checkExist(nodeList.item(i), nodeName);
if (ret)
{
return true;
}
}
return false;
}
else
{
if (root.getTextContent().equals(nodeName))
{
return true;
}
else
{
return false;
}
}
}
/**
* 1、获取原XML dom元素根节点 2、保存当前循环节点的上节点名 3、
*/
public static void main(String[] args)
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
List<String> nodeNameList1 = new ArrayList<String>();
nodeNameList1.add("M1");
nodeNameList1.add("M2");
nodeNameList1.add("M3");
nodeNameList1.add("M4");
nodeNameList1.add("M5");
try
{
DocumentBuilder db = dbf.newDocumentBuilder();
// 读入xml文档
String path = "D:\\test.xml";
Document document;
document = db.parse(new File(path));
Node root = document.getDocumentElement();
for (int c = 0; c < nodeNameList1.size(); c++)
{
String currNodeName = nodeNameList1.get(c);
System.out.println("currNodeName:" + currNodeName);
boolean isExist = checkExist(root, currNodeName);
System.out.println(isExist);
// 如果不存在需要新增一节点
if (!isExist)
{
// 获取所有desc节点,循环比较。
NodeList nodeList = document.getElementsByTagName("desc");
for (int i = 0; i < nodeList.getLength(); i++)
{
Node currNode = nodeList.item(i);
String nodeName = currNode.getNodeName();
String nodeValue = currNode.getTextContent();
System.out.println("nodeName:" + nodeName);
System.out.println("nodeValue:" + nodeValue);
System.out.println("nodeNameList1.get(c - 1):" + nodeNameList1.get(c - 1));
// 取前一节点的元素
if (nodeValue.equals(nodeNameList1.get(c - 1)))
{
Element newnode0 = (Element) document.createElement("bill_1");
Element newnode1 = (Element) document.createElement("desc");
newnode1.appendChild(document.createTextNode("xxxx Fee"));
Element newnode2 = (Element) document.createElement("amount");
newnode2.appendChild(document.createTextNode("0.00"));
newnode0.appendChild(newnode1);
newnode0.appendChild(newnode2);
root.insertBefore(newnode0, currNode);
break;
}
}
}
}
// 利用transformer对象将修改后的文档重新输出
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer;
transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
// 将xml文档保存
StreamResult result = new StreamResult(new java.io.File("D:\\test.xml"));
transformer.transform(source, result);
}
catch (TransformerConfigurationException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}