web学习第三天
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了web学习第三天相关的知识,希望对你有一定的参考价值。
Xml文档
<?xml version="1.0" encoding="UTF-8"?>
<书架>
<书>
<书名 name="小黄书">Java就业培训教程</书名>
<作者>张兆祥</作者>
<售价>999元</售价>
<售价>59yuan</售价>
<售价>1000元</售价>
</书>
<书>
<书名>javascript网页开发</书名>
<作者>张兆祥2</作者>
<售价>12</售价>
</书>
</书架>
基本操作
1得到xml文档中所有的文档:
package day3;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
public class booksax {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//1,创建解析工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//2,得到解析器
SAXParser sp=factory.newSAXParser();
//3,得到读取器
XMLReader reader= sp.getXMLReader();
//4,设置内容处理器
reader.setContentHandler(new ListHandler());
//5,读取xml文档内容
reader.parse("src/day3/book.xml");
}
}
//得到xml文档中所有的文档
class ListHandler implements ContentHandler{
@Override
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException {
System.out.println("<"+name+">");
for(int i=0;atts!=null&i<atts.getLength();i++) {
String attname=atts.getQName(i);
String attvalue=atts.getValue(i);
System.out.println(attname+":"+attvalue);
}
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
System.out.println("<"+name+">");
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
System.out.println(new String(ch,start,length));
}
@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void processingInstruction(String target, String data) throws SAXException {
// TODO Auto-generated method stub
}
@Override
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub
}
}
2获取指定便签的值
package day3;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class booksax2 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//1,创建解析工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//2,得到解析器
SAXParser sp=factory.newSAXParser();
//3,得到读取器
XMLReader reader= sp.getXMLReader();
//4,设置内容处理器
reader.setContentHandler(new TagValueHandler());
//5,读取xml文档内容
reader.parse("src/day3/book.xml");
}
}
//获取指定便签的值
class TagValueHandler extends DefaultHandler {
private String currentTag;//记住当前解析到的是什么标签
private int needNumber=2; //记住想获取第几个作者标签的值
private int currentNumber=0;//当前解析到第几个
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
currentTag=name;
if(currentTag.equals("作者")) {
currentNumber++;
}
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
currentTag=null;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if("作者".equals(currentTag)&¤tNumber==needNumber) {
System.out.println(new String(ch,start,length));
}
}
}
3Xml->book->list
book对象
package day3;
public class Book_booksax3 {
private String name;
private String author;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
操作:
package day3;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
public class booksax3 {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//1,创建解析工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//2,得到解析器
SAXParser sp=factory.newSAXParser();
//3,得到读取器
XMLReader reader= sp.getXMLReader();
//4,设置内容处理器
BeanListHandler handler=new BeanListHandler();
reader.setContentHandler( handler);
//5,读取xml文档内容
reader.parse("src/day3/book.xml");
@SuppressWarnings("unchecked")
List<Book_booksax3> list=handler.getBooks();
System.out.println(list);
}
}
//把xml文档中的每一本书封装到book对象中,并把每一个book对象放在list集合中返回
class BeanListHandler extends DefaultHandler {
private List list=new ArrayList();
private String currentTag;
private Book_booksax3 book;
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
currentTag=name;
if("书".equals(currentTag)) {
book=new Book_booksax3();
}
}
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
currentTag=null;
if(name.equals("书")) {
list.add(book);
book=null;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if("书名".equals(currentTag)) {
String name=new String(ch,start,length);
book.setName(name);
}
if("作者".equals(currentTag)) {
String author=new String(ch,start,length);
book.setAuthor(author);
}
if("售价".equals(currentTag)) {
String price=new String(ch,start,length);
book.setPrice(price);
}
}
public List getBooks() {
return list;
}
}
dom4j
Xml文档
<?xml version="1.0" encoding="UTF-8"?>
<书架>
<书>
<书名 name="小黄书">Java就业培训教程</书名>
<作者>张兆祥</作者>
<售价>999元</售价>
<售价>59yuan</售价>
<售价>1000元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>张兆祥2</作者>
<售价>12</售价>
</书>
基本操作
package day3_2;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;
public class dom4j {
@Test
//读取第二本书的书名:<书名>JavaScript网页开发</书名>
public void reader()throws Exception {
SAXReaderreader =new SAXReader();
Documentdocument =reader.read(new File("src/day3_2/book.xml"));
Elementroot=document.getRootElement();
Elementbook=(Element)root.elements("书").get(1);
Stringvalue=book.element("书名").getText();
System.out.println(value);
}
@Test
//读取第一本书的属性:<书名 name="小黄书">Java就业培训教程</书名>
public void readAtt()throws Exception {
SAXReaderreader =new SAXReader();
Documentdocument =reader.read(new File("src/day3_2/book.xml"));
Elementroot=document.getRootElement();
Elementbook=(Element)root.elements("书").get(0);
Stringvalue=book.element("书名").attributeValue("name");
System.out.println(value);
}
@Test
//在第一本书添加新的售价:<售价>1000元</售价>
public void add()throws Exception {
SAXReaderreader =new SAXReader();
Documentdocument =reader.read(new File("src/day3_2/book.xml"));
Elementbook=document.getRootElement().element("书");
book.addElement("售价").setText("1000元");
OutputFormatformat = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
// XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream( "src/day3_2/book.xml" ),"gb2312"));
XMLWriterwriter =new XMLWriter(new FileOutputStream("src/day3_2/book.xml" ),format);
writer.write(document);
writer.close();
}
@Test
//在第一本书指定位置添加新的售价:<售价>999元</售价>
public void add2()throws Exception {
SAXReaderreader =new SAXReader();
Documentdocument =reader.read(new File("src/day3_2/book.xml"));
Elementbook=document.getRootElement().element("书");
List list=book.elements();//[书名,作者,售价,售价..]
Elementprice=DocumentHelper.createElement("售价");
price.setText("999元");
list.add(2,price);
OutputFormatformat = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
// XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream( "src/day3_2/book.xml" ),"gb2312"));
XMLWriterwriter =new XMLWriter(new FileOutputStream("src/day3_2/book.xml" ),format);
writer.write(document);
writer.close();
}
@Test
//删除上面添加的书售价 节点
public void delete()throws Exception {
SAXReaderreader =new SAXReader();
Documentdocument =reader.read(new File("src/day3_2/book.xml"));
Elementprice=document.getRootElement().element("书").element("售价");
price.getParent().remove(price);
OutputFormatformat = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
// XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream( "src/day3_2/book.xml" ),"gb2312"));
XMLWriterwriter =new XMLWriter(new FileOutputStream("src/day3_2/book.xml" ),format);
writer.write(document);
writer.close();
}
@Test
//更新: <作者>张三</作者>
public void update()throws Exception {
SAXReaderreader =new SAXReader();
Documentdocument =reader.read(new File("src/day3_2/book.xml"));
Elementbook =(Element)document.getRootElement().elements("书").get(1);
book.element("作者").setText("张三");
OutputFormatformat = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
// XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream( "src/day3_2/book.xml" ),"gb2312"));
XMLWriterwriter =new XMLWriter(new FileOutputStream("src/day3_2/book.xml" ),format);
writer.write(document);
writer.close();
}
}
Xpath
Xml文档
<?xml version="1.0" encoding="UTF-8"?>
<书架>
<书>
<书名 name="小黄书" number="123456">Java就业培训教程</书名>
<作者>张兆祥</作者>
<售价>999元</售价>
<售价>59yuan</售价>
<售价>1000元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>张兆祥2</作者>
<售价>12</售价>
</书>
</书架>
查找
package day3_2;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
//应用xpath提取xml文档的数据
public class xpath {
public static void main(String[]args)throws DocumentException {
SAXReaderreader =new SAXReader();
Documentdocument =reader.read(new File("src/day3_2/book.xml"));
Stringvalue =document.selectSingleNode("//作者").getText();
System.out.println(value);
}
}
属性对应的标签匹配
package day3_2;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class xpath_matching {
public static void main(String[]args)throws DocumentException {
Stringname="小黄书1";
Stringnumber="123456";
SAXReaderreader =new SAXReader();
Documentdocument =reader.read(new File("src/day3_2/book.xml"));
Nodenode=document.selectSingleNode("//书名[@name=‘"+name+"‘ and @number=‘"+number+"‘]");
if(node==null) {
System.out.println("没有这本书");
}else {
System.out.println("查找成功!");
}
}
}
XML Schema
http://www.runoob.com/schema/schema-tutorial.html
二维数组问题
package day3_2;
/*打印
3 7
2 4 6 8
1 5 9*/
public class arr {
public static void main(String[] args) {
int num=43;
int height=(num+3)/4;
int width=num;
int arr[] []=new int[height][width];
int x=height-1,y=0;
boolean order=false;
for(int i=1;i<=width-1;i++) {
arr[x][y]=i;
y++;
if(!order) {
x--;
}
if(order) {
x++;
}
if(x<0) {
order=true;
x=x+2;
}
if(x>(num-1)/4) {
order=false;
x=x-2;
}
}
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
if(arr[i][j]==0) {
System.out.print(" ");
}else {
System.out.print(arr[i][j]);
}
}
System.out.println();
}
以上是关于web学习第三天的主要内容,如果未能解决你的问题,请参考以下文章