當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Java JSON 之 Xml 转 JSON 字符串
生活随笔
收集整理的這篇文章主要介紹了
Java JSON 之 Xml 转 JSON 字符串
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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.xmlSAXReader reader=new SAXReader();try{Document doc=reader.read(new File("product.xml"));//根據(jù)路徑 /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 存入 productListproductList.add(product);}//productList 轉(zhuǎn)化成 JSON 字符串JSONArray jsonArray=new JSONArray(productList);System.out.println(jsonArray.toString());} catch (DocumentException e){e.printStackTrace();}} }package com.json20;public class Product {//產(chǎn)品(編號(hào),產(chǎn)品名稱,數(shù)量,價(jià)格)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("物產(chǎn)構(gòu)造方法調(diào)用");}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
轉(zhuǎn)載于:https://www.cnblogs.com/verejava/p/9236973.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Java JSON 之 Xml 转 JSON 字符串的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 自动化测试中,测试数据如何管理?
- 下一篇: 6.28 头像预览:form方法和aja