日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Java JSON 之 Xml 转 JSON 字符串

發布時間:2025/6/17 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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.xmlSAXReader 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 存入 productListproductList.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

轉載于:https://www.cnblogs.com/verejava/p/9236973.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Java JSON 之 Xml 转 JSON 字符串的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。