Java JSON 之 Xml 转 JSON 字符串

http://www.verejava.com/?id=16998704382152

下载依赖 jar 包   

json.jar 
dom4j-1.6.1.jar 
jaxen-1.1-beta-6.jar 

product.xml 

<product>
  <id>1</id>
  <name>电脑</name>
  <quantity>2</quantity>
  <price>4000</price>
</product>




package com.json19;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.json.JSONArray;

import com.json18.Product;

public class Test
{
	public static void main(String[] args)
	{
		List<Product> productList=new ArrayList<Product>();
		//SAXReader 读取  product.xml
		SAXReader reader=new SAXReader();
		try
		{
			Document doc=reader.read(new File("product.xml"));
			//根据路径 /data/product 获得 所有的 product element 元素集合
			List<Element> elementList=doc.selectNodes("/data/product");
			for(int i=0;i<elementList.size();i++)
			{
				/*
				 <product>
			        <id>1</id>
			        <name>电脑</name>
			        <quantity>2</quantity>
			        <price>4000</price>
			    </product>
				 */
				Product product=new Product();
				Element element=elementList.get(i);
				//获得 product 下面的子元素
				Iterator<Element> childIter=element.elementIterator();
				while(childIter.hasNext())
				{
					Element childElement=childIter.next();
					if("id".equals(childElement.getName()))
					{
						product.setId(Integer.parseInt(childElement.getText()));
					}else if("name".equals(childElement.getName()))
					{
						product.setName(childElement.getText());
					}else if("quantity".equals(childElement.getName()))
					{
						product.setQuantity(Integer.parseInt(childElement.getText()));
						
					}else if("price".equals(childElement.getName()))
					{
						product.setPrice(Double.parseDouble(childElement.getText()));
					}
				}
				
				//prodcut 存入 productList
				productList.add(product);
			}
			
			//productList 转化成  JSON 字符串
			JSONArray jsonArray=new JSONArray(productList);
			System.out.println(jsonArray.toString());
			
		} catch (DocumentException e)
		{
			e.printStackTrace();
		}
	}
}





package com.json20;

public class Product
{
	//产品(编号,产品名称,数量,价格)
	private int id;
	private String name;
	private int quantity;
	private double price;
	
	public Product(int id, String name, int quantity, double price)
	{
		super();
		this.id = id;
		this.name = name;
		this.quantity = quantity;
		this.price = price;
	}
	public Product()
	{
		super();
		this.id=100;
		System.out.println("物产构造方法调用");
	}
	public int getId()
	{
		return id;
	}
	public void setId(int id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getQuantity()
	{
		return quantity;
	}
	public void setQuantity(int quantity)
	{
		this.quantity = quantity;
	}
	public double getPrice()
	{
		return price;
	}
	public void setPrice(double price)
	{
		this.price = price;
	}
	
	
}


http://www.verejava.com/?id=16998704382152

posted @ 2018-06-28 09:07  verejava  阅读(445)  评论(0)    收藏  举报