Jdom处理xml例子-全角数据转化为半角数据等等

来源:互联网 发布:域名个人能备案么 编辑:程序博客网 时间:2024/04/29 22:42
import java.io.FileOutputStream;import java.io.IOException;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.JDOMException;import org.jdom.input.SAXBuilder;import org.jdom.output.XMLOutputter;     public class JdomForXML { /**  *   * @param inputfile 需要处理的xml文件名  * @param outputfile 经过处理以后的xml文件名  * @param type 处理xml文件的方式  * @throws IOException  * @throws JDOMException  */public void readXML(String inputfile,String outputfile,String type)  throws IOException, JDOMException{        SAXBuilder builder = new SAXBuilder();        Document read_xml = builder.build(inputfile);        Element root = read_xml.getRootElement();                List list = root.getChildren();        System.out.println(list.size());        System.out.println("根节点是:"+root.getName());        System.out.println("—————————————");                for(int i = 0;i < list.size();i++)        {            Element e = (Element)list.get(i);             //Element tmp = root.getChild(e.getName());            //tmp.setText(e.getValue());            List listChildren = e.getChildren();            if(listChildren.isEmpty())            {            System.out.println("@@@@没有孩子节点的属性"+e.getName());            System.out.println("@@@@没有孩子节点的值"+e.getValue());            Element tmp = root.getChild(e.getName());            tmp.setText(parseStr(e.getValue(),type));            }            else            {            findChildren(e,listChildren,type);            }        }        root.detach();        Document doc=new Document(root);        XMLOutputter XMLOut = new XMLOutputter("",true,"GBK");        try{XMLOut.output(doc,new FileOutputStream(outputfile));}catch(Exception e) {e.printStackTrace();System.out.println("Write file fail!");}        }/** * 递归方法,找到孩子节点,并做处理 * @param e * @param listChildren * @param type */private void findChildren(Element e,List listChildren,String type){            for(int j = 0;j < listChildren.size();j++)            {            Element  eTmp=(Element)listChildren.get(j);                        System.out.println("属性:"+eTmp.getName());                        Element temp =e.getChild(eTmp.getName());                                 System.out.println("值:"+eTmp.getValue());            System.out.println("修改值:"+parseStr(eTmp.getValue(),type));            if(temp.getChildren().isEmpty())            {            temp.setText(parseStr(eTmp.getValue(),type));             }            else            {            findChildren(eTmp,eTmp.getChildren(),type);            }            }}/* * 替换传入字符串中的中的全角为半角 *  */private String dbc2sbc(String strInput){String strResult="";int intTemp=0;for(int i=0;i0xfee0)strResult+=(char)(strInput.codePointAt(i)-0xfee0);elsestrResult+=(char)(strInput.charAt(i));}return strResult;}    /* * 去除传入字符串前后的空格 *  */private String rmoveAllSpace(String str){String tmpstr=str.trim();    return tmpstr;} /* * 根据参数处理字符串 *  */private String parseStr(String str,String type){String strParse = null;if(type.equals("d2s")){strParse = dbc2sbc(str);}else if(type.equals("sp")){strParse = rmoveAllSpace(str);}else if(type.equals("all")){strParse = dbc2sbc(rmoveAllSpace(str));}else{System.out.println("格式化参数错误!");}return strParse;}    }   测试函数:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import org.jdom.JDOMException;/** * @author zhoulei * */public class Dbc2sbc {/** * @param args */public static void main(String[] args) {String[] param = null;//定义数组System.out.println("请输入三个参数:-i -o -m");System.out.println("-i 输入的xml文件");System.out.println("-o 输出的xml文件");System.out.println("-m 取值:d2s sp all ,其中d2s代表功能是全角转换半角,sp代表功能是去后导空格,all代表是执行所有功能");BufferedReader buf=new BufferedReader(new InputStreamReader(System.in)); while(true){try {String str=buf.readLine();param = str.split(" ");if(param.length<3){System.out.println("参数不够,请重新输入:");}else{if(param[2].equals("d2s")){break;}else if(param[2].equals("sp")){break;}else if(param[2].equals("all")){    break;}elseSystem.out.println("格式化参数不正确,请重新输入");}}catch (IOException e) {// TODO 自动生成 catch 块e.printStackTrace();}}JdomForXML jfx = new JdomForXML();try {jfx.readXML(param[0],param[1],param[2]);} catch (IOException e) {// TODO 自动生成 catch 块e.printStackTrace();System.out.println("ParseFile fail!");} catch (JDOMException e) {// TODO 自动生成 catch 块e.printStackTrace();System.out.println("ParseFile fail!");}}}
原创粉丝点击