当前位置: 技术问答>java相关
1500分征XML学习心得, 资料集中(参与者均可得分,同开5贴)
来源: 互联网 发布时间:2015-07-12
本文导语: JSP, UTF-8,Java,类映射 | 这是用jdom import org.jdom.*; import org.jdom.output.*; import org.jdom.input.*; import java.io.*; public class TestJDOM { public static void main(String args[])throws Exception { //SAXBuilde...
JSP, UTF-8,Java,类映射
|
这是用jdom
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
public class TestJDOM
{
public static void main(String args[])throws Exception
{
//SAXBuilder builds a JDOM tree using SAX.
SAXBuilder sb = new SAXBuilder();
//从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了
Document doc = sb.build(new FileInputStream("exampleA.xml"));
//加入一条处理指令
ProcessingInstruction pi = new ProcessingInstruction ("xml-stylesheet","href="/tech-qa-java/greeting.css" type="text/css"");
doc.addContent(pi);
Element root = doc.getRootElement(); //得到根元素
java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
Element book = (Element)books.get(0); //得到第一个book元素
//为第一本书添加一条属性
Attribute a = new Attribute("hot","true");
book.setAttribute(a);
Element author = book.getChild("author"); //得到指定的字元素
author.setText("王五"); //将作者改为王五
//或 Text t = new Text("王五");book.addContent(t);
Element price = book.getChild("price"); //得到指定的字元素
price.setText(Float.toString(50.0f));
String indent = " ";
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,"gb2312");
outp.output(doc, new FileOutputStream("exampleB.xml"));
}
}
==================
exampleA.xml
Java编程入门
张三
2002-6-6
35.0
XML在Java中的应用
李四
2002-9-16
92.0
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import java.io.*;
public class TestJDOM
{
public static void main(String args[])throws Exception
{
//SAXBuilder builds a JDOM tree using SAX.
SAXBuilder sb = new SAXBuilder();
//从文件构造一个Document,因为XML文件中已经指定了编码,所以这里不必了
Document doc = sb.build(new FileInputStream("exampleA.xml"));
//加入一条处理指令
ProcessingInstruction pi = new ProcessingInstruction ("xml-stylesheet","href="/tech-qa-java/greeting.css" type="text/css"");
doc.addContent(pi);
Element root = doc.getRootElement(); //得到根元素
java.util.List books = root.getChildren(); //得到根元素所有子元素的集合
Element book = (Element)books.get(0); //得到第一个book元素
//为第一本书添加一条属性
Attribute a = new Attribute("hot","true");
book.setAttribute(a);
Element author = book.getChild("author"); //得到指定的字元素
author.setText("王五"); //将作者改为王五
//或 Text t = new Text("王五");book.addContent(t);
Element price = book.getChild("price"); //得到指定的字元素
price.setText(Float.toString(50.0f));
String indent = " ";
boolean newLines = true;
XMLOutputter outp = new XMLOutputter(indent,newLines,"gb2312");
outp.output(doc, new FileOutputStream("exampleB.xml"));
}
}
==================
exampleA.xml
Java编程入门
张三
2002-6-6
35.0
XML在Java中的应用
李四
2002-9-16
92.0
|
1 HERE'S THE ORIGINAL XML DOCUMENT (THE DTD HAS BEEN OMITTED):
AA
700
DFW
30MAR
0630A
ON TIME
0632A
C16
0644A
0632A
LGA
30MAR
1040A
ON TIME
1043A
D5
B
1043A
2 ADD THE PAGE DIRECTIVE
AA
700
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
29.
30. ON TIME
31.
32. D5
33. B
34. 1043A
35.
36.
37.
4 HERE'S OUR EXAMPLE REWRITTEN TO USE SUCH A BEAN.
1.
2.
3.
4.
5.
6. AA
7. 700
8.
9.
10. DFW
11. 30MAR
12.
13.
14.
15. ON TIME
16.
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
27. ON TIME
28.
29. D5
30. B
31. 1043A
32.
33.
34.
HERE'S OUR EXAMPLE REWRITTEN TO USE SUCH A BEAN.
1.
2.
3.
4.
5.
6. AA
7. 700
8.
9.
10. DFW
11. 30MAR
12.
13.
14.
15. ON TIME
16.
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
27. ON TIME
28.
29. D5
30. B
31. 1043A
32.
33.
34.
4HERE'S OUR EXAMPLE REWRITTEN TO USE SUCH A BEAN.
1.
2.
3.
4.
5.
6. AA
7. 700
8.
9.
10. DFW
11. 30MAR
12.
13.
14.
15. ON TIME
16.
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
27. ON TIME
28.
29. D5
30. B
31. 1043A
32.
33.
34.
4 HERE'S OUR EXAMPLE REWRITTEN TO USE SUCH A BEAN.
1.
2.
3.
4.
5.
6. AA
7. 700
8.
9.
10. DFW
11. 30MAR
12.
13.
14.
15. ON TIME
16.
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
27. ON TIME
28.
29. D5
30. B
31. 1043A
32.
33.
34.
AA
700
DFW
30MAR
0630A
ON TIME
0632A
C16
0644A
0632A
LGA
30MAR
1040A
ON TIME
1043A
D5
B
1043A
2 ADD THE PAGE DIRECTIVE
AA
700
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
29.
30. ON TIME
31.
32. D5
33. B
34. 1043A
35.
36.
37.
4 HERE'S OUR EXAMPLE REWRITTEN TO USE SUCH A BEAN.
1.
2.
3.
4.
5.
6. AA
7. 700
8.
9.
10. DFW
11. 30MAR
12.
13.
14.
15. ON TIME
16.
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
27. ON TIME
28.
29. D5
30. B
31. 1043A
32.
33.
34.
HERE'S OUR EXAMPLE REWRITTEN TO USE SUCH A BEAN.
1.
2.
3.
4.
5.
6. AA
7. 700
8.
9.
10. DFW
11. 30MAR
12.
13.
14.
15. ON TIME
16.
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
27. ON TIME
28.
29. D5
30. B
31. 1043A
32.
33.
34.
4HERE'S OUR EXAMPLE REWRITTEN TO USE SUCH A BEAN.
1.
2.
3.
4.
5.
6. AA
7. 700
8.
9.
10. DFW
11. 30MAR
12.
13.
14.
15. ON TIME
16.
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
27. ON TIME
28.
29. D5
30. B
31. 1043A
32.
33.
34.
4 HERE'S OUR EXAMPLE REWRITTEN TO USE SUCH A BEAN.
1.
2.
3.
4.
5.
6. AA
7. 700
8.
9.
10. DFW
11. 30MAR
12.
13.
14.
15. ON TIME
16.
18. C16
19. 0644A
20. 0632A
21.
22.
23. LGA
24. 30MAR
25.
26.
27. ON TIME
28.
29. D5
30. B
31. 1043A
32.
33.
34.
|
XMLForJava
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.*;
import com.objectspace.jgl.*;
import java.lang.reflect.*;
import org.w3c.dom.*;
import oracle.xml.parser.v2.*;
import java.util.Locale;
public class XMLForJava extends Object {
public XMLForJava() {}
//根据URL获取XMLDom对象
public XMLDocument getXMLDomByURL(/tech-qa-java/String URLString/index.html)throws Exception
{
DOMParser theParser = new DOMParser();
theParser.parse(URLString);
return theParser.getDocument();
}
//将String转换成XMLDom对象
public XMLDocument parseStringtoXML(String xmlString)throws Exception
{
XMLDocument dom = null;
DOMParser aparser = new DOMParser();
ByteArrayInputStream theStream = new ByteArrayInputStream(xmlString.getBytes());
aparser.setValidationMode(false);
aparser.parse( theStream );
dom = aparser.getDocument();
return dom;
}
//将Array转换成XML字符串
public String parseArrayToXML(com.objectspace.jgl.Array ObjArray,String XMLFileName)throws Exception
{
String responseIslandXML = "";
for(int i=0;i
import java.io.*;
import java.util.*;
import java.net.*;
import java.lang.*;
import com.objectspace.jgl.*;
import java.lang.reflect.*;
import org.w3c.dom.*;
import oracle.xml.parser.v2.*;
import java.util.Locale;
public class XMLForJava extends Object {
public XMLForJava() {}
//根据URL获取XMLDom对象
public XMLDocument getXMLDomByURL(/tech-qa-java/String URLString/index.html)throws Exception
{
DOMParser theParser = new DOMParser();
theParser.parse(URLString);
return theParser.getDocument();
}
//将String转换成XMLDom对象
public XMLDocument parseStringtoXML(String xmlString)throws Exception
{
XMLDocument dom = null;
DOMParser aparser = new DOMParser();
ByteArrayInputStream theStream = new ByteArrayInputStream(xmlString.getBytes());
aparser.setValidationMode(false);
aparser.parse( theStream );
dom = aparser.getDocument();
return dom;
}
//将Array转换成XML字符串
public String parseArrayToXML(com.objectspace.jgl.Array ObjArray,String XMLFileName)throws Exception
{
String responseIslandXML = "";
for(int i=0;i
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。