xml的复杂解析取值和节点插入导出合并后的xml文件
2006-08-31 09:28
417 查看
如果你正准备 对xml进行解析,而这个xml里面元素放置不规则,个个节点的值取得麻烦,然后又想 往这个xml中随心所欲的插入节点,导出完整的xml文件, 那么请 that
public static String matchtoAppXml(String cpxml_path){
try {
InputStream in = null;
// 初始化XML解析
SAXBuilder builder = new SAXBuilder();
Document requestDoc = null;
requestDoc = builder.build(cpxml_path);
Element root = (Element)requestDoc.getRootElement();
Iterator list = root.getChildren().iterator();
while(list.hasNext()) {
//1-取得外挂信息
Element node = (Element) list.next();
String s_city="";
String s_name="";
String s_add="";
String s_tel="";
//System.out.println(node.getChild("Na").getAttributeValue("v"));
if (node.getChild("Na") != null) {
s_name = node.getChild("Na").getAttributeValue("v");
}
if (node.getChild("Add") != null) {
s_add = node.getChild("Add").getAttributeValue("v");
}
//查询xml重的某个字段如
- <Poi id="0">
<Na v="上海西门子开关有限公司" />
<Cla v="BA80" />
<Dis v="310112" />
<add v="上海市闵行经济技术开发区天宁路298号" />
<Tel v="021-24030000" />
- <Other>
<case n="from" v="elong" i="Y" />
<case n="city" v="北京" i="Y" />
<case n="url" v="http://www.powerproduct.com/user1/index.asp?id=12702" i="Y" />
<case n="fax" v="021-64924606" i="Y" />
</Other>
</Poi>
对应代码如下
if (XPath.selectSingleNode(node,"Other/case[@n='city']/@v")!= null) {
Attribute s_city_att = (Attribute)(XPath.selectSingleNode(node,"Other/case[@n='city']/@v"));
s_city = s_city_att.getValue();
}
//2-取得匹配得到的 俗名/经度纬度
Poi poi = getPoi(s_city,s_name,s_add,s_tel);
//3-插入外挂数据中
if(poi!=null){
Element pop=new Element("Pop");
//pop.setText("aaaa");
pop.setAttribute("v",poi.getName());
node.addContent(2,pop);//第二位插入
Element geo=new Element("Geo");
geo.setAttribute("lo",Integer.toString(poi.getLongitude()));
geo.setAttribute("la",Integer.toString(poi.getLatitude()));
node.addContent(3,geo);
}else{
node.removeContent();
}
}
//4-保存
XMLOutputter so=new XMLOutputter();
FileOutputStream fos = new FileOutputStream("d:/out.xml");
so.output(requestDoc,fos);
得到的out.xml的效果如下:
<Poi id="2">
<Na v="昆仑饭店" />
<Pop v="昆仑饭店" />
<Geo lo="11646273" la="3994418" />
<Cla v="2020" />
<Dis v="110105" />
<add v="北京朝阳区新源南路2号" />
<Tel v="010-65903388" />
- <Other>
<case n="from" v="derby" i="Y" />
<case n="city" v="北京" i="N" />
<case n="passportCode" v="P07000301DD77B864" i="N" />
<case n="url" v="http://api.51ditu.com" i="N" />
</Other>
</Poi>
} catch (MalformedURLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return "";
}
public static String matchtoAppXml(String cpxml_path){
try {
InputStream in = null;
// 初始化XML解析
SAXBuilder builder = new SAXBuilder();
Document requestDoc = null;
requestDoc = builder.build(cpxml_path);
Element root = (Element)requestDoc.getRootElement();
Iterator list = root.getChildren().iterator();
while(list.hasNext()) {
//1-取得外挂信息
Element node = (Element) list.next();
String s_city="";
String s_name="";
String s_add="";
String s_tel="";
//System.out.println(node.getChild("Na").getAttributeValue("v"));
if (node.getChild("Na") != null) {
s_name = node.getChild("Na").getAttributeValue("v");
}
if (node.getChild("Add") != null) {
s_add = node.getChild("Add").getAttributeValue("v");
}
//查询xml重的某个字段如
- <Poi id="0">
<Na v="上海西门子开关有限公司" />
<Cla v="BA80" />
<Dis v="310112" />
<add v="上海市闵行经济技术开发区天宁路298号" />
<Tel v="021-24030000" />
- <Other>
<case n="from" v="elong" i="Y" />
<case n="city" v="北京" i="Y" />
<case n="url" v="http://www.powerproduct.com/user1/index.asp?id=12702" i="Y" />
<case n="fax" v="021-64924606" i="Y" />
</Other>
</Poi>
对应代码如下
if (XPath.selectSingleNode(node,"Other/case[@n='city']/@v")!= null) {
Attribute s_city_att = (Attribute)(XPath.selectSingleNode(node,"Other/case[@n='city']/@v"));
s_city = s_city_att.getValue();
}
//2-取得匹配得到的 俗名/经度纬度
Poi poi = getPoi(s_city,s_name,s_add,s_tel);
//3-插入外挂数据中
if(poi!=null){
Element pop=new Element("Pop");
//pop.setText("aaaa");
pop.setAttribute("v",poi.getName());
node.addContent(2,pop);//第二位插入
Element geo=new Element("Geo");
geo.setAttribute("lo",Integer.toString(poi.getLongitude()));
geo.setAttribute("la",Integer.toString(poi.getLatitude()));
node.addContent(3,geo);
}else{
node.removeContent();
}
}
//4-保存
XMLOutputter so=new XMLOutputter();
FileOutputStream fos = new FileOutputStream("d:/out.xml");
so.output(requestDoc,fos);
得到的out.xml的效果如下:
<Poi id="2">
<Na v="昆仑饭店" />
<Pop v="昆仑饭店" />
<Geo lo="11646273" la="3994418" />
<Cla v="2020" />
<Dis v="110105" />
<add v="北京朝阳区新源南路2号" />
<Tel v="010-65903388" />
- <Other>
<case n="from" v="derby" i="Y" />
<case n="city" v="北京" i="N" />
<case n="passportCode" v="P07000301DD77B864" i="N" />
<case n="url" v="http://api.51ditu.com" i="N" />
</Other>
</Poi>
} catch (MalformedURLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return "";
}
相关文章推荐
- xml的复杂解析取值和节点插入导出合并后的xml文件
- Java生成和解析XML格式文件——重复生成多xml标记用于restful服务端插入Oracle数据库测试
- Java创建,解析XML片段文件,无根节点root
- 通过xml的解析多数据源对文件进行导出成excel
- java解析String类型t复杂xml,多级节点,最好的例子
- 【慕课笔记】2-3 应用DOM方式解析XML—使用 DOM 解析XML 文件的节点名和节点值
- Ajax实现xml文件数据插入数据库(一)--- 构建解析xml文件的js库
- Java如何从XML文件中获取节点信息的代码解析 Document方法
- 解析数据库生成XML文件及解析XML文件插入数据库
- Dom解析是将xml文件全部载入,组装成一颗dom树,然后通过节点以及节点之间的关系来解析xml文件,下面结合这个xml文件来进行dom解析。
- 使用 boost 解析 XML 文件中的节点属性
- Ajax实现xml文件数据插入数据库(一)--- 构建解析xml文件的js库
- Dom4j解析xml文件-操作节点和属性及写入其他xml文件
- Java使用DOM解析XML的文件属性名和属性值、节点名和节点值
- 使用XStream解析复杂的XML文件
- Maven学习笔记(一)--pom.xml文件常用节点解析
- XML文件解析,未找到节点
- DOM解析XML的文件属性名和属性值、节点名和节点值
- 使用DOM解析XML的文件属性名和属性值、节点名和节点值
- 使用XStream解析复杂的XML文件