The problem of sax parsing xml

characters will have many problems. Record them and find them for a long time. It turns out that they are the problem of spaces. Here is the xml file after format and the corresponding print

xml file:


Corresponding log:


Reference: the following excerpt from http://blog.csdn.net/Garmiter/article/details/8016840

XML type 1: no space left

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <workers>  
  3. <worker id="1001">  
  4. <name>Garmiter</name>  
  5. <sex>Boy</sex>  
  6. <address>Sichuan Province,Chengdu City</address>  
  7. <money>$2345678</money>  
  8. <status>001</status>  
  9. </worker>  
  10. <worker id="1002">  
  11. <name>Garmiter2</name>  
  12. <sex>Boy2</sex>  
  13. <address>Sichuan Province,Chengdu City</address>  
  14. <money>$23456782</money>  
  15. <status>002</status>  
  16. </worker>  
  17. </workers>  

XML type 2: space on the left

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <workers>  
  3.   <worker id="1001">  
  4.     <name>Garmiter</name>  
  5.     <sex>Boy</sex>  
  6.     <address>Sichuan Province,Chengdu City</address>  
  7.     <money>$2345678</money>  
  8.     <status>001</status>  
  9.   </worker>  
  10.   <worker id="1002">  
  11.     <name>Garmiter2</name>  
  12.     <sex>Boy2</sex>  
  13.     <address>Sichuan Province,Chengdu City</address>  
  14.     <money>$23456782</money>  
  15.     <status>002</status>  
  16.   </worker>  
  17. </workers>  

android parses XML fragment code - type 1: execute the above XML types only once, execute the above XML types twice and affect the result

  1. @Override  
  2.     public void characters(char[] ch, int start, int length)  
  3.             throws SAXException {  
  4.         //System.out.println("-----------tagname="+tagname);  
  5.         if(tagname.equals("name")){  
  6.             hisname=new String(ch,start,length);  
  7.             System.out.println("===="+hisname);  
  8.         }else if(tagname.equals("sex")){  
  9.             sex=new String(ch,start,length);   
  10.             System.out.println("===="+sex);  
  11.         }else if(tagname.equals("address")){  
  12.             address=new String(ch,start,length);   
  13.             System.out.println("===="+address);  
  14.         }else if(tagname.equals("money")){  
  15.             money=new String(ch,start,length);   
  16.             System.out.println("===="+money);  
  17.         }else if(tagname.equals("status")){  
  18.             status=new String(ch,start,length);   
  19.             System.out.println("===="+status);  
  20.         }  
  21.           
  22.     }  

android parsing XML fragment code - type 2: execute the above XML types only once, and execute the above XML types twice without affecting the result

  1. @Override  
  2.     public void characters(char[] ch, int start, int length)  
  3.             throws SAXException {  
  4.         //System.out.println("-----------tagname="+tagname);  
  5.         if(tagname.equals("name")){  
  6.             hisname=new String(ch,start,length);  
  7.             System.out.println("===="+hisname);  
  8.         }else if(tagname.equals("sex")){  
  9.             sex=new String(ch,start,length);   
  10.             System.out.println("===="+sex);  
  11.         }else if(tagname.equals("address")){  
  12.             address=new String(ch,start,length);   
  13.             System.out.println("===="+address);  
  14.         }else if(tagname.equals("money")){  
  15.             money=new String(ch,start,length);   
  16.             System.out.println("===="+money);  
  17.         }else if(tagname.equals("status")){  
  18.             status=new String(ch,start,length);   
  19.             System.out.println("===="+status);  
  20.         }  
  21.         tagname="";//Give tagname to not meet any of the above conditions, so that no valid steps will be taken for multiple executions  
  22.     }  

Keywords: xml encoding Android Fragment

Added by Codewarrior123 on Sat, 04 Apr 2020 14:47:05 +0300