为了账号安全,请及时绑定邮箱和手机立即绑定

生成的第一个book节点没有结束标记</book>

大家没发现生成的第一个book节点没有结束标记</book>吗?

怎么回事??

正在回答

2 回答

package com.imooc.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.AttributesImpl;
public class SaxTest {
 
  public  ArrayList<Book> xmlParse(){
   SAXParserFactory factory=SAXParserFactory.newInstance();
   HandlerTest handler=null;  
   try {
    SAXParser parser=factory.newSAXParser();
     handler=new HandlerTest();
       parser.parse("f:\\books.xml",handler); 
    /*  System.out.println("----共有"+handler.getBookList().size()+"本书-----"); 
      for(Book book1:handler.getBookList()){//遍历booList中的内容
       System.out.println("===========开始==========");
       System.out.println(book1.getID());
       System.out.println(book1.getName());
       System.out.println(book1.getAuthor());
       System.out.println(book1.getYear());
       System.out.println(book1.getPrice());
       System.out.println(book1.getLanguage());
       System.out.println("===========结束==========");
      }*/
   
   } catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
         return handler.getBookList();
        }
   
  public void xmlCreate(){
   ArrayList<Book> booklist=xmlParse();
  SAXTransformerFactory tff=(SAXTransformerFactory) SAXTransformerFactory.newInstance();
  try {
   TransformerHandler handler=tff.newTransformerHandler();
   Transformer tf=handler.getTransformer();
   tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
   tf.setOutputProperty(OutputKeys.INDENT, "yes");
   File f=new File("src/res/newbooks.xml");
   if(!f.exists()){
       f.createNewFile();
   }
   Result result=new StreamResult(new FileOutputStream(f));
   handler.setResult(result);
   handler.startDocument();
   AttributesImpl attr=new AttributesImpl();
   handler.startElement("","","bookStore",attr);
   for(Book book:booklist){
   attr.clear();
   attr.addAttribute("", "", "id","",book.getID());
         handler.startElement("","","book",attr);
        
         if(book.getName()!=null&&!book.getName().trim().equals("")){
            attr.clear();
            handler.startElement("", "","name", attr); 
            handler.characters(book.getName().toCharArray(), 0,book.getName().length());
             handler.endElement("","","name");
         }
        
         
        
         if(!book.getAuthor().equals(null)&&!book.getAuthor().trim().equals("")){
           attr.clear();
              handler.startElement("", "","author", attr);
              handler.characters(book.getAuthor().toCharArray(), 0,book.getAuthor().length());
               handler.endElement("","","author");
         }
            
           if(book.getYear()!=null&&!book.getYear().trim().equals("")){
          attr.clear();
          handler.startElement("", "","year", attr);          
             handler.characters(book.getYear().toCharArray(), 0,book.getYear().length());
             handler.endElement("","","year");         
         }
            if(book.getLanguage()!=null&&!book.getLanguage().trim().equals("")){
          attr.clear();
          handler.startElement("", "","language", attr);          
             handler.characters(book.getLanguage().toCharArray(), 0,book.getLanguage().length());
             handler.endElement("","","language");         
         }
            if(book.getPrice()!=null&&!book.getPrice().trim().equals("")){
          attr.clear();
          handler.startElement("", "","price", attr);          
             handler.characters(book.getPrice().toCharArray(), 0,book.getPrice().length());
             handler.endElement("","","price");         
         }
    
            handler.endElement("","","book");
   }
   handler.endElement("","","bookStore");
   handler.endDocument();
   
   
  } catch (TransformerConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
 }catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
 }catch (SAXException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } 
  }  
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 SaxTest test=new SaxTest();
 test.xmlCreate();
 }
}

代码的位置放置不对

0 回复 有任何疑惑可以回复我~

估计是你的代码漏了这一句:tfhandler.endElement("", "", "book");

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

生成的第一个book节点没有结束标记</book>

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信