日期:2014-04-08  浏览次数:21090 次

如果xml文件里含有中文,需求加上
<?xml version="1.0" encoding="GB2312"?>才可以解析
for instance:
 a XML file like this:
<动作>
   <插入 目标结点编号="001" 插入方式="5">
      <插入项 属性名="名称" 值="中国"/>
      <插入项 属性名="名称" 值="86"/>
   </插入>
</动作>

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

public class xml2oth{
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Element root = doc.getDocumentElement();
            NodeList allNode = root.getChildNodes();
            for (int i = 1; i < allNode.getLength(); i += 2) {//
                Element item = (Element) allNode.item(i);
                Xml2DbmsBean _Bean = new Xml2DbmsBean();
                System.out.println(item.getNodeName());
                names = item.getElementsByTagName("插入项");//设置成你本人的
            }
}