- 浏览: 59453 次
- 性别:
- 来自: 西安
最新评论
-
greatwall3:
引用[i][i]第三方的神烦大叔地方地方[/i][/i]
mongodb之DBRef的java应用 -
yangfei3169725:
谢谢.....帮我忙了
做MongoDB并发测试,报出上述错误。究其原因,是数据库连接数太少
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.newegg.lab.webdatacenter.VO.SitedataVO;
import com.newegg.lab.webdatacenter.VO.WebsiteVO;
import com.newegg.lab.webdatacenter.dbconfig.Constant;
import com.newegg.lab.webdatacenter.dbconfig.CrawlerMongoDBManager;
import com.newegg.lab.webdatacenter.util.XmlBuilder;
public class createXML implements FilenameFilter {
private String filename;
private String extension;
private SitedataVO data;
public List<String> xmlfilenamelist = new ArrayList<String>();
/**
* DatafeedXML生成时其他程序禁止对sellerproduct表进行插入修改等操作
*/
public static boolean DatafeedCreatemk = false;
/**
* DatafeedXML数据被读取时则取消DatafeedXML生成等任务执行
*/
public static boolean DatafeedExecutemk = false;
/**
* DatafeedXML首次生成标志(首次采用全表索引的方式)
*/
public static boolean DatafeedFirstmk = true;
public createXML() {
}
public createXML(String filename, String extension) {
this.filename = filename;
this.extension = extension;
}
public SitedataVO getData() {
return data;
}
public void setData(SitedataVO data) {
this.data = data;
}
/**
* 生成某个站点的XML文件
*/
public void execute() {
boolean newDocumentmk = true;
// 获得mongodb数据源
DBCollection collection = CrawlerMongoDBManager
.getCollection("mongodb_datacenter_productlist");
DBCursor cursor = collection.find().limit(100);
XmlBuilder outXml = new XmlBuilder();
while (cursor.hasNext()) {
if (newDocumentmk) {
outXml.appendRowText(new String[] { "isfullindex" },
new String[] { "0" });
newDocumentmk = false;
}
// System.out.println(Util.getDate()+"---->"+cursor.next().get("productName"));
DBObject dbObject = cursor.next();
String[] colName = { "active", "shopname", "catalogname", "brand",
"website", "itemid", "producturl", "productname", "model",
"unitprice", "listprice", "rebate", "shipping",
"shippingtime", "inventory", "inventorydetail", "infotime",
"status", "imagepath", "imageurl", "imageexist",
"isdeleted" };
String active = "";
if (dbObject.get("active") != null) {
active = (String) dbObject.get("active");
}
String shopname = "";// 商家名称
if (dbObject.get("shopname") != null) {
shopname = dbObject.get("shopname").toString();
}
String catalogname = "";// 商品分类名称
if (dbObject.get("catalogname") != null) {
catalogname = dbObject.get("catalogname").toString();
}
String brand = "";
if (dbObject.get("brand") != null) {
brand = (String) dbObject.get("brand");
}
String website = (String) dbObject.get("websiteId");
if (dbObject.get("websiteId") != null) {
website = (String) dbObject.get("websiteId");
}
String itemSKU = (String) dbObject.get("itemSku");
if (dbObject.get("itemSku") != null) {
itemSKU = (String) dbObject.get("itemSku");
}
String productUrl = (String) dbObject.get("productUrl");
if (dbObject.get("productUrl") != null) {
productUrl = (String) dbObject.get("productUrl");
}
String productname = (String) dbObject.get("productName");
if (dbObject.get("productName") != null) {
productname = (String) dbObject.get("productName");
}
String model = (String) dbObject.get("model");
if (dbObject.get("model") != null) {
model = (String) dbObject.get("model");
}
String unitprice = (String) dbObject.get("unitPrice");
if (dbObject.get("unitPrice") != null) {
unitprice = (String) dbObject.get("unitPrice");
}
String listprice = (String) dbObject.get("listPrice");
if (dbObject.get("listPrice") != null) {
listprice = (String) dbObject.get("listPrice");
}
String rebate = (String) dbObject.get("rebate");
String shipping = (String) dbObject.get("shipping");
String shippingtime = (String) dbObject.get("shippingTime");
String inventory = (String) dbObject.get("inventory");
String inventorydetail = (String) dbObject.get("inventoryDetail");
String infotime = (String) dbObject.get("infoTime");
String status = (String) dbObject.get("status");
String imagepath = (String) dbObject.get("imagePath");
String imageUrl = (String) dbObject.get("imageUrl");
String imageexist = "0";
String isdeleted = "0";
String[] colData = { active, shopname, catalogname,
"<![CDATA[" + brand + "]]>", website, itemSKU,
"<![CDATA[" + productUrl + "]]>",
"<![CDATA[" + productname + "]]>",
"<![CDATA[" + model + "]]>", unitprice, listprice, rebate,
shipping, shippingtime, inventory, inventorydetail,
infotime, status, imagepath, imageUrl, imageexist,
isdeleted };
outXml.appendRowText(colName, colData);
}
String websitename = "360361";
String xmldir = "d:/test/";
int xmlindex = Getfilestartnum(websitename, new File(xmldir));
SaveXml(outXml.getContext("product"), xmlindex, websitename, xmldir);
System.out.println("xml end!!!!!!!!!!!!!");
}
/**
* datafeed数据生成
*/
public void CreateDatafeedXml() {
xmlfilenamelist.clear();
if (DatafeedExecutemk)
return;
String xmldirpath = Constant.DATAFEED_XML_DIR;
File fdis = new File(xmldirpath);
if (!fdis.exists())
fdis.mkdirs();
if (DatafeedFirstmk) {
// 全文索引则先初始化XML文件
InitXml(fdis);
}
if (!CanExecute(fdis))
return;
List<WebsiteVO> sellerlist = new ArrayList<WebsiteVO>();
// 获得mongodb数据源
DBCollection collection = CrawlerMongoDBManager
.getCollection(Constant.DB_CONN_WEBSITE);
BasicDBObject contion = new BasicDBObject();
contion.put("siteName", "www.newegg.com.cn");
DBCursor cursor = collection.find(contion);
while (cursor.hasNext()) {
DBObject dbObject = cursor.next();
String siteid = "";
if (dbObject.get("_id") != null)
siteid = dbObject.get("_id").toString();
String sitename = "";
if (dbObject.get("siteName") != null)
sitename = (String) dbObject.get("siteName");
sellerlist.add(new WebsiteVO(siteid, sitename));
}
for (WebsiteVO seller : sellerlist) {
String websitename = seller.getSitename();
String condition = "";
if (DatafeedFirstmk) {
condition = seller.getSiteid();
CreateProductXmlFile(condition, 0, websitename, fdis);
} else {
// condition =
// " AND sep.SellerID = "+seller.getSellerId()+" AND sep.State = 1";
// CreateProductXmlFile(condition,1,websitename,fdis);
}
}
// sellerService.UpdateSellerProductState();
// 更新datafeed indexXML文件
UpdateIndexXml(fdis);
DatafeedFirstmk = false;
sellerlist.clear();
}
/**
* 生成完datadeed产生xml后,修改indexxml文件配置
*
* @param fdis
*/
private void UpdateIndexXml(File fdis) {
try {
String path = fdis.getAbsolutePath() + File.separatorChar
+ "datafeed_index.xml";
SAXReader saxReader = new SAXReader(false);
Document document = saxReader.read(path);
Element Root = document.getRootElement();
for (String filename : xmlfilenamelist) {
Element Fileindex = Root.addElement("fileindex");
Element Filetype = Fileindex.addElement("filetype");
Filetype.addCDATA("1");
Element Filename = Fileindex.addElement("filename");
Filename.addCDATA(filename);
Element Fileurl = Fileindex.addElement("fileurl");
Fileurl.addCDATA("d:/test2/datafeed_" + filename + ".xml");
}
// Writer fileWriter=new FileWriter(path);
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path));
xmlWriter.write(document);
xmlWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据条件获得站点数据
* @return
*/
@SuppressWarnings("unchecked")
private List<SitedataVO> getAllDataFromWebsite(String condition) {
List<SitedataVO> spList = new ArrayList<SitedataVO>();
// 获得mongodb数据源
DBCollection collection = CrawlerMongoDBManager
.getCollection(Constant.DB_DATACENTER_PRODUCTLIST);
BasicDBObject contion = new BasicDBObject();
contion.put("websiteId", condition);
contion.put("commentInfo", new BasicDBObject("$ne", null));
DBCursor cursor = collection.find(contion).limit(10);
while (cursor.hasNext()) {
DBObject dbObject = cursor.next();
String active = "";
if (dbObject.get("active") != null) {
active = (String) dbObject.get("active");
}
String shopname = "";// 商家名称
if (dbObject.get("shopname") != null) {
shopname = dbObject.get("shopname").toString();
}
String catalogname = "";// 商品分类名称
if (dbObject.get("catalogname") != null) {
catalogname = dbObject.get("catalogname").toString();
}
String brand = "";
if (dbObject.get("brand") != null) {
brand = (String) dbObject.get("brand");
}
String website = (String) dbObject.get("websiteId");
if (dbObject.get("websiteId") != null) {
website = (String) dbObject.get("websiteId");
}
String itemSKU = (String) dbObject.get("itemSku");
if (dbObject.get("itemSku") != null) {
itemSKU = (String) dbObject.get("itemSku");
}
String productUrl = (String) dbObject.get("productUrl");
if (dbObject.get("productUrl") != null) {
productUrl = (String) dbObject.get("productUrl");
}
String productname = (String) dbObject.get("productName");
if (dbObject.get("productName") != null) {
productname = (String) dbObject.get("productName");
}
String model = (String) dbObject.get("model");
if (dbObject.get("model") != null) {
model = (String) dbObject.get("model");
}
String unitprice = (String) dbObject.get("unitPrice");
if (dbObject.get("unitPrice") != null) {
unitprice = (String) dbObject.get("unitPrice");
}
String listprice = (String) dbObject.get("listPrice");
if (dbObject.get("listPrice") != null) {
listprice = (String) dbObject.get("listPrice");
}
String rebate = (String) dbObject.get("rebate");
String shipping = (String) dbObject.get("shipping");
String shippingtime = (String) dbObject.get("shippingTime");
String inventory = (String) dbObject.get("inventory");
String inventorydetail = (String) dbObject.get("inventoryDetail");
String createTime = (String) dbObject.get("createTime");
String status = (String) dbObject.get("status");
String imagepath = (String) dbObject.get("imagePath");
String imageUrl = (String) dbObject.get("imageUrl");
String imageexist = "0";
String isdeleted = "0";
String commenttotal = "";
if (dbObject.get("commenttotal") != null)
commenttotal = (String) dbObject.get("commenttotal");
String commenturl = "";
if (dbObject.get("commenturl") != null)
commenturl = (String) dbObject.get("commenturl");
String commentactive = "";
if (dbObject.get("commentactive") != null)
commentactive = (String) dbObject.get("commentactive");
String introduce = "";
if (dbObject.get("introduce") != null)
introduce = (String) dbObject.get("introduce");
String englishbrand = "";
if (dbObject.get("englishbrand") != null)
englishbrand = (String) dbObject.get("englishbrand");
String brandlogo = "";
if (dbObject.get("brandlogo") != null)
brandlogo = (String) dbObject.get("brandlogo");
String brandDesc = "";
if (dbObject.get("brandDesc") != null)
brandDesc = (String) dbObject.get("brandDesc");
String manufacture = "";
if (dbObject.get("manufacture") != null)
manufacture = (String) dbObject.get("manufacture");
String manufactureurl = "";
if (dbObject.get("manufactureurl") != null)
manufactureurl = (String) dbObject.get("manufactureurl");
DBObject commentinfo = (DBObject) dbObject.get("commentInfo");
List<DBObject> propertyinfo = (List<DBObject>) dbObject
.get("propertyInfo");
if (propertyinfo != null && propertyinfo.size() > 0) {
propertyinfo = (List<DBObject>) dbObject.get("propertyInfo");
} else {
propertyinfo = null;
}
spList.add(new SitedataVO(itemSKU, productname, productUrl,
shopname, brand, model, unitprice, listprice, rebate,
shipping, shippingtime, inventory, inventorydetail, active,
status, website, catalogname, imagepath, imageUrl,
imageexist, createTime, commenttotal, commenturl,
commentactive, commentinfo, propertyinfo, introduce,
isdeleted, englishbrand, brandlogo, brandDesc, manufacture,
manufactureurl));
}
return spList;
}
/**
* 生成某个站点的XML文件
*
* @param condition
* @param isfullindex
* @param websitename
* @param xmldir
*/
@SuppressWarnings( { "unchecked", "deprecation" })
private void CreateProductXmlFile(String condition, int isfullindex,
String websitename, File xmldir) {
try {
List<SitedataVO> spList = getAllDataFromWebsite(condition);
System.out.println("website:" + websitename + " itemscount:"
+ spList.size());
int xmlindex = Getfilestartnum(websitename, xmldir);
boolean newDocumentmk = true;
Document document = null;
Element Root = null;
int XmlLen = 39;
for (int i = 0; i < spList.size(); i++) {
SitedataVO sellerProduct = spList.get(i);
if (newDocumentmk) {
document = DocumentHelper.createDocument();
Root = document.addElement("Root");
XmlLen += 13;
Element Fullindex = Root.addElement("isfullindex");
Fullindex.addCDATA(isfullindex + "");
XmlLen += 28;
newDocumentmk = false;
}
Element Product = Root.addElement("product");
XmlLen += 19;
Element Shopname = Product.addElement("shopname");
XmlLen += 21;
Shopname.addCDATA(sellerProduct.getShopname().trim());
XmlLen += sellerProduct.getShopname().getBytes().length;
Element Catalogname = Product.addElement("catalogname");
XmlLen += 27;
Catalogname.addCDATA(sellerProduct.getCatalogname().trim());
XmlLen += sellerProduct.getCatalogname().getBytes().length;
Element Brand = Product.addElement("brand");
XmlLen += 15;
Brand.addCDATA(sellerProduct.getBrand().trim());
XmlLen += sellerProduct.getBrand().getBytes().length;
Element Website = Product.addElement("website");
XmlLen += 19;
Website.addCDATA(sellerProduct.getWebsite().trim());
XmlLen += sellerProduct.getWebsite().getBytes().length;
Element Itemid = Product.addElement("itemid");
// Itemid.addCDATA(String.valueOf(sellerProduct.getProducturl().trim().hashCode()));
// XmlLen += 17;
Itemid.addCDATA(sellerProduct.getItemSKU().trim());
XmlLen += sellerProduct.getItemSKU().getBytes().length;
Element Producturl = Product.addElement("producturl");
XmlLen += 25;
Producturl.addCDATA(sellerProduct.getProducturl().trim());
XmlLen += sellerProduct.getProducturl().getBytes().length;
Element Productname = Product.addElement("productname");
XmlLen += 27;
Productname.addCDATA(sellerProduct.getProductname().trim());
XmlLen += sellerProduct.getProductname().getBytes().length;
Element Model = Product.addElement("model");
XmlLen += 15;
Model.addCDATA(sellerProduct.getModel().trim());
XmlLen += sellerProduct.getModel() != null ? sellerProduct
.getModel().getBytes().length : 23;
Element Unitprice = Product.addElement("unitprice");
XmlLen += 23;
Unitprice.addCDATA(sellerProduct.getUnitprice().toString());
XmlLen += sellerProduct.getUnitprice().toString().getBytes().length;
/*
* if (isfullindex == 2 && sellerProduct.getListprice().size()
* == 2){ //更新数据时则需要填写最近价格变动情况 Element Listprice =
* Product.addElement("listprice"); XmlLen += 23;
* Listprice.addCDATA
* (sellerProduct.getListprice().get(1).getPrice().toString());
* XmlLen +=
* sellerProduct.getListprice().get(1).getPrice().toString
* ().getBytes().length; Element Rebate =
* Product.addElement("rebate"); XmlLen += 17; Double re =
* sellerProduct
* .getPriceList().get(0).getPrice()-sellerProduct.getPriceList
* ().get(1).getPrice(); Rebate.addCDATA(re.toString()); XmlLen
* += re.toString().getBytes().length; } else{ Element Listprice
* = Product.addElement("listprice"); XmlLen += 23; Element
* Rebate = Product.addElement("rebate"); XmlLen += 17; }
*/
Element Shipping = Product.addElement("shipping");
XmlLen += 21;
Shipping.addCDATA(sellerProduct.getShipping());
XmlLen += sellerProduct.getShipping() != null ? sellerProduct
.getShipping().getBytes().length : 25;
Element Shippingtime = Product.addElement("shippingtime");
XmlLen += 29;
Shippingtime.addCDATA(sellerProduct.getShippingtime());
XmlLen += sellerProduct.getShippingtime() != null ? sellerProduct
.getShippingtime().getBytes().length
: 25;
Element Inventory = Product.addElement("inventory");
XmlLen += 24;
Inventory.addCDATA(sellerProduct.getInventory());
XmlLen += sellerProduct.getInventory() != null ? sellerProduct
.getInventory().getBytes().length : 25;
Element Inventorydetail = Product.addElement("inventorydetail");
XmlLen += 35;
Inventorydetail.addCDATA(sellerProduct.getInventorydetail());
XmlLen += sellerProduct.getInventorydetail() != null ? sellerProduct
.getInventorydetail().getBytes().length
: 25;
Element createtime = Product.addElement("createtime");
XmlLen += 21;
createtime.addCDATA((sellerProduct.getCreatetime()));
XmlLen += sellerProduct.getCreatetime().getBytes().length;
/*
* if (isfullindex == 2){ if
* (sellerProduct.getActive().intValue() == 1){ Element Deleted
* = Product.addElement("isdeleted"); Deleted.addCDATA("0"); }
* else{ Element Deleted = Product.addElement("isdeleted");
* Deleted.addCDATA("1"); } } else{ Element Deleted =
* Product.addElement("isdeleted"); Deleted.addCDATA("0"); }
*/
XmlLen += 24;
Element Imagepath = Product.addElement("imagepath");
XmlLen += 23;
Imagepath.addCDATA(sellerProduct.getImagepath());
XmlLen += sellerProduct.getImagepath() != null ? sellerProduct
.getImagepath().getBytes().length : 25;
Element Imageurl = Product.addElement("imageurl");
XmlLen += 21;
Imageurl
.addCDATA(sellerProduct.getImageurl() != null ? sellerProduct
.getImageurl()
: "");
XmlLen += (sellerProduct.getImageurl() != null ? sellerProduct
.getImageurl() : "").getBytes().length;
Element Imageexist = Product.addElement("imageexist");
XmlLen += 26;
Imageexist
.addCDATA(sellerProduct.getImageexist() != null ? sellerProduct
.getImageexist()
: "");
XmlLen += (sellerProduct.getImageexist() != null ? sellerProduct
.getImageexist()
: "").getBytes().length;
Element Propertyinfo = Product.addElement("propertyinfo");
XmlLen += 29;
if (sellerProduct.getPropertyinfo() != null) {
Element Propertygroup = null;
Element Property = null;
for (int j = 0; j < sellerProduct.getPropertyinfo().size(); j++) {
BasicDBObject property = (BasicDBObject) sellerProduct
.getPropertyinfo().get(j);
Propertygroup = Propertyinfo
.addElement("propertygroup");
XmlLen += 31;
String propertyGroupName = property.get(
"propertyGroupName").toString();
Propertygroup.setAttributeValue("name",
propertyGroupName);
XmlLen += propertyGroupName.getBytes().length;
List<DBObject> propertyvalueList = (List<DBObject>) property
.get("propertyGroupValue");
for (DBObject dbObject2 : propertyvalueList) {
Property = Propertygroup.addElement("property");
XmlLen += 21;
String propertyName = dbObject2.get("propertyName") != null ? dbObject2
.get("propertyName").toString()
: "";
String propertyValue = dbObject2
.get("propertyValue") != null ? dbObject2
.get("propertyValue").toString() : "";
Property.setAttributeValue("name", propertyName);
XmlLen += propertyName.getBytes().length;
Property.addCDATA(propertyValue);
XmlLen += propertyValue.getBytes().length;
}
}
}
Element Commentactive = Product.addElement("commentactive");
XmlLen += 31;
Commentactive
.addCDATA(sellerProduct.getCommentactive() != null ? sellerProduct
.getCommentactive()
: "");
XmlLen += (sellerProduct.getCommentactive() != null ? sellerProduct
.getCommentactive()
: "").getBytes().length;
Element Commenttotal = Product.addElement("commenttotal");
Commenttotal.addCDATA("0");
XmlLen += 30;
Element commentInfo = Product.addElement("commentinfo");
XmlLen += 29;
if (sellerProduct.getCommentinfo() != null) {
Element Comments = null;
DBObject commentinfo = sellerProduct.getCommentinfo();
Comments = commentInfo.addElement("goodcomments");
XmlLen += 31;
DBObject Commentsinfo = (DBObject) commentinfo
.get("GoodComments");
XmlLen=parseCommentContent(Comments, Commentsinfo, XmlLen);
Comments = commentInfo.addElement("generalcomments");
XmlLen += 31;
Commentsinfo = (DBObject) commentinfo
.get("GeneralComments");
XmlLen=parseCommentContent(Comments, Commentsinfo, XmlLen);
Comments = commentInfo.addElement("poolcomments");
XmlLen += 31;
Commentsinfo = (DBObject) commentinfo
.get("PoorComments");
XmlLen = parseCommentContent(Comments, Commentsinfo, XmlLen);
}
Element Introduce = Product.addElement("introduce");
XmlLen += 23;
Introduce.addCDATA(sellerProduct.getIntroduce().trim());
XmlLen += sellerProduct.getIntroduce().trim().getBytes().length;
Element EnglishBrand = Product.addElement("englishBrand");
XmlLen += 29;
EnglishBrand.addCDATA(sellerProduct.getEnglishbrand().trim());
XmlLen += sellerProduct.getEnglishbrand().getBytes().length;
Element BrandLogo = Product.addElement("brandLogo");
XmlLen += 23;
BrandLogo.addCDATA(sellerProduct.getBrandlogo().trim());
XmlLen += (sellerProduct.getBrandlogo()).getBytes().length;
Element BrandDesc = Product.addElement("BrandDesc");
XmlLen += 23;
BrandDesc.addCDATA(sellerProduct.getBrandDesc().trim());
XmlLen += sellerProduct.getBrandDesc().trim().getBytes().length;
Element Manufacture = Product.addElement("manufacture");
XmlLen += 27;
Manufacture.addCDATA(sellerProduct.getManufacture().trim());
XmlLen += (sellerProduct.getManufacture()).getBytes().length;
Element Manufactureurl = Product.addElement("manufactureurl");
XmlLen += 33;
Manufactureurl.addCDATA(sellerProduct.getManufactureurl()
.trim());
XmlLen += (sellerProduct.getManufactureurl()).getBytes().length;
if (i == spList.size() - 1) {
SaveXml(document, xmlindex, websitename, xmldir
.getAbsolutePath());
return;
}
if (i != 0 && i % 1000 == 0) {
if (Islimit(document)) {
SaveXml(document, xmlindex, websitename, xmldir
.getAbsolutePath());
xmlindex++;
newDocumentmk = true;
}
}
if (XmlLen * 2 >= 20 * 1024 * 1024) {
SaveXml(document, xmlindex, websitename, xmldir
.getAbsolutePath());
xmlindex++;
newDocumentmk = true;
XmlLen = 39;
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private int parseCommentContent(Element GoodComments,DBObject goodCommentsinfo,int XmlLen){
if (goodCommentsinfo != null) {
Element commentcount = GoodComments.addElement("commentcount");
XmlLen += 31;
String commentnum = goodCommentsinfo
.get("commentcount") != null ? goodCommentsinfo
.get("commentcount").toString() : "";
commentcount.addCDATA(commentnum);
Element actualcommentcount = GoodComments
.addElement("actualcommentcount");
XmlLen += 31;
String actualcommentnum = goodCommentsinfo
.get("actualcommentcount") != null ? goodCommentsinfo
.get("actualcommentcount").toString()
: "";
actualcommentcount.addCDATA(actualcommentnum);
List<DBObject> commentList = (List<DBObject>) goodCommentsinfo
.get("comment");
if (commentList != null) {
for (DBObject commentcontent : commentList) {
Element comment = GoodComments.addElement("comment");
XmlLen += 31;
Element publisher = comment.addElement("publisher");
XmlLen += 31;
publisher
.addCDATA((String) (commentcontent
.get("publisher") != null ? commentcontent
.get("publisher")
: ""));
XmlLen += (commentcontent.get("publisher") != null ? commentcontent
.get("publisher").toString().getBytes().length
: 31);
Element rank = comment.addElement("rank");
XmlLen += 31;
rank.addCDATA((String) (commentcontent
.get("rank") != null ? commentcontent
.get("rank") : ""));
XmlLen += (commentcontent.get("rank") != null ? commentcontent
.get("rank").toString().getBytes().length
: 31);
Element description = comment.addElement("description");
XmlLen += 31;
description
.addCDATA((String) (commentcontent
.get("description") != null ? commentcontent
.get("description")
: ""));
XmlLen += (commentcontent.get("description") != null ? commentcontent
.get("description").toString()
.getBytes().length
: 31);
Element publishingtime = comment
.addElement("publishingtime");
XmlLen += 31;
publishingtime
.addCDATA((String) (commentcontent
.get("publishingtime") != null ? commentcontent
.get("publishingtime")
: ""));
XmlLen += (commentcontent.get("publishingtime") != null ? commentcontent
.get("publishingtime").toString()
.getBytes().length
: 31);
}
}
}
return XmlLen;
}
/**
* 保存当前document为XML文件
*
* @param document
* @param xmlindex
* @param websitename
* @param dirpath
*/
private void SaveXml(String text, int xmlindex, String websitename,
String dirpath) {
try {
String filename = "datafeed_" + websitename + "_"
+ String.format("%02d", xmlindex);
xmlfilenamelist.add(filename);
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(dirpath
+ File.separatorChar + filename + ".xml"));
Document document = new SAXReader().read(new ByteArrayInputStream(
text.getBytes()));
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (DocumentException e) {
e.printStackTrace();
}
}
private void SaveXml(Document document, int xmlindex,
String websitename, String dirpath) throws DocumentException {
try {
String filename = "datafeed_" + websitename + "_"
+ String.format("%02d", xmlindex);
xmlfilenamelist.add(filename);
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(dirpath
+ File.separatorChar + filename + ".xml"));
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
* 判断XML是否达到20M容量
*
* @param document
* @return
*/
private static boolean Islimit(Document document) {
byte[] xmlby = document.asXML().getBytes();
if (xmlby.length >= 20 * 1024 * 1024)
return true;
else
return false;
}
/**
* 获取某站点XML文件起始编号
*
* @param websitename
* @param xmldir
* @return
*/
private static int Getfilestartnum(String websitename, File xmldir) {
File[] filenames = xmldir.listFiles();
int index = 1;
for (File pfile : filenames) {
if (pfile.isFile() && pfile.getName().contains(websitename))
index++;
}
return index;
}
/**
* 初始化datafeedXML文件,先删除目录下所有XML文件,然后产生基础的目录文件
*
* @param xmldir
*/
private void InitXml(File fdis) {
try {
// FileUtils.cleanDirectory(fdis);
deleteFiles(fdis.getAbsolutePath(), "*", "xml");
String path = fdis.getAbsolutePath() + File.separatorChar
+ "datafeed_index.xml";
Document document = DocumentHelper.createDocument();
@SuppressWarnings("unused")
Element Root = document.addElement("Root");
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(path));
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除指定文件或一些文件
* @param path
* @param inname
* @param inextension
*/
private void deleteFiles(String path, String inname,
String inextension) {
FileManagerFilter fmf = new FileManagerFilter(inname, inextension);
File file = new File(path);
File[] filelist = file.listFiles(fmf);
if (filelist.length != 0) {
System.out.println("**************删除文件*************");
for (File fl : filelist) {
System.out.println(fl + (fl.delete() ? " 成功" : " 没有成功")
+ "被删除!");
}
} else {
System.out.println("根据您所给的条件,没有找到要删除的文件!");
}
}
/**
* 判断本次定时是否可以执行,若上次产生的XML文件尚未处理则本次输出XML任务不执行
*
* @param xmldir
* @return
*/
private boolean CanExecute(File xmldir) {
File[] filenames = xmldir.listFiles();
if (filenames.length > 1)
return false;
else
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
// new createXML().UpdateIndexXml(new File("d:/test2/"));
new createXML().CreateDatafeedXml();
System.out.println("end@@@@@@@@@@@@@@@@@@@@@@@@");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean accept(File dir, String name) {
boolean fileOK = true;
if (name == "*" && extension == "*") {
return fileOK = true;
}
if (name != null) {
if (name != "*") {
fileOK &= filename.startsWith(name);
}
}
if (extension != null) {
fileOK &= filename.endsWith('.' + extension);
}
return fileOK;
}
}
发表评论
-
软件开发中的11个系统思维定律
2014-08-15 09:08 783英文原文:11 Laws of The ... -
单点登录cas jasig学习笔记
2014-05-08 16:25 915cas单点登录 1 什么是单点登录 ... -
ORA-00913: 值过多
2014-03-25 11:54 1379今天做项目 ... -
eclipse maven plugin 插件 安装 和 配置
2013-05-27 14:45 836环境准备: eclipse(He ... -
为经典版eclipse增加web and JavaEE插件
2013-05-07 08:26 824没怎么用eclipse所以对它不熟悉,近期在看pho ... -
提升Heritrix3.1.1的抓取效率
2013-05-02 16:47 1123Heritrix3.1.1是老外写的爬虫,可配置性非常 ... -
Linux软件安装方法小结(附:rpm详解)
2013-04-27 18:24 935在使用Linux系统的过程中,软件包的安装是避免不了的,在L ... -
eclipse安装 最新版 m2eclipse插件
2013-04-05 12:09 840近来,安装Maven,多数人提示在 http://m2ecl ... -
strust2demo
2013-02-20 14:56 8031)首先新建一个web工程:struts2demo 2)在 ... -
中英文字符截取问题
2013-02-05 16:18 793public static void main(Strin ... -
java 命令行引用jar包的方法
2013-02-02 09:31 1077关于这个问题,一直有些疑惑。今天在网上找了一下相关的资料。 ... -
java 常用小工具总结
2013-01-29 16:28 3187import java.io.BufferedReader; ... -
删除指定目录下指定类型的文件操作
2012-12-14 14:31 975import java.io.File; publ ... -
List 不同于数组
2012-11-23 14:41 798Java 开发人员常常错误地认为 ArrayList 就是 J ... -
equals() 与 Comparable
2012-11-23 14:33 1669在浏览 Javadoc 时,Java 开发人员常常会遇到 So ... -
注意可变的 hashCode()
2012-11-23 14:29 928Map 是很好的集合,为我们带来了在其他语言(比如 Perl) ... -
Spring框架简介(转)
2012-11-06 17:13 8761、使用框架的意义与 ... -
java.lang.OutOfMemoryError: Java heap space
2012-11-03 13:46 809想一想,还是对于垃 ... -
java 实现的生产者-消费者问题
2012-11-02 15:23 762/** * * 生成者消费者问题 **/ pub ... -
mongodb之DBRef的java应用
2012-10-25 15:13 7420List<DBObject> listObj ...
相关推荐
JAVA 解析XML和生成XML文档源码。比较全 1.DOM生成和解析XML文档 2.SAX生成和解析XML文档 3.DOM4J生成和解析XML文档 4.JDOM生成和解析XML
首先,让我们理解Java生成XML的基本步骤: 1. **创建XML文档对象**:在Java中,我们可以使用`DocumentBuilderFactory`和`DocumentBuilder`类来创建一个`Document`对象,这个对象代表整个XML文档。 ```java ...
用java生成xml文档.DocumentBuilderFactory DocumentBuilder Document doc;至此可以用doc来操作xml文档了
java生成xml文件的时候如何控制xml的缩进格式.pdf
JAVA中生成xml文件到指定路径和上传到ftp服务器到指定路径的方法。
本篇主要介绍如何通过Java编程语言生成XML文档,结合“XML项目开发中用到的”这一标签,我们将深入探讨Java API中的DOM、SAX和StAX三种解析器,以及它们在创建XML文件时的应用。 1. DOM(Document Object Model)...
在Java开发中,XML(可扩展...总结起来,Java生成XML和XSD涉及到XML和XSD的基本概念,以及使用JAXB等Java库进行XML操作的技术。在实际项目中,理解并熟练运用这些知识对于处理结构化数据的序列化和反序列化至关重要。
本篇文章将深入探讨如何使用Java来生成XML文件,并结合数据库操作进行数据提取。 首先,我们需要了解XML的基本结构。XML文件由一系列元素组成,每个元素都有开始标签(如`<element>`)和结束标签(如`</element>`)...
在Java编程中,生成XML文档是一项常见的任务,特别是在构建配置文件、数据交换或者保存结构化信息时。XML(eXtensible Markup Language)是一种标记语言,它的结构清晰、可读性强,非常适合用来存储和传输数据。...
### JAVA生成XML文件知识点 #### 一、简介 在Java编程中,生成XML文件是一种常见的需求,尤其是在数据交换、配置管理或系统间通信时。本文档通过一个具体的示例介绍了如何使用Java语言来生成XML文件,并对代码进行...
java 生成xml,需要jar包需要自己下。
标签"解析XML和生成XML的必备选择"表明,DOM不仅适用于生成XML,也常用于解析XML文档。解析XML时,`DocumentBuilder`会构建一个DOM树,然后可以通过DOM API查询和修改树中的节点。 总结一下,Java DOM提供了一套...
1. **数据模型(Model)**:这是你要生成XML所依赖的数据,可以是Java对象、Map或者其他集合类型。例如,你可以创建一个包含多个元素的列表,每个元素都有属性如`name`、`age`等。 ```java List<Person> persons = ...
本篇文章将深入探讨如何使用Java生成XML文档以及操作XML文件设置值。 一、Java生成XML文档 1. DOM(Document Object Model):DOM是W3C推荐的一种XML的标准API,它将XML文档解析为一个树形结构,允许我们通过节点...
在Java编程中,生成XML文件是一项常见的任务,特别是在数据存储、配置管理或数据交换的应用场景。XML(eXtensible Markup Language)是一种结构化数据格式,具有良好的可读性和易于解析的特点。本教程将深入讲解如何...
Java生成XML是Java编程中常见的任务,特别是在处理数据序列化或与外部系统交换结构化信息时。本教程将深入探讨如何使用JDOM库在Java中生成XML文档,并且重点介绍如何处理中文字符。 JDOM(Java Document Object ...
在Java编程中,生成XML(eXtensible Markup Language)文件是一项常见的任务,尤其是在数据交换、配置存储或报告生成等领域。XML是一种结构化数据格式,它以易于人类阅读和机器解析的方式存储信息。本篇文章将深入...
Java解析和生成XML的知识点主要涵盖了使用JAXB(Java Architecture for XML Binding)框架来实现Java对象与XML(可扩展标记语言)之间的数据转换。JAXB提供了一种便捷的方式,通过注解(Annotation)来实现这一过程...
2. XSD到JavaBean的生成:生成JavaBean是将XSD文件中的元素和属性映射为Java类的过程,这有助于在Java应用程序中操作XML数据。通常,开发者会使用命令行脚本或者库来完成这个任务。例如,`createPro_Xsd.bat`和`...
Java2XML生成XML