dom4j解析xml问题

sunjigen803 上海涌杰信息科技 码农  2010-10-25 11:02:54

package com.huateng.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.util.List;


import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class ExtraUtil {
Document doc = null;
Element rootElement = null;
String label = null;
String editor = null;
boolean createLabel = false;
boolean createEditor = false;
public ExtraUtil(Document doc,String label,String editor){
this.doc = doc;
this.rootElement = doc.getRootElement();
this.label = label;
this.editor = editor;
this.init();
}
public void init() {
if(this.label!=null&&!this.label.equals("")){
this.createLabel = true;
}
if(this.editor!=null&&!this.editor.equals("")){
this.createEditor = true;
}
//如果一个label标签都没有,添加FieldLabels
if(this.createLabel){
if(this.rootElement.selectNodes("FieldLabels").size()==0){
this.rootElement.addElement("FieldLabels");
}
}
//如果一个editor标签都没有,添加Editors
if(this.createEditor){
if(this.rootElement.selectNodes("Editors").size()==0){
this.rootElement.addElement("Editors");
}
}
}
public static Document read(String fileName) throws MalformedURLException, DocumentException {
SAXReader reader = new SAXReader();
Document document = reader.read(new File(fileName));
System.out.println(document.getStringValue());
return document;
}
public static void main(String[] args) {
//对应xml的位置
String docPath = "D:\\work\\SICS\\WEB-INF\\ExtraUtilTest.xml";
//对应的dataset名字
String datasetName = "GlobalData";
//要添加的label的后缀,如果不需要生成label=""即可
String label = "Label";
//要添加的editor的后缀
String editor = "";

try{
Document doc = ExtraUtil.read(docPath);
ExtraUtil extraUtil = new ExtraUtil(doc,label,editor);
//Element rootElement = doc.getRootElement();

Element datasetElement = extraUtil.getDataSetByName(datasetName);
if(datasetElement==null) throw new Exception("不存在的dataset");

extraUtil.createLabel(datasetElement);

//去掉换行

//转换为window格式

//写入原文件
XMLWriter output;
OutputFormat format = OutputFormat.createCompactFormat();
format.setEncoding("UTF-8");
//输出文件时定义已UTF-8形成文档
FileWriter fileWriter = new FileWriter(docPath);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(docPath),"UTF-8");
//out.write(doc.getStringValue());
//out.close();
output = new XMLWriter(fileWriter, format);
output.write(doc);
output.close();

}catch(Exception e){
e.printStackTrace();
}

}

public Element getDataSetByName(String datasetName) {
List datasets = ((Element)this.rootElement.selectNodes("Datasets").get(0)).selectNodes("Dataset");
for(int i=0;i<datasets.size();i++){
Element dataset = (Element)datasets.get(i);
Node datasetId = dataset.selectSingleNode("id");
if(datasetId.getText().equals(datasetName)){
return dataset;
}
}
return null;
}

public Element createLabel(Element datasetElement) {
Element fieldLabelsElement = null;
Element editorsElement = null;
String datasetName = datasetElement.selectSingleNode("id").getText();
if(this.createLabel) {
fieldLabelsElement = (Element)this.rootElement.selectNodes("FieldLabels").get(0);
}
if(this.createEditor){
editorsElement = (Element)this.rootElement.selectNodes("Editors").get(0);
}
List datasetFields = ((Element)datasetElement.selectNodes("Fields").get(0)).selectNodes("Field");

//getLabelOrEditorMaxIndex();
for(int i=0;i<datasetFields.size();i++){
String fieldName = ((Element)datasetFields.get(i)).selectSingleNode("name").getText();
if(fieldName.equals("AvailRecord")||fieldName.equals("sqlResend")||fieldName.equals("ibps_sql_id")){
continue;
}
if(this.createLabel){
Element fieldLabelElement = fieldLabelsElement.addElement("FieldLabel");
Element e1 = fieldLabelElement.addElement("dataField");
e1.setText(fieldName);
Element e2 = fieldLabelElement.addElement("dataset");
e2.setText(datasetName);
Element e3 = fieldLabelElement.addElement("id");
e3.setText(fieldName+this.label);
}

if(this.createEditor){
Element editorElement = editorsElement.addElement("Editor");
Element e4 = editorElement.addElement("dataField");
e4.setText(fieldName);
Element e5 = editorElement.addElement("dataset");
e5.setText(datasetName);
Element e6 = editorElement.addElement("id");
e6.setText(fieldName+this.editor);
Element e7 = editorElement.addElement("width");
e7.setText("200");
}
}
return null;
}
}


<?xml version="1.0" encoding="UTF-8"?>
<Extra><Datasets><Dataset><datasetType>custom</datasetType><id>GlobalData</id><Fields><Field><dataType>string</dataType><name>AvailRecord</name></Field><Field><dataType>string</dataType><defaultValue>00000001</defaultValue><label>SQL 上送报文用resend</label><name>sqlResend</name></Field><Field><defaultValue>3813_1</defaultValue><name>ibps_sql_id</name></Field><Field><dataType>date</dataType><label>原工作日期</label><name>SICS_080110_OWorkDate</name><size>10</size></Field><Field><dataType>string</dataType><dropDown>SICS_080110_InOutFlag</dropDown><label>提出提入标志</label><name>SICS_080110_InOutFlag</name><size>1</size></Field><Field><dataType>string</dataType><label>原交易发起机构</label><name>SICS_080110_O_Originato</name><size>12</size></Field><Field><dataType>string</dataType><dropDown>SICS_080110_TrnCode</dropDown><label>交易类型</label><name>SICS_080110_TrnCode</name><size>4</size></Field><Field><dataType>string</dataType><label>原交易系统参考号</label><mask>y</mask><maskErrorMessage>n</maskErrorMessage><name>SICS_080110_O_ID</name><size>16</size></Field></Fields></Dataset></Datasets><FieldLabels><FieldLabel><dataField>SICS_080110_OWorkDate</dataField><dataset>GlobalData</dataset><id>fldlabel1_SICS_080110_OWorkDate</id></FieldLabel></FieldLabels><Editors><Editor><dataField>SICS_080110_OWorkDate</dataField><dataset>GlobalData</dataset><id>SICS_080110_OWorkDateEditor</id><width>200</width></Editor></Editors></Extra>



java读取xml时,输出的中文乱码,请高手看看怎么解决
...全文
192 点赞 收藏 4
写回复
4 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lijingzhong1987 2010-10-25
做个编码转换
回复
aoli444 2010-10-25
类似 str = new String(str.getBytes("GBK"), "ISO8859-1");
回复
witcheryne 2010-10-25
楼上正解..
回复
老紫竹 2010-10-25
涉及到的文件流,得指定编码字符集
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-10-25 11:02
社区公告
暂无公告