以XML文件格式的内容保存:
String ext = metaFileName.substring(metaFileName.lastIndexOf("."));
logger.debug("文件后缀名:" + ext);
if(!".xml".equalsIgnoreCase(ext)) {
addActionError("文件类型错误,只能是XML格式的文件。");
} else {
logger.debug("元数据文件:{}", meta.getAbsolutePath());
logger.debug("元数据文件存在:{}", meta.exists());
SAXReader sax = new SAXReader();
Document doc;
try {
doc = sax.read(meta);
getModel().setFileContent(doc.asXML());
super.save();
} catch (DocumentException e) {
e.printStackTrace();
}
}
开始读取XML文件的时候用的是流的方式,影响文件编码格式(以后做事要多动脑子,多试。。。。)
读取XML后,根据xml标签说明对应文件,解析XML成文本内容。
public String queryMetadataInfo() {
Metadata mdata = getManager().getMatadataInfo(getModel().getType());
if (mdata != null) {
XMLWriter outFile = null;
try {
logger.debug("元数据文件内容:{}", mdata.getFileContent());
Document outerdoc = DocumentHelper.parseText(mdata.getFileContent());
String folderPath = getServletContext().getRealPath("/metafile/");
File folder = new File(folderPath);
if (!folder.exists()) {
folder.mkdirs();
}
outFile = new XMLWriter(new FileOutputStream(folderPath + File.separator + mdata.getMetaSet()+ ".xml"));
outFile.write(outerdoc);
Document doc = readDoc(folderPath + File.separator + mdata.getMetaSet()+ ".xml");
Element root = doc.getRootElement();
//读取元数据文件对应的节点信息
Map infoMap = getMetaFileInfo(folderPath + File.separator + "config.xml");
//遍历元数据文件所有的子节点
viewAllElement(root);
String mContent = mdata.getFileContent();
for (Node node : metaList) {
if (StringUtils.isBlank(node.getText())) {
mContent = mContent.replace("</"+node.getName()+">", "<br/>").replace("<"+node.getName()+">",
"<b>" + infoMap.get(node.getName()).toString() + "<br/>" + "</b>").replace(" ", " ");
} else {
mContent = mContent.replace("</"+node.getName()+">", "<br/>").replace("<"+node.getName()+">",
"<b>" + infoMap.get(node.getName()).toString() + ": " + "</b>").replace(" ", " ");
}
}
getResponse().setCharacterEncoding("UTF-8");
getResponse().setContentType("text/html");
render(getResponse(), "<font size='2'>"+mContent+"</font>", "text/html");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outFile != null) {
outFile.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
render(getResponse(), "暂无数据", "text/html");
}
return null;
}
/**
* 读取元数据文件对应的节点信息
* @param filePath 文件
*/
@SuppressWarnings("unchecked")
private Map getMetaFileInfo(String filePath) {
Document document = readDoc(filePath);
List nodeList = document.selectNodes("//item");
Iterator iter = nodeList.iterator();
Map fileInfoMap = new HashMap();
while(iter.hasNext()){
Element element=(Element)iter.next();
Iterator itemNameIter=element.elementIterator("itemName");
Iterator aliasIter=element.elementIterator("itemAlias1");
Element itemNameElement = null;
Element aliasElement = null;
while(itemNameIter.hasNext()){
itemNameElement=(Element)itemNameIter.next();
}
while(aliasIter.hasNext()){
aliasElement=(Element)aliasIter.next();
}
//logger.info(itemNameElement.getText() + " : " + aliasElement.getText());
fileInfoMap.put(itemNameElement.getText(), aliasElement.getText());
}
return fileInfoMap;
}
/**
* 取得document对象
* @param filename 文件名
*/
private Document readDoc(String filename) {
SAXReader reader = new SAXReader();
try {
return reader.read(new FileInputStream(new File(filename)));
} catch (DocumentException e) {
e.printStackTrace();
throw new ApplicationException(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
throw new ApplicationException(e.getMessage());
}
}
/**
* 遍历所有节点
* @param element
*/
@SuppressWarnings("unchecked")
public void viewAllElement(Element element) {
for ( int i = 0, size = element.nodeCount(); i < size; i++ ) {
Node node = element.node(i);
if ( node instanceof Element ) {
viewAllElement( (Element) node );
//logger.info("节点名称:" + node.getName() + " 节点内容:" + node.getText());
metaList.add(node);
}
}
}
多想,多试,使用多种方法实现。。。