使用java自带的xml解析器解析xml,
其实我不推荐,可以用Dom解析,或其他的方式,
因为Java自带的解析器使用不但麻烦,且还有bug出现。
它要求,针对不同的xml(结构不同),必须写对应的handler处理类,
而Dom解析可以写通用的方法(前提是要写出通用逻辑)来处理,开发比较快。
下面上代码
1 //------------------ main point --------------------- 2 javax.xml.parsers.SAXParserFactory 3 public static void main(String[] args) 4 { 5 //cdu 存放结果的对象 6 CS3drawUnit cdu=new CS3drawUnit(); 7 8 SAXParserFactory factory = SAXParserFactory.newInstance(); 9 10 SAXParser parser; 11 try { 12 parser = factory.newSAXParser(); 13 14 /* // 方式一 15 InputStream is=new ByteArrayInputStream(strxml.getBytes("UTF-8")); 16 parser.parse(is, new Apphandler(cdu)); 17 */ 18 19 // 方式二 20 parser.parse(new File("cs3.xml"),new Apphandler(cdu)); 21 } 22 catch (ParserConfigurationException e) { 23 // TODO Auto-generated catch block 24 e.printStackTrace(); 25 } catch (SAXException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 } 33 34 // 存放结果的实体 35 //------------------CS3drawUnit bean--------------------- 36 public class CS3drawUnit { 37 38 //可以有多个 fields,因为太多我省略了好多 39 private String XWallThickness=""; 40 41 public String getXWallThickness() { 42 return XWallThickness; 43 } 44 public void setXWallThickness(String xWallThickness) { 45 XWallThickness = xWallThickness; 46 } 47 48 } 49 50 // 这就是处理类handler 51 //------------------Apphandler --------------------- 52 import org.xml.sax.Attributes; 53 import org.xml.sax.SAXException; 54 import org.xml.sax.helpers.DefaultHandler; 55 56 public class Apphandler extends DefaultHandler { 57 58 /* 59 <field> 60 <name>Type / tubes in face</name> 61 <value>A (1/2" OD) / 16</value> 62 </field> 63 64 */ 65 66 67 Unit u=null; 68 //内部类 为了在characters方法中调用,使代码显的清晰 69 public class Unit{ 70 71 private String xname=""; 72 private String xvalue=""; 73 private String xmodelNo =""; 74 private String xahTag =""; 75 76 77 public String getXahTag() { 78 return xahTag; 79 } 80 public void setXahTag(String xahTag) { 81 this.xahTag = xahTag; 82 } 83 public String getXname() { 84 return xname; 85 } 86 public void setXname(String xname) { 87 this.xname = xname; 88 } 89 public String getXvalue() { 90 return xvalue; 91 } 92 public void setXvalue(String xvalue) { 93 this.xvalue = xvalue; 94 } 95 public String getxmodelNo() { 96 return xmodelNo; 97 } 98 public void setxmodelNo(String xmodelNo) { 99 this.xmodelNo = xmodelNo; 100 } 101 102 103 } 104 CS3drawUnit cdu=null; 105 106 private final int xname_STATE = 1; 107 private final int xvalue_STATE = 2; 108 private final int xmodelNo_STATE = 3; 109 private final int xahTag_STATE = 4; 110 111 112 private int currentState; 113 114 public Apphandler(CS3drawUnit cdu) 115 { 116 this.cdu=cdu; 117 currentState=0; 118 u=new Unit(); 119 } 120 121 //获取解析结果,可以再处理,此处把结果再加工,set到cdu实例中返回 122 ///@Override 123 public void endElement(String uri, String localName, String qName) 124 throws SAXException { 125 126 127 if(qName.equals("modelNo")&& u!= null){ 128 cdu.setXModelNo(u.getxmodelNo()); 129 } 130 if(qName.equals("ahTag")&& u!= null){ 131 cdu.setXAhTag(u.getXahTag()); 132 } 133 134 } 135 136 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 137 // TODO Auto-generated method stub 138 //System.out.println("qName:"+qName); 139 if (qName.equals("field")){ 140 u=new Unit(); 141 return; 142 } 143 144 if (qName.equals("name")){ 145 currentState = xname_STATE; 146 return ; 147 } 148 149 if (qName.equals("value")){ 150 currentState = xvalue_STATE; 151 return ; 152 } 153 if (qName.equals("modelNo")){ 154 currentState = xmodelNo_STATE; 155 return ; 156 } 157 158 if (qName.equals("ahTag")){ 159 currentState = xahTag_STATE; 160 return ; 161 } 162 } 163 164 165 public void characters(char[] ch, int start, int length) throws SAXException { 166 167 //length这个值在某些情况下,会被裁断(莫名其妙),估计是bug,遇到过一回, 168 //所以我在后来不让它解析出错时的字段,改用Dom解析。 169 String theString = new String(ch, start, length); 170 171 switch(currentState) 172 { 173 case xname_STATE: 174 u.setXname(theString); 175 currentState = 0; 176 break; 177 178 case xvalue_STATE: 179 u.setXvalue(theString); 180 currentState = 0; 181 break; 182 case xmodelNo_STATE: 183 u.setxmodelNo(theString); 184 currentState = 0; 185 break; 186 case xahTag_STATE: 187 u.setXahTag(theString); 188 currentState = 0; 189 break; 190 } 191 } 192 193 }
若转载请标注 :转自 博客园 : http://www.cnblogs.com/rojas/p/4013297.html
时间: 2024-10-10 07:54:35