首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

这种xml要如何解析

2013-07-09 
这种xml要怎么解析啊如题?xml version1.0 encodingutf-8 ?rootprovince data北京cityci

这种xml要怎么解析啊
如题
<?xml version="1.0" encoding="utf-8" ?>
<root>
  <province data="北京">
    <city>
      <cityid>101010100</cityid>
      <cityname>北京</cityname>
    </city>
    <city>
      <cityid>101010200</cityid>
      <cityname>海淀</cityname>
    </city>
    <city>
      <cityid>101010300</cityid>
      <cityname>朝阳</cityname>
    </city>
    <city>
      <cityid>101010400</cityid>
      <cityname>顺义 </cityname>
    </city>
  </province>
</root>
[解决办法]
这还有什么好说的么?
[解决办法]
public class province
{
private String data;
private List<City> city;
}

这个还有什么好说了 sax 或者pull自己解析吧,很清晰 
[解决办法]
用dom4j吧之前做java开发时用这个 很简单  http://www.cnblogs.com/macula/archive/2011/07/27/2118003.html可以看看
[解决办法]
我随便写了个 你下个jar包可以直接用  Document doc = null;
SAXReader sax = new SAXReader();
try {
doc = sax.read(new File("d://aaa.xml"));
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element root = doc.getRootElement();

List<Map<String, String>> dbConnections = new ArrayList<Map<String, String>>();
 
     Iterator<Element> rootIter = root.elementIterator();


     while (rootIter.hasNext()) {
         Element connection = rootIter.next();
         Iterator<Element> childIter = connection.elementIterator();
         Map<String, String> connectionInfo = new HashMap<String, String>();
         List<Attribute> attributes = connection.attributes();
         for (int i = 0; i < attributes.size(); ++i) { // 添加节点属性
         System.out.println(attributes.get(i).getName() +"  attributes value= "+attributes.get(i).getValue());
             connectionInfo.put(attributes.get(i).getName(), attributes.get(i).getValue());
         }
         while (childIter.hasNext()) { // 添加子节点
             Element attr = childIter.next();
             System.out.println(attr.getName().trim() +"        "+attr.getText().trim());
             connectionInfo.put(attr.getName().trim(), attr.getText().trim());
             
             
             Iterator<Element> ddd = attr.elementIterator();
             while (ddd.hasNext()) { // 添加子节点
             Element xxx = ddd.next();
             System.out.println(xxx.getName().trim() +"        "+xxx.getText().trim());
             connectionInfo.put(xxx.getName().trim(), xxx.getText().trim());
             }
         }
         
         dbConnections.add(connectionInfo);


     }

热点排行