-
import
import
putStream;
import
tFoundException;
import
tputStream;
import
ption;
import
tream;
import
import
p;
import
or;
import
import
import
import
ntBuilder;
import
ntBuilderFactory;
import
ormer;
import
ormerFactory;
import
rce;
import
Result;
import
nt;
import
t;
import
st;
import
64Encoder;
public
class
XmlWord
{
private
Map
dataMap
=
new
HashMap
public
Map
{
return
dataMap
;
}
public
void
setDataMap(Map
dataMap
)
{
this
.
p>
dataMap
=
dataMap
;
}
/**
*
设置标识值
*
@param
tagList
标识
*
@param
dataList
数据
*
@param
dataMap
*/
public
void
setData(List
tagList
,
List
dataList
)
{
Iterator
it1
=
tagList
.iterator();
Iterator
it2
=
dataList
.iterator();
while
(
it1
.hasNext())
{
this
.
dataMap
.put(
it1
.next(),
it2
.next());
}
}
/**
*
载入一个
xml
文档
*
@param
filename
文件路径
*
@return
成功返回
Document
对象,失败返回
null<
/p>
*/
public
Document LoadXml(String
filename
)
{
Document
doc
=
null
;
try
{
DocumentBuilderFactory
factory
=
Documen
tBuilderFactory.
newInstance
(
);
DocumentBuilder
builder
=
factory
.newDocumentBuilder();
doc
=
builder
.parse(
new
File(
filename
));
System.
out
.println(
doc
);
}
catch
(Exception
e
)
{
System.
out
.println(
载入
xml
文件时出错
);
e
.printStackTrace();
}
return
doc
;
}
/**
*
图片转码
*
@return
返回图片
base64
字符串
*
@throws
Exception
*/
public
String
getImageStr(String
imgFile
)
{
InputStream
in
=
null
;
BASE64Encoder
encoder
=
null
;
byte
[]
data
=
null
;
try
{
in
=
new
FileInputStream(
imgFile
);
}
catch
(FileNotFoundException
e
)
{
System.
out
.println(
文件没找到!
< br>
);
e
.printStackTrace();
}
try
{
data
=
new
byte
[
in
.available()];
in
.re
ad(
data
);
in
.close();
}
catch
(IOException
e
)
{
e
.printStackTrace();
}
encoder
=
new
BASE64Encoder();
return
p>
encoder
.encode(
data
);
}
/**
* doc2XmlFile
*
将
Document<
/p>
对象保存为一个
xml
文件
*
@return
true:
保存成功
flase:
失败
*
@param
filename
保存的文件名
*
@param
document
需要保存的
document
对象
*/
public
boolean
doc2XmlFile(Document
document
, String
filename
)
{
boolean
flag
=
true
;
try
{
TransformerFactory
transFactory
=
Tr
ansformerFactory.
newInstance
();
Transformer
transformer
=
tra
nsFactory
.newTransformer();
DOMSource
source
=
new
DOMSource();
source
.setNode(
do
cument
);
StreamResult
result
=
new
StreamResult();
FileOutputStream
fileOutputStream
=
new
FileOutputStr
eam(
filename
);
result
.setOutputStream(
fileOutputStream
);
transformer
.transform(
source
,
result
);
fileOutputStream
.close();
}
catch
(Exception
ex
)
{
flag
=
false
;
ex
.printStackTrace();
}
-
-
-
-
-
-
-
-
-
上一篇:浅谈宁海饮食习俗
下一篇:“以小见大”的杰作——浅谈莫泊桑《羊脂球》的创作手法