java html解析成xml文件_Java解析XML文件

转:

2017年12月27日 09:47:29 zflovecf 阅读数:35152

1.DOM方式解析XML

Dom解析是将xml文件全部载入到内存,组装成一颗dom树,然后通过节点以及节点之间的关系来解析xml文件,与平台无关,java提供的一种基础的解析XML文件的API,理解较简单,但是由于整个文档都需要载入内存,不适用于文档较大时。

2.SAX方式解析XML

基于事件驱动,逐条解析,适用于只处理xml数据,不易编码,而且很难同时访问同一个文档中的多处不同数据

3.JDOM方式解析XML

简化与XML的交互并且比使用DOM实现更快,仅使用具体类而不使用接口因此简化了API,并且易于使用

4.DOM4j方式解析XML

JDOM的一种智能分支,功能较强大,建议熟练使用

下面给出例子:

books.xml

冰与火之歌

乔治马丁

2014

89

安徒生童话

安徒生

2004

77

think think think

aaa

1997

100

冰与火之歌

乔治马丁

2014

89

安徒生童话

安徒生

2004

77

think think think

aaa

1997

100

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

bean类:Book.java

public class Book {

/**

* @author lune

*/

private int id;

private String name;

private String author;

private int year;

private double price;

/**

* @return the id

*/

public int getId() {

return id;

}

/**

* @param id the id to set

*/

public void setId(int id) {

this.id = id;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the author

*/

public String getAuthor() {

return author;

}

/**

* @param author the author to set

*/

public void setAuthor(String author) {

this.author = author;

}

/**

* @return the year

*/

public int getYear() {

return year;

}

/**

* @param year the year to set

*/

public void setYear(int year) {

this.year = year;

}

/**

* @return the price

*/

public double getPrice() {

return price;

}

/**

* @param price the price to set

*/

public void setPrice(double price) {

this.price = price;

}

@Override

public String toString() {

return “Book [id=” + id + “, name=” + name + “, author=” + author + “, year=” + year + “, price=” + price + “]”;

}

}

public class Book {

/**

* @author lune

*/

private int id;

private String name;

private String author;

private int year;

private double price;

/**

* @return the id

*/

public int getId() {

return id;

}

/**

* @param id the id to set

*/

public void setId(int id) {

this.id = id;

}

/**

* @return the name

*/

public String getName() {

return name;

}

/**

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/**

* @return the author

*/

public String getAuthor() {

return author;

}

/**

* @param author the author to set

*/

public void setAuthor(String author) {

this.author = author;

}

/**

* @return the year

*/

public int getYear() {

return year;

}

/**

* @param year the year to set

*/

public void setYear(int year) {

this.year = year;

}

/**

* @return the price

*/

public double getPrice() {

return price;

}

/**

* @param price the price to set

*/

public void setPrice(double price) {

this.price = price;

}

@Override

public String toString() {

return "Book [id=" + id + ", name=" + name + ", author=" + author + ", year=" + year + ", price=" + price + "]";

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

1.DOM方式解析XML

import java.util.ArrayList;

import java.util.List;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.NodeList;

import com.lune.bean.Book;

/**

* 用DOM方式读取xml文件

* @author lune

*/

public class ReadxmlByDom {

private static DocumentBuilderFactory dbFactory = null;

private static DocumentBuilder db = null;

private static Document document = null;

private static List books = null;

static{

try {

dbFactory = DocumentBuilderFactory.newInstance();

db = dbFactory.newDocumentBuilder();

} catch (ParserConfigurationException e) {

e.printStackTrace();

}

}

public static List getBooks(String fileName) throws Exception{

//将给定 URI 的内容解析为一个 XML 文档,并返回Document对象

document = db.parse(fileName);

//按文档顺序返回包含在文档中且具有给定标记名称的所有 Element 的 NodeList

NodeList bookList = document.getElementsByTagName(”book”);

books = new ArrayList();

//遍历books

for(int i=0;i

Book book = new Book();

//获取第i个book结点

org.w3c.dom.Node node = bookList.item(i);

//获取第i个book的所有属性

NamedNodeMap namedNodeMap = node.getAttributes();

//获取已知名为id的属性值

String id = namedNodeMap.getNamedItem(”id”).getTextContent();//System.out.println(id);

book.setId(Integer.parseInt(id));

//获取book结点的子节点,包含了Test类型的换行

NodeList cList = node.getChildNodes();//System.out.println(cList.getLength());9

//将一个book里面的属性加入数组

ArrayList contents = new ArrayList<>();

for(int j=1;j

org.w3c.dom.Node cNode = cList.item(j);

String content = cNode.getFirstChild().getTextContent();

contents.add(content);

//System.out.println(contents);

}

book.setName(contents.get(0));

book.setAuthor(contents.get(1));

book.setYear(Integer.parseInt(contents.get(2)));

book.setPrice(Double.parseDouble(contents.get(3)));

books.add(book);

}

return books;

}

public static void main(String args[]){

String fileName = ”src/res/books.xml”;

try {

List list = ReadxmlByDom.getBooks(fileName);

for(Book book :list){

System.out.println(book);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

import java.util.ArrayList;

import java.util.List;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.NodeList;

import com.lune.bean.Book;

/**

* 用DOM方式读取xml文件

* @author lune

*/

public class ReadxmlByDom {

private static DocumentBuilderFactory dbFactory = null;

private static DocumentBuilder db = null;

private static Document document = null;

private static List books = null;

static{

try {

dbFactory = DocumentBuilderFactory.newInstance();

db = dbFactory.newDocumentBuilder();

} catch (ParserConfigurationException e) {

e.printStackTrace();

}

}

public static List getBooks(String fileName) throws Exception{

//将给定 URI 的内容解析为一个 XML 文档,并返回Document对象

document = db.parse(fileName);

//按文档顺序返回包含在文档中且具有给定标记名称的所有 Element 的 NodeList

NodeList bookList = document.getElementsByTagName("book");

books = new ArrayList();

//遍历books

for(int i=0;i

Book book = new Book();

//获取第i个book结点

org.w3c.dom.Node node = bookList.item(i);

//获取第i个book的所有属性

NamedNodeMap namedNodeMap = node.getAttributes();

//获取已知名为id的属性值

String id = namedNodeMap.getNamedItem("id").getTextContent();//System.out.println(id);

book.setId(Integer.parseInt(id));

//获取book结点的子节点,包含了Test类型的换行

NodeList cList = node.getChildNodes();//System.out.println(cList.getLength());9

//将一个book里面的属性加入数组

ArrayList contents = new ArrayList<>();

for(int j=1;j

org.w3c.dom.Node cNode = cList.item(j);

String content = cNode.getFirstChild().getTextContent();

contents.add(content);

//System.out.println(contents);

}

book.setName(contents.get(0));

book.setAuthor(contents.get(1));

book.setYear(Integer.parseInt(contents.get(2)));

book.setPrice(Double.parseDouble(contents.get(3)));

books.add(book);

}

return books;

}

public static void main(String args[]){

String fileName = "src/res/books.xml";

try {

List list = ReadxmlByDom.getBooks(fileName);

for(Book book :list){

System.out.println(book);

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

2.SAX方式解析XML

需要自定义DefaultHandler处理器

import java.util.ArrayList;

import java.util.List;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

import com.lune.bean.Book;

/**

* 用SAX解析xml文件时需要的handler

* @author lune

*/

public class SAXParseHandler extends DefaultHandler {

private List list;         //存放解析到的book数组

private Book book;               //存放当前解析的book

private String content = null;   //存放当前节点值

/**

* 开始解析xml文档时调用此方法

*/

@Override

public void startDocument() throws SAXException {

super.startDocument();

System.out.println(”开始解析xml文件”);

list = new ArrayList();

}

/**

* 文档解析完成后调用此方法

*/

@Override

public void endDocument() throws SAXException {

super.endDocument();

System.out.println(”xml文件解析完毕”);

}

/**

* 开始解析节点时调用此方法

*/

@Override

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

super.startElement(uri, localName, qName, attributes);

//当节点名为book时,获取book的属性id

if(qName.equals(“book”)){

book = new Book();

String id = attributes.getValue(”id”);//System.out.println(“id值为”+id);

book.setId(Integer.parseInt(id));

}

}

/**

*节点解析完毕时调用此方法

*

*@param qName 节点名

*/

@Override

public void endElement(String uri, String localName, String qName) throws SAXException {

super.endElement(uri, localName, qName);

if(qName.equals(“name”)){

book.setName(content);

//System.out.println(“书名”+content);

}else if(qName.equals(“author”)){

book.setAuthor(content);

//  System.out.println(“作者”+content);

}else if(qName.equals(“year”)){

book.setYear(Integer.parseInt(content));

//  System.out.println(“年份”+content);

}else if(qName.equals(“price”)){

book.setPrice(Double.parseDouble(content));

//  System.out.println(“价格”+content);

}else if(qName.equals(“book”)){         //当结束当前book解析时,将该book添加到数组后置为空,方便下一次book赋值

list.add(book);

book = null;

}

}

/**

* 此方法用来获取节点的值

*/

@Override

public void characters(char[] ch, int start, int length) throws SAXException {

super.characters(ch, start, length);

content = new String(ch, start, length);

//收集不为空白的节点值

//      if(!content.trim().equals(“”)){

//          System.out.println(“节点值为:”+content);

//      }

}

public List getBooks(){

return list;

}

}

import java.util.ArrayList;

import java.util.List;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

import com.lune.bean.Book;

/**

* 用SAX解析xml文件时需要的handler

* @author lune

*/

public class SAXParseHandler extends DefaultHandler {

private List list; //存放解析到的book数组

private Book book; //存放当前解析的book

private String content = null; //存放当前节点值

/**

* 开始解析xml文档时调用此方法

*/

@Override

public void startDocument() throws SAXException {

super.startDocument();

System.out.println("开始解析xml文件");

list = new ArrayList();

}

/**

* 文档解析完成后调用此方法

*/

@Override

public void endDocument() throws SAXException {

super.endDocument();

System.out.println("xml文件解析完毕");

}

/**

* 开始解析节点时调用此方法

*/

@Override

public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

super.startElement(uri, localName, qName, attributes);

//当节点名为book时,获取book的属性id

if(qName.equals("book")){

book = new Book();

String id = attributes.getValue("id");//System.out.println("id值为"+id);

book.setId(Integer.parseInt(id));

}

}

/**

*节点解析完毕时调用此方法

*

*@param qName 节点名

*/

@Override

public void endElement(String uri, String localName, String qName) throws SAXException {

super.endElement(uri, localName, qName);

if(qName.equals("name")){

book.setName(content);

//System.out.println("书名"+content);

}else if(qName.equals("author")){

book.setAuthor(content);

// System.out.println("作者"+content);

}else if(qName.equals("year")){

book.setYear(Integer.parseInt(content));

// System.out.println("年份"+content);

}else if(qName.equals("price")){

book.setPrice(Double.parseDouble(content));

// System.out.println("价格"+content);

}else if(qName.equals("book")){ //当结束当前book解析时,将该book添加到数组后置为空,方便下一次book赋值

list.add(book);

book = null;

}

}

/**

* 此方法用来获取节点的值

*/

@Override

public void characters(char[] ch, int start, int length) throws SAXException {

super.characters(ch, start, length);

content = new String(ch, start, length);

//收集不为空白的节点值

// if(!content.trim().equals("")){

// System.out.println("节点值为:"+content);

// }

}

public List getBooks(){

return list;

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

import java.io.IOException;

import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.ParserFactory;

import com.lune.bean.Book;

import com.lune.handler.SAXParseHandler;

/**

* 用SAX方式读取xml文件

* @author lune

*/

public class ReadXmlBySAX {

private static List books = null;

private  SAXParserFactory sParserFactory = null;

private  SAXParser parser = null;

public List getBooks(String fileName) throws Exception{

SAXParserFactory sParserFactory = SAXParserFactory.newInstance();

SAXParser parser = sParserFactory.newSAXParser();

SAXParseHandler handler = new SAXParseHandler();

parser.parse(fileName, handler);

return handler.getBooks();

}

/**

* @param args

*/

public static void main(String[] args) {

try {

books = new ReadXmlBySAX().getBooks(“src/res/books.xml”);

for(Book book:books){

System.out.println(book);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

import java.io.IOException;

import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.ParserFactory;

import com.lune.bean.Book;

import com.lune.handler.SAXParseHandler;

/**

* 用SAX方式读取xml文件

* @author lune

*/

public class ReadXmlBySAX {

private static List books = null;

private SAXParserFactory sParserFactory = null;

private SAXParser parser = null;

public List getBooks(String fileName) throws Exception{

SAXParserFactory sParserFactory = SAXParserFactory.newInstance();

SAXParser parser = sParserFactory.newSAXParser();

SAXParseHandler handler = new SAXParseHandler();

parser.parse(fileName, handler);

return handler.getBooks();

}

/**

* @param args

*/

public static void main(String[] args) {

try {

books = new ReadXmlBySAX().getBooks("src/res/books.xml");

for(Book book:books){

System.out.println(book);

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

3.JDOM方式解析XML

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.jdom2.JDOMException;

import org.jdom2.input.SAXBuilder;

import com.lune.bean.Book;

import org.jdom2.*;

/**

* 用JDOM方式读取xml文件

* @author lune

*/

public class ReadXMLByJDom {

private List books = null;

private Book book = null;

public List getBooks(String fileName){

SAXBuilder saxBuilder = new SAXBuilder();

try {

Document document = saxBuilder.build(new FileInputStream(fileName));

//获取根节点bookstore

Element rootElement = document.getRootElement();

//获取根节点的子节点,返回子节点的数组

List bookList = rootElement.getChildren();

books = new ArrayList();

for(Element bookElement : bookList){

book = new Book();

//获取bookElement的属性

List bookAttributes = bookElement.getAttributes();

for(Attribute attribute : bookAttributes){

if(attribute.getName().equals(“id”)){

String id = attribute.getValue(); //System.out.println(id);

book.setId(Integer.parseInt(id));

}

}

//获取bookElement的子节点

List children = bookElement.getChildren();

for(Element child : children){

if(child.getName().equals(“name”)){

String name = child.getValue();//System.out.println(name);

book.setName(name);

}else if(child.getName().equals(“author”)){

String author = child.getValue();

book.setAuthor(author);//System.out.println(author);

}else if(child.getName().equals(“year”)){

String year = child.getValue();

book.setYear(Integer.parseInt(year));

}else if(child.getName().equals(“price”)){

String price = child.getValue();

book.setPrice(Double.parseDouble(price));

}

}

books.add(book);

book = null;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (JDOMException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return books;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

String fileName = ”src/res/books.xml”;

List books= new ReadXMLByJDom().getBooks(fileName);

for(Book book : books){

System.out.println(book);

}

}

}

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.jdom2.JDOMException;

import org.jdom2.input.SAXBuilder;

import com.lune.bean.Book;

import org.jdom2.*;

/**

* 用JDOM方式读取xml文件

* @author lune

*/

public class ReadXMLByJDom {

private List books = null;

private Book book = null;

public List getBooks(String fileName){

SAXBuilder saxBuilder = new SAXBuilder();

try {

Document document = saxBuilder.build(new FileInputStream(fileName));

//获取根节点bookstore

Element rootElement = document.getRootElement();

//获取根节点的子节点,返回子节点的数组

List bookList = rootElement.getChildren();

books = new ArrayList();

for(Element bookElement : bookList){

book = new Book();

//获取bookElement的属性

List bookAttributes = bookElement.getAttributes();

for(Attribute attribute : bookAttributes){

if(attribute.getName().equals("id")){

String id = attribute.getValue(); //System.out.println(id);

book.setId(Integer.parseInt(id));

}

}

//获取bookElement的子节点

List children = bookElement.getChildren();

for(Element child : children){

if(child.getName().equals("name")){

String name = child.getValue();//System.out.println(name);

book.setName(name);

}else if(child.getName().equals("author")){

String author = child.getValue();

book.setAuthor(author);//System.out.println(author);

}else if(child.getName().equals("year")){

String year = child.getValue();

book.setYear(Integer.parseInt(year));

}else if(child.getName().equals("price")){

String price = child.getValue();

book.setPrice(Double.parseDouble(price));

}

}

books.add(book);

book = null;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (JDOMException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return books;

}

public static void main(String[] args) {

// TODO Auto-generated method stub

String fileName = "src/res/books.xml";

List books= new ReadXMLByJDom().getBooks(fileName);

for(Book book : books){

System.out.println(book);

}

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

4.DOM4j方式解析XML

import java.io.File;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import com.lune.bean.Book;

/**

* 用DOM4J方法读取xml文件

* @author lune

*/

public class ReadXMLByDom4j {

private List bookList = null;

private Book book = null;

public List getBooks(File file){

SAXReader reader = new SAXReader();

try {

Document document = reader.read(file);

Element bookstore = document.getRootElement();

Iterator storeit = bookstore.elementIterator();

bookList = new ArrayList();

while(storeit.hasNext()){

book = new Book();

Element bookElement = (Element) storeit.next();

//遍历bookElement的属性

List attributes = bookElement.attributes();

for(Attribute attribute : attributes){

if(attribute.getName().equals(“id”)){

String id = attribute.getValue();//System.out.println(id);

book.setId(Integer.parseInt(id));

}

}

Iterator bookit = bookElement.elementIterator();

while(bookit.hasNext()){

Element child = (Element) bookit.next();

String nodeName = child.getName();

if(nodeName.equals(“name”)){

//System.out.println(child.getStringValue());

String name = child.getStringValue();

book.setName(name);

}else if(nodeName.equals(“author”)){

String author = child.getStringValue();

book.setAuthor(author);

}else if(nodeName.equals(“year”)){

String year = child.getStringValue();

book.setYear(Integer.parseInt(year));

}else if(nodeName.equals(“price”)){

String price = child.getStringValue();

book.setPrice(Double.parseDouble(price));

}

}

bookList.add(book);

book = null;

}

} catch (DocumentException e) {

e.printStackTrace();

}

return bookList;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File(“src/res/books.xml”);

List bookList = new ReadXMLByDom4j().getBooks(file);

for(Book book : bookList){

System.out.println(book);

}

}

}

import java.io.File;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import com.lune.bean.Book;

/**

* 用DOM4J方法读取xml文件

* @author lune

*/

public class ReadXMLByDom4j {

private List bookList = null;

private Book book = null;

public List getBooks(File file){

SAXReader reader = new SAXReader();

try {

Document document = reader.read(file);

Element bookstore = document.getRootElement();

Iterator storeit = bookstore.elementIterator();

bookList = new ArrayList();

while(storeit.hasNext()){

book = new Book();

Element bookElement = (Element) storeit.next();

//遍历bookElement的属性

List attributes = bookElement.attributes();

for(Attribute attribute : attributes){

if(attribute.getName().equals("id")){

String id = attribute.getValue();//System.out.println(id);

book.setId(Integer.parseInt(id));

}

}

Iterator bookit = bookElement.elementIterator();

while(bookit.hasNext()){

Element child = (Element) bookit.next();

String nodeName = child.getName();

if(nodeName.equals("name")){

//System.out.println(child.getStringValue());

String name = child.getStringValue();

book.setName(name);

}else if(nodeName.equals("author")){

String author = child.getStringValue();

book.setAuthor(author);

}else if(nodeName.equals("year")){

String year = child.getStringValue();

book.setYear(Integer.parseInt(year));

}else if(nodeName.equals("price")){

String price = child.getStringValue();

book.setPrice(Double.parseDouble(price));

}

}

bookList.add(book);

book = null;

}

} catch (DocumentException e) {

e.printStackTrace();

}

return bookList;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("src/res/books.xml");

List bookList = new ReadXMLByDom4j().getBooks(file);

for(Book book : bookList){

System.out.println(book);

}

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

其中后两者需要导入外部jar包.

文章中的源码可在下面地址下载:

讲html文件转换成xml文件 本代码实现了将一个html文件转成xml文件 立即下载

相关推荐

Jtidy 将HTML转化成XML

利用Jtidy工具, 将HTML转化成XML,这里给出简单的转换方法。

java html转换xml_Html转XML

使用nekohtml进行转化源程序:html2xml.javaimport org.w3c.dom.Node;import org.w3c.dom.DocumentFragment;import org.w3c.dom.html.HTMLDocument;import org.xml.sax.InputSource;import org.apache.html.dom.HTMLDocumentIm...

weixin_36374843的博客 1222

html转换xml代码

这是我根据网上一些代码,自己改写的程序,完成html格式转换成为标准的xml格式,虽然转换后有一点小错误,但转换后文件能有浏览器打开并能用Dom解析

java html转换xml_Java HTML/XML - 如何将简单Java转换为XML

import javax.xml.bind.JAXBContext;import javax.xml.bind.Marshaller;import javax.xml.bind.annotation.XmlRootElement;public class Main {public static void main(String[] args) throws Exception {JAXBConte...

weixin_39827034的博客 333

html转xml小工具,包括源码

html转xml的小工具 包括源码 C#

地平线天工开物AI开发平台实战:从量化训练到模型部署的完整指南

本文提供了地平线天工开物(OpenExplorer)AI开发平台的实战指南,详细解析了从量化训练到模型部署的完整流程。文章重点对比了量化感知训练(QAT)与训练后量化(PTQ)两条核心路径的优劣与适用场景,并分享了图像分类模型PTQ部署的实战经验与常见问题调试技巧,旨在帮助开发者高效地将AI模型部署到地平线AI芯片上。

weixin_29046035的博客 465

java html解析成xml文件_Java HTML/XML - 如何使用DOM解析XML文件

import java.io.FileInputStream;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;p...

weixin_39676034的博客 90

【SIP基础】SIP协议基本呼叫流程

SIP呼叫流程下图显示了一个SIP会话的基本呼叫流程。 下面是上述呼叫流程的详细解释: Alice发送INVITE请求到代理服务器,INVITE请求负责发起会话。 代理服务器立即发送Trying 100给请求者(Alice),表示试图响应INVITE请求,以防止重传该INVITE请求。 代理服务器搜索Bob所在位置服务器的地址,得到的地址后,将转发INVITE请求到Bob。 此后,Bob回应180

VVSIP官网博客 1万+

java html解析成xml文件_java 读取xml文件,并转换成字符串 | 学步园

package com.sfzc.books.service.imp;import java.io.File;import java.io.IOException;import java.io.StringWriter;import java.net.URL;import org.jdom.input.SAXBuilder;import org.jdom.output.Format;import ...

weixin_34237125的博客 277

告别懵圈!用CANoe手把手解析ISO15765多帧传输(附实战报文分析)

本文详细解析了ISO15765多帧传输协议在汽车电子诊断中的应用,通过CANoe实战演示了从首帧发送到流控协商再到连续帧传输的完整流程。文章包含嵌入式系统诊断协议的深度分析,特别适合UDS和CAN总线开发人员学习多帧传输机制及常见问题解决方案。

weixin_30813225的博客 287

励磁电感 Lm 选对了,LLC 才真“高效”:半桥 LLC 效率优化

你有没有遇到过这种 LLC:参数一套下来,ZVS 也有了,波形看着也“挺谐振”,但一上功率——,效率就是死活上不去。我说句直白的:LLC 的“高效”,很大一部分其实是在背后控场。Lm 选小了,励磁电流蹭蹭涨,谐振电流 RMS 变大,导通损耗、开关损耗、整流损耗一起上天;Lm 选太大,又可能把 ZVS 的门槛抬高,轻载掉 ZVS,EMI 也开始作妖。

weixin_43199439的博客 450

java html解析成xml文件_在Java中解析包含HTML实体的XML文件,而无需更改XML

我必须解析一些Java文件,这些文件有时候是无效的,它包含HTML实体,例如& mdash;& gt;等等。我理解正确的处理方法是在解析之前向XML文件添加适当的实体声明。但是,我无法做到这一点,因为我无法控制这些XML文件。是否有某种回调,我可以覆盖在Java XML解析器遇到这样一个实体时被调用?我没有能够在API中找到一个。我想用:DocumentBuilderFactory...

weixin_39717865的博客 362

北斗导航 | 卫星/惯导组合导航方法

从组合导航结构方面来讲,卫星/惯导(下文以GPS/INS)组合模式一般分为三种,即松组合、紧组合和超紧组合(即深组合)。

尘世冰封的专栏 1555

java html解析成xml文件,在不改变XML的情况下,在Java中解析包含HTML实体的XML文件...

I have to parse a bunch of XML files in Java that sometimes -- and invalidly -- contain HTML entities such as —, > and so forth. I understand the correct way of dealing with this is to add suitable...

weixin_42361112的博客 189

Dify 第 3 篇 Dify 入门示例 - 聊天助手

本文介绍了如何使用Dify平台快速搭建一个聊天助手。首先通过创建空白应用并选择"聊天助手"模板完成基础设置,然后安装配置通义千问等大语言模型(LLM)并设置API密钥。进阶部分演示了如何创建支持编程语言选择的代码解释器,包括添加提示词约束、插入变量、设置开场白等功能。最后展示了运行效果:用户可选择编程语言后输入代码,系统会给出相应解释。文章还提到应用支持发布共享,并附有Dify官方文档参考链接。

甘蓝的专栏 869

Dify之八添加各种在线大模型

Dify之八添加各种在线大模型

yilvyangguang520的博客 954

java html解析成xml文件_使用XSLT将XML文档转换成HTML

[java]代码库/*** 使用XSLT将XML文档转换成HTML* @param xmlFileName源XML文件名* @param xslFileNameXSL文件名* @param htmlFileName输出的HTML文件名* @return返回HTML文件名*/public static String xml_xslt_html(String xmlFileName, String x...

weixin_34603814的博客 265

PD详解—第一章—Type-C连接

Type-C连接 今天大师匈下班比较早,再给大家侃一侃PD,希望大家支持。 PD和Type-c的关系 首先讲一下PD快充的实现步骤,本文主要是Source端(电源),Sink端的差不多也一样,只是策略不同而已。 TypeC连接-->广播电源能力Source capbility(数据交互)->打电压电流->等待其他指令。 所以typec连接是第一步,很多人会从字面意思理解typec连接,这不就是把线给插上去就好了吗? 不知道有没有优秀的同学想到正确答案; 正确答案:typec连接的本质是为

大师匈的博客 1万+

将多个html文件合成一个,将多个xml文件合并为一个html表

我在一个具有相同结构的目录中有很多XML,我需要生成HTML表,其中每个XML的“结果”都是一列。在我有一个csv文件之前,这很容易。但现在我甚至不知道如何开始。我读了一些关于XSLT的内容,但是我不知道如何使用它,因为我只有路径到XML文件的目录,而不是一个文件的路径。将多个xml文件合并为一个html表例file1.xmlaaok例file2.xmlaafailed我需要一个像这样的表:fil...

weixin_30741979的博客 798

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

java dom4j 解析XML文件 简易教程

java dom4j 解析XML文件 简易教程

琼华 2392

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

java xsl解析xml_XSL解析xml | 学步园

1.解析步骤(1)取得xml文件的内容xmlContent(通过IO流或者由webservice得到应答字符串)(2)根据xmlContent构造输入流xmlStreamInputStream xmlStream = new ByteArrayInputStream(xmlContent.getBytes("UTF-8");(3)根据xsl文件的路径构造输入流xsltStreamInputStre...

weixin_29612623的博客 567

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

用java去访问web service 提供的电视节目预告

思路: 1 用程序去访问 web service 服务.返回 xml文件。 2 用dom 去解析xml 3 用解析后的数据 生成 html文件 访问web service 的代码 [code="java"]public class StaticHTMLFile{ public static boolean PrintPage(String page, String...

qiao88的专栏 391

android图文报告生成,【原创】android 测试生成 html 报告

之前写过一个关于怎样生成 html 的东西,当时是通过 java 代码去解析生成的 xml 然后再拼装成 html,感觉那个方法不是很好,太繁琐,后来看到 java 可以利用 ant 来生成 html,于是就试了一下。虽然很不愿意去看 ant 的资料,在各位大神严厉的不严厉的批评下还是硬着头皮大致过了一遍,觉得实现方法比较低端就没写贴,看到今天有小伙伴需要就贴一下,献个丑。如下为 build.xm...

weixin_42130786的博客 271

java读取html生成xml_java 解析并生成 XML

在 java 中使用 Dom4j 解析 XML对 XML 文件的解析,通常使用的是 Dom4j 和 jdom作为XML解析工具。在此只介绍下 Dom4j 对 XML 文件的解析使用方法。1. 明白了解 XML 文件的树结构2. 了解 DOM4J 的一些接口1. XML 文件的树结构(附:XML :http://www.w3school.com.cn/xml/xml_tree.asp)XML 文档...

weixin_39996742的博客 196

java解析XML和java解析HTML

一。dom4j  解析xml:   俩jar包:dom4j-1.6.1.jar 和jaxen-1.1-beta-6.jar   /** * java解析xml文件各个节点信息 * * @author Jeelon * @param string * :解析的文件名 */ private static void getXmlIn

JeelonXu.feng 7388
上一篇: elementui ts vant冲突_解决vant框架做H5时踩过的坑(下拉刷新、上拉加载等)
下一篇: java swing 子窗口居中_Java Swing实现让窗体居中显示的方法示例
IT追梦人
博客等级 码龄7年 34粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值