這是讀取xml文件的java程序,我調(diào)試好的。采用的是dom方式讀取xml文件到Vector中。
?1
package
?src;
?2
import
?java.io.
*
;
?3
import
?java.util.Vector;
?4
import
?javax.xml.parsers.
*
;
?5
import
?org.w3c.dom.
*
;
?6
public
?
class
?readxml?
{
?7
static
?Document?document;
?8
private
?
boolean
?validating;
?9
public
?readxml()?
{
10
}
11
public
?Vector?toRead(String?filename)?
{
12
Vector?title
=
new
?Vector();
13
Vector?content
=
new
?Vector();
14
String?myStr
=
new
?String();
15
try
?
{
16
DocumentBuilderFactory?factory?
=
?DocumentBuilderFactory.newInstance();
17
factory.setValidating(validating);
18
DocumentBuilder?builder?
=
?factory.newDocumentBuilder();
19
document?
=
?builder.parse(
new
?File(filename));
20
document.getDocumentElement().normalize();
21
Node?node?
=
?document.getFirstChild();
22
NodeList?list?
=
?node.getChildNodes();
23
for
?(
int
?i?
=
?
0
;?i?
<
?list.getLength();?i
++
)?
{
24
Node?nodeitm?
=
?list.item(i);
25
if
?(nodeitm.getNodeName().equals(
"
Title
"
))?
{
26
myStr
=
nodeitm.getFirstChild().getNodeValue();
27
title.addElement(myStr);
//
getFirstChild()
28
}
29
if
?(nodeitm.getNodeName().equals(
"
Content
"
))?
{
30
myStr
=
nodeitm.getFirstChild().getNodeValue();
31
content.addElement(myStr);
32
}
33
}
34
}
?
catch
?(Exception?exp)?
{
35
exp.printStackTrace();
36
return
?
null
;
37
}
38
Vector?all
=
new
?Vector();
39
all.add(title);
40
all.add(content);?
41
return
?all;
42
}
43
44
public
?
static
?
void
?main(String[]?args)?
{
45
Vector?A;
46
readxml?my?
=
?
new
?readxml();
47
A?
=
?my.toRead(
"
file.xml
"
);
48
for
?(
int
?i?
=
?
0
;?i?
<
?A.size();?i
++
)?
{
49
System.out.println(A.elementAt(i));
50
}
51
}
52
}
53
package
?src;?2
import
?java.io.
*
;?3
import
?java.util.Vector;?4
import
?javax.xml.parsers.
*
;?5
import
?org.w3c.dom.
*
;?6
public
?
class
?readxml?
{?7
static
?Document?document;?8
private
?
boolean
?validating;?9
public
?readxml()?
{10
}
11
public
?Vector?toRead(String?filename)?
{12
Vector?title
=
new
?Vector();13
Vector?content
=
new
?Vector();14
String?myStr
=
new
?String();15
try
?
{16
DocumentBuilderFactory?factory?
=
?DocumentBuilderFactory.newInstance();17
factory.setValidating(validating);18
DocumentBuilder?builder?
=
?factory.newDocumentBuilder();19
document?
=
?builder.parse(
new
?File(filename));20
document.getDocumentElement().normalize();21
Node?node?
=
?document.getFirstChild();22
NodeList?list?
=
?node.getChildNodes();23
for
?(
int
?i?
=
?
0
;?i?
<
?list.getLength();?i
++
)?
{24
Node?nodeitm?
=
?list.item(i);25
if
?(nodeitm.getNodeName().equals(
"
Title
"
))?
{26
myStr
=
nodeitm.getFirstChild().getNodeValue();27
title.addElement(myStr);
//
getFirstChild()
28
}
29
if
?(nodeitm.getNodeName().equals(
"
Content
"
))?
{30
myStr
=
nodeitm.getFirstChild().getNodeValue();31
content.addElement(myStr);32
}
33
}
34
}
?
catch
?(Exception?exp)?
{35
exp.printStackTrace();36
return
?
null
;37
}
38
Vector?all
=
new
?Vector();39
all.add(title);40
all.add(content);?41
return
?all;42
}
43
44
public
?
static
?
void
?main(String[]?args)?
{45
Vector?A;46
readxml?my?
=
?
new
?readxml();47
A?
=
?my.toRead(
"
file.xml
"
);48
for
?(
int
?i?
=
?
0
;?i?
<
?A.size();?i
++
)?
{49
System.out.println(A.elementAt(i));50
}
51
}
52
}
53
這是將xml寫入文件。其中,transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312")關(guān)系到編碼問題,非常重要。
?1
import
?org.w3c.dom.
*
;
?2
import
?javax.xml.parsers.
*
;
?3
import
?javax.xml.transform.
*
;
?4
import
?javax.xml.transform.dom.DOMSource;
?5
import
?javax.xml.transform.stream.StreamResult;
?6
import
?java.io.
*
;
?7
public
?
class
?writexml?
{
?8
private
?Document?document;
?9
private
?String?filename;
10
11
public
?writexml(String?name)?
throws
?ParserConfigurationException
{
12
filename
=
name;
13
DocumentBuilderFactory?factory
=
DocumentBuilderFactory.newInstance();
14
DocumentBuilder?builder
=
factory.newDocumentBuilder();
15
document
=
builder.newDocument();
16
}
17
public
?
void
?toWrite(String?mytitle,String?mycontent)
{
18
Element?root
=
document.createElement(
"
WorkShop
"
);
19
document.appendChild(root);
20
Element?title
=
document.createElement(
"
Title
"
);
21
title.appendChild(document.createTextNode(mytitle));
22
root.appendChild(title);
23
Element?content
=
document.createElement(
"
Content
"
);
24
content.appendChild(document.createTextNode(mycontent));
25
root.appendChild(content);
26
}
27
public
?
void
?toSave()
{
28
try
{
29
TransformerFactory?tf
=
TransformerFactory.newInstance();
30
Transformer?transformer
=
tf.newTransformer();
31
DOMSource?source
=
new
?DOMSource(document);
32
transformer.setOutputProperty(OutputKeys.ENCODING,
"
GB2312
"
);
33
transformer.setOutputProperty(OutputKeys.INDENT,
"
yes
"
);
34
PrintWriter?pw
=
new
?PrintWriter(
new
?FileOutputStream(filename));
35
StreamResult?result
=
new
?StreamResult(pw);
36
transformer.transform(source,result);
37
}
38
catch
(TransformerException?mye)
{
39
mye.printStackTrace();
40
}
41
catch
(IOException?exp)
{
42
exp.printStackTrace();
43
}
44
}
45
public
?
static
?
void
?main(String?args[])
{
46
try
{
47
writexml?myxml
=
new
?writexml(
"
file.xml
"
);
48
myxml.toWrite(
"
中文題目
"
,
"
中文內(nèi)容
"
);
49
myxml.toSave();
50
System.out.print(
"
Your?writing?is?successful.
"
);
51
}
52
catch
(ParserConfigurationException?exp)
{
53
exp.printStackTrace();
54
System.out.print(
"
Your?writing?is?failed.
"
);
55
}
?
56
}
57
}
58
import
?org.w3c.dom.
*
;?2
import
?javax.xml.parsers.
*
;?3
import
?javax.xml.transform.
*
;?4
import
?javax.xml.transform.dom.DOMSource;?5
import
?javax.xml.transform.stream.StreamResult;?6
import
?java.io.
*
;?7
public
?
class
?writexml?
{?8
private
?Document?document;?9
private
?String?filename;10
11
public
?writexml(String?name)?
throws
?ParserConfigurationException
{12
filename
=
name;13
DocumentBuilderFactory?factory
=
DocumentBuilderFactory.newInstance();14
DocumentBuilder?builder
=
factory.newDocumentBuilder();15
document
=
builder.newDocument();16
}
17
public
?
void
?toWrite(String?mytitle,String?mycontent)
{18
Element?root
=
document.createElement(
"
WorkShop
"
);19
document.appendChild(root);20
Element?title
=
document.createElement(
"
Title
"
);21
title.appendChild(document.createTextNode(mytitle));22
root.appendChild(title);23
Element?content
=
document.createElement(
"
Content
"
);24
content.appendChild(document.createTextNode(mycontent));25
root.appendChild(content);26
}
27
public
?
void
?toSave()
{28
try
{29
TransformerFactory?tf
=
TransformerFactory.newInstance();30
Transformer?transformer
=
tf.newTransformer();31
DOMSource?source
=
new
?DOMSource(document);32
transformer.setOutputProperty(OutputKeys.ENCODING,
"
GB2312
"
);33
transformer.setOutputProperty(OutputKeys.INDENT,
"
yes
"
);34
PrintWriter?pw
=
new
?PrintWriter(
new
?FileOutputStream(filename));35
StreamResult?result
=
new
?StreamResult(pw);36
transformer.transform(source,result);37
}
38
catch
(TransformerException?mye)
{39
mye.printStackTrace();40
}
41
catch
(IOException?exp)
{42
exp.printStackTrace();43
}
44
}
45
public
?
static
?
void
?main(String?args[])
{46
try
{47
writexml?myxml
=
new
?writexml(
"
file.xml
"
);48
myxml.toWrite(
"
中文題目
"
,
"
中文內(nèi)容
"
);49
myxml.toSave();50
System.out.print(
"
Your?writing?is?successful.
"
);51
}
52
catch
(ParserConfigurationException?exp)
{53
exp.printStackTrace();54
System.out.print(
"
Your?writing?is?failed.
"
);55
}
?56
}
57
}
58
?
?


