如何在Java中从URL获取xml响应并将其存储在MONGODB中

如何在Java中从URL获取xml响应并将其存储在MONGODB中,java,mongodb,Java,Mongodb,我想请求一个URL,该URL将返回如下xml响应: <SERVERPERFORMANCEMETRICS> <SERVERNAME>abc</SERVERNAME> <SERVERSTARTUPTIME>11/25/2016 11:00 AM</SERVERSTARTUPTIME> <TOTALMASTERREQUESTS>4</TOTALMASTERREQUESTS> <TOTALRENDERERRE

我想请求一个URL,该URL将返回如下xml响应:

<SERVERPERFORMANCEMETRICS>
<SERVERNAME>abc</SERVERNAME> 
<SERVERSTARTUPTIME>11/25/2016 11:00 AM</SERVERSTARTUPTIME> 
<TOTALMASTERREQUESTS>4</TOTALMASTERREQUESTS> 
<TOTALRENDERERREQUESTS>13</TOTALRENDERERREQUESTS> 
<FAILEDREQUESTS>10</FAILEDREQUESTS>
</SERVERPERFORMANCEMETRICS>

abc
2016年11月25日上午11:00
4.
13
10
现在我想在MONGODB中解析并存储这个xml数据

        URL url;
        HttpURLConnection urlConnection = null; 


        MongoClientURI mongoClientURI = new MongoClientURI(dbURL);
        MongoClient mongoClient = new MongoClient(mongoClientURI);

        /*  To connect database, you need to specify the database name, if the database 
            doesn't exist then MongoDB creates it automatically. */
        MongoDatabase mongoDatabase = mongoClient.getDatabase("myDb");
        System.out.println("Connected to database successfully");

        MongoCollection<Document> mongoCollection = mongoDatabase.getCollection("myCollection");
        System.out.println("Collection created successfully");

        url = new URL(targetURL);
        urlConnection =  (HttpURLConnection) url.openConnection();
        InputStream inputStream = urlConnection.getInputStream();
        InputStreamReader inputStreamReader =  new InputStreamReader(inputStream);
URL;
HttpURLConnection-urlConnection=null;
MongoClientURI MongoClientURI=新的MongoClientURI(dbURL);
MongoClient MongoClient=新的MongoClient(MongoClient);
/*若要连接数据库,需要指定数据库名称(如果数据库
如果不存在,MongoDB会自动创建它*/
MongoDatabase MongoDatabase=mongoClient.getDatabase(“myDb”);
System.out.println(“成功连接到数据库”);
MongoCollection MongoCollection=mongoDatabase.getCollection(“myCollection”);
System.out.println(“已成功创建集合”);
url=新url(targetURL);
urlConnection=(HttpURLConnection)url.openConnection();
InputStream InputStream=urlConnection.getInputStream();
InputStreamReader InputStreamReader=新的InputStreamReader(inputStream);

使用W3C的
DocumentBuilder

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(inputStream);

使用W3C的
DocumentBuilder

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(inputStream);

我自己用json java jar找到了解决方案

       if (inputStream != null) {
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = bufferedReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }

            JSONObject jsonObject = XML.toJSONObject(responseStrBuilder.toString());
            System.out.println(jsonObject);

            org.bson.Document jsonDocument = org.bson.Document.parse(jsonObject.toString());
            mongoCollection.insertOne(jsonDocument);
            System.out.println("Collection inserted successfully");
        }   

我自己用json java jar找到了解决方案

       if (inputStream != null) {
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = bufferedReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }

            JSONObject jsonObject = XML.toJSONObject(responseStrBuilder.toString());
            System.out.println(jsonObject);

            org.bson.Document jsonDocument = org.bson.Document.parse(jsonObject.toString());
            mongoCollection.insertOne(jsonDocument);
            System.out.println("Collection inserted successfully");
        }   

请将xml作为代码而不是图片发布。
abc 11/25/2016 11:00 AM 4 13 10
请将xml作为代码而不是图片发布。
abc 11/25/2016 11:00 AM 4 13 10