java document的用法_Java Document.add方法代码示例

import org.dom4j.Document; //导入方法依赖的package包/类

/**

* 保存http请求信息到xml

*

* @param hc

* @return

* @throws Exception

*/

public static boolean saveHttpConfig(HttpConfig hc) throws Exception {

SAXReader reader = new SAXReader();

File xml = new File(HTTP_CONFIG_FILE);

Document doc = null;

Element root = null;

boolean isNew = true;

if (!xml.exists()) {

doc = DocumentHelper.createDocument();

root = DocumentHelper.createElement("root");

root.addElement("configs");

doc.add(root);

}

if (doc == null) {

try (FileInputStream in = new FileInputStream(xml); Reader read = new InputStreamReader(in, "UTF-8")) {

doc = reader.read(read);

root = doc.getRootElement();

}

}

Element cfg = (Element) root.selectSingleNode("/root/configs");

Element e = (Element) root.selectSingleNode("/root/configs/config[@name='" + hc.getName() + "']");

if (e != null) {

isNew = false;

cfg.remove(e);

}

CONFIG_MAP.put(hc.getName(), hc);

Element cfg1 = cfg.addElement("config");

cfg1.addAttribute("name", hc.getName());

cfg1.addAttribute("encodeType", hc.getEncodeType());

cfg1.addAttribute("charset", hc.getCharset());

cfg1.addAttribute("requestType", hc.getRequestType());

cfg1.addAttribute("sendXML", hc.getSendXML().toString());

cfg1.addAttribute("packHead", hc.getPackHead().toString());

cfg1.addAttribute("lowercaseEncode", hc.getLowercaseEncode().toString());

cfg1.addElement("url").setText(hc.getUrl());

cfg1.addElement("header").setText(hc.getHeaderStr());

cfg1.addElement("parameter").setText(hc.getParameterStr());

cfg1.addElement("encodeField").setText(hc.getEncodeFieldName());

cfg1.addElement("encodeKey").setText(hc.getEncodeKey());

OutputFormat format = OutputFormat.createPrettyPrint();

format.setEncoding("UTF-8");

XMLWriter writer = new XMLWriter(new FileOutputStream(xml), format);

writer.write(doc);

writer.close();

return isNew;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Java将JSON文档数组插入MongoDB时,你需要使用BSON工具将JSON转换为BSON格式。以下是一个示例代码,可以将JSON数组插入到MongoDB中: ``` import com.mongodb.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.bson.Document; import org.bson.conversions.Bson; import org.bson.types.ObjectId; import java.util.ArrayList; import java.util.List; public class MongoDBTest { public static void main(String[] args) { // 创建MongoDB连接 MongoClient client = new MongoClient("localhost", 27017); // 获取MongoDB数据库 MongoDatabase database = client.getDatabase("test"); // 获取MongoDB集合 MongoCollection<Document> collection = database.getCollection("users"); // 创建JSON数组 String json = "[{ \"name\" : \"Tom\", \"age\" : 20 },{ \"name\" : \"Jerry\", \"age\" : 25 }]"; // 将JSON数组转换为BSON文档列表 List<Document> documents = new ArrayList<>(); JsonParser parser = new JsonParser(); JsonArray jsonArray = parser.parse(json).getAsJsonArray(); for (JsonElement jsonElement : jsonArray) { Document document = Document.parse(jsonElement.toString()); documents.add(document); } // 插入BSON文档列表到MongoDB集合 collection.insertMany(documents); // 关闭MongoDB连接 client.close(); } } ``` 在这个示例中,我们使用了JsonParser将JSON字符串转换为JsonArray,然后遍历JsonArray,将每个元素转换为BSON文档,并将文档添加到BSON文档列表中。最后,我们使用insertMany方法将BSON文档列表插入到MongoDB集合中。注意,在使用BSON工具时,需要注意BSON格式与JSON格式的差异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值