zoeban的gravatar头像
zoeban 2016-07-26 22:05:22

jaxb操作xml文件(自定义xml namespace 前缀,可以有多个namespace)

最近在用jaxb 操作xml文件,所以我把代码整理了哈,里面实现的内容有:

1.  用jaxb把pojo对象转换成xml格式的字符串;

1.1. 生成的pojo对象在pojo文件夹里面(这个xml文件支持自定义namespace的前缀,可以有多个namespace),主要的两个文件是

ObjectFactor,package-info.java

1.2. jaxb把pojo对象转换成xml的代码片段:

public static String marshal(Object pObj, String pEncoding) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(pObj.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, pEncoding);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
    //the propertyName(com.sun.xml.internal.bind.namespacePrefixMapper) may be different, it depends on your marshaller implement class
    marshaller.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
        @Override
        public String getPreferredPrefix(String pNamespaceUri, String pPerfix, boolean pB) {
            if (NameSpace.NAMESPACE_CEB_URI.equals(pNamespaceUri)) {
                return NameSpace.NAMESPACE_CEB_PREFIX;
            } else if (NameSpace.NAMESPACE_XSI_URI.equals(pNamespaceUri)) {
                return NameSpace.NAMESPACE_XSI_PREFIX;
            }
            return pPerfix;
        }
    });
    StringWriter stringWriter = null;
    try {
        stringWriter = new StringWriter();
        marshaller.marshal(pObj, stringWriter);
        String xmlContent = stringWriter.toString();
        return xmlContent;
    } finally {
        if (stringWriter != null) {
            stringWriter.flush();
            stringWriter.close();
        }
    }
}

2.  根据xml格式的字符串生成新的xml文件;

public static void generateXmlFile(String pXmlContent, File pFile, String pEncoding) throws Exception {
    if (!pFile.exists()) {
        pFile.createNewFile();
    }
    FileOutputStream fos = null;
    OutputStreamWriter osw = null;
    try {
        fos = new FileOutputStream(pFile);
        osw = new OutputStreamWriter(fos, pEncoding);
        osw.write(pXmlContent);
    } finally {
        if (osw != null) {
            osw.flush();
            osw.close();
        }
        if (fos != null) {
            fos.flush();
            fos.close();
        }
    }
}

3.  复制xml文件,并且能够保持原来的xml文件格式跟新生成的xml文件格式一致(思路是先把xml文件读取成xml格式的字符串,然后再根据该字符串生成新的xml文件);

public static boolean copyXmlFile(File pSourceFile, File pDestFile, String pEncoding) throws Exception {
    if (!pSourceFile.exists()) {
        throw new Exception("the sourceFile " + pSourceFile + " does not exist!");
    }
    if (!pDestFile.exists()) {
        pDestFile.createNewFile();
    }
    String fileContent = readXmlFile(pSourceFile, pEncoding);
    generateXmlFile(fileContent, pDestFile, pEncoding);
    return true;
}

public static String readXmlFile(File pFile, String pEncoding) throws Exception {
    InputStream is = null;
    BufferedReader br = null;
    try {
        is = new FileInputStream(pFile);
        br = new BufferedReader(new InputStreamReader(is, pEncoding));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append(System.getProperty("line.separator"));
        }
        return sb.toString();
    } finally {
        if (br != null) {
            br.close();
        }
        if (is != null) {
            is.close();
        }
    }
}

4.  测试的jdk版本1.7,详细的代码请下载压缩包进行查看,本地已测试过


打赏

文件名:jaxb.zip,文件大小:9.48K 下载
最代码最近下载分享源代码列表最近下载
caoshx  LV1 2021年8月3日
张洪  LV1 2021年7月1日
596350754  LV1 2021年6月26日
zyd200888  LV2 2020年5月22日
cssktk  LV1 2019年9月3日
llyeean  LV2 2019年4月22日
seacosmos  LV1 2018年12月27日
wangpeng  LV4 2018年10月10日
放开那女孩  LV15 2017年9月19日
fengzhong5485  LV6 2017年7月5日
最代码最近浏览分享源代码列表最近浏览
1443251642  LV1 2022年12月19日
wado001  LV1 2021年10月9日
szh922  LV1 2021年7月5日
张洪  LV1 2021年7月1日
596350754  LV1 2021年6月26日
15939671505 2020年12月18日
暂无贡献等级
liuguojun920  LV6 2020年9月9日
1234567891011  LV5 2020年6月26日
zyd200888  LV2 2020年5月22日
13543318166  LV1 2020年3月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友