将XML文件转化为JSON格式的文件

本文介绍了如何将XML文件转换为JSON格式,强调了JSON在JavaScript中的应用和其轻量级、简洁的特性。XML作为结构化信息的存储,转换为JSON能更好地服务于web脚本调用。文中提供了一种实现XML到JSON转化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JSON是主要应用于JavaScript的格式文件,具有轻便、简洁的特点,XML文件常常是记录各种结构式信息的载体,在实际web应用中,常常需要将XML文件的信息读取进来转化为JSON形式,方便脚本的调用。网络上已经有许多优秀的JAR助于实现这个功能,下面是一种实现方式:

首先要准备一些必须的包,如下图,
  
其核心是json-lib,里面的JSON类可以将流化的XML文件信息转化为JSON格式的信息,代码如下,实现一个窗口组件能把一个XML文件转化为JSON文件:
package com.xmltojson;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.io.IOUtils;
public class XMLToJSON extends JFrame implements ActionListener {
     private JButton choose,convert;
     private JPanel jp;
     private JTextArea jta;
     private JFileChooser jfc;
     private String xpath=null;
     private StringBuffer opath=new StringBuffer("");
     public XMLToJSON(){
    choose=new JButton("choose file");
    convert=new JButton("convert");
    choose.addActionListener(this);
    choose.setActionCommand("choose");
    convert.addActionListener(this);
    convert.setActionCommand("convert");
    jta=new JTextArea();
    jta.setText("no file");
    jta.setEnabled(false);
    jp=new JPanel();
         jp.add(choose);
         jp.add(convert);
         jp.add(jta);
         this.add(jp);
         this.setSize(400,100);
         this.setLocation(200,200);
         this.setVisible(true);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("choose")){
jfc=new JFileChooser("please choose the xml file");
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
       jfc.setFileHidingEnabled(true);  
       jfc.setAcceptAllFileFilterUsed(false);  
       jfc.addChoosableFileFilter(new MyFilter("xml"));
       int rv=jfc.showOpenDialog(null);
       if(rv==JFileChooser.APPROVE_OPTION){
       xpath=jfc.getSelectedFile().getAbsolutePath().toString();
       System.out.println(xpath);
       int length=xpath.lastIndexOf('.');
       opath.append(xpath,0,length);
       opath.append(".json");
       System.out.println(opath);
       jta.setText(xpath);
       }
       
}
else if(e.getActionCommand().equals("convert")){
if(xpath==null){
jta.setText("error,no file is selected");
}
else{
String xmlContext;
       try{
        InputStream is=new FileInputStream(xpath);//流化读取文件
        xmlContext=IOUtils.toString(is);
        XMLSerializer xmlSerializer=new XMLSerializer();
        JSON json=xmlSerializer.read(xmlContext);
        File fjson=new File(opath.toString());
        if(fjson.exists()){
        System.out.println("will cover the file!");
        }
        FileOutputStream fos=new FileOutputStream(fjson);
        byte buffer[]=json.toString().getBytes();
        fos.write(buffer);
       }catch(IOException el){
        el.printStackTrace();
       }
}
}
}
public static void main(String args[]){
new XMLToJSON();
}
}
class MyFilter extends FileFilter{
private String ftype;
public MyFilter(String ftype){
this.ftype=ftype;
}
public boolean accept(File f){
if(f.isDirectory()){
return true;
}
String suffix=f.getName();
int index=suffix.lastIndexOf('.');
if(suffix.toLowerCase().substring(index+1).equals(this.ftype)&&index>0){
return true;
}
else{
return false;
}
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return this.ftype.toUpperCase();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值