无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

jdom 解析 xml (csdn)

Posted on 2009-07-26 00:37 Gavin.lee 阅读(409) 评论(0)  编辑  收藏 所属分类: xml doc 操作

jdom parse xml

from:http://topic.csdn.net/t/20041231/11/3691173.html

<?xml version="1.0" encoding="GB2312"?> 
<specificationSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation
="http://www.citi.qut.edu.au/yawl YAWL_Schema.xsd" 
xmlns
="http://www.citi.qut.edu.au/yawl" 
xmlns:mm
="www.citi.qut.edu.au/yawl/exampleSchemas/make_trip"> 
<specification uri="Maketrip1.ywl"> 
    
<metaData/> 
    
<decomposition id="pay" xsi:type="问题在这里!如何处理带有命名空间的属性?"> 
        
<inputParam name="customer"> 
            
<type>xs:string</type> 
        
</inputParam> 

        
<inputParam name="payment_account_number"> 
            
<type>xs:string</type> 
        
</inputParam> 

        
<inputParam name="carDetails"> 
            
<type>xs:string</type> 
        
</inputParam> 

        
<inputParam name="hotelDetails"> 
            
<type>xs:string</type> 
        
</inputParam> 

        
<inputParam name="flightDetails"> 
            
<type>xs:string</type> 
        
</inputParam> 
    
</decomposition> 
</specification> 
</specificationSet>

package com.Gavin.tools.xml;

import java.io.File;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

public class JdomParse {
    
private static StringBuffer strbuf = new StringBuffer();

    
public static void main(String[] argv) {
        
try {
            SAXBuilder sb 
= new SAXBuilder();
            Document doc 
= sb.build(new File("c:/jdom.xml"));
            Element root 
= doc.getRootElement();
            strbuf.append(
"<?xml   version=\"1.0\"   encoding=\"GB2312\"?>").append(System.getProperty("line.separator"));
            strbuf.append(
"<").append(root.getName());
            
            
if (!"".equals(root.getNamespaceURI())) {// Namespace信息
                strbuf.append("   xmlns=\"" + root.getNamespaceURI() + "\"");
            }

            
            
// 得到附加的Namespace,比如:xmlns:xsi
            accessAdditionalNamespaces(root);
            
// 得到附加的Namespace的属性,如:xsi:schemaLocation
            accessAttribute(root);
            
            strbuf.append(
">");
            
// 遍历节点!
            accessElement(root);
            strbuf.append(
"</" + root.getName() + ">");
            
            System.out.println(strbuf.toString());
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }


    
/**
     * accessElement 遍历结点
     * 
     * 
@param parent
     *            Element
     
*/

    
public static void accessElement(Element parent) {
        List listChild 
= parent.getChildren();
        
int iChild = listChild.size();
        
for (int i = 0; i < iChild; i++{
            Element e 
= (Element) listChild.get(i);
            strbuf.append(System.getProperty(
"line.separator"));
            strbuf.append(
"     <");
            strbuf.append(e.getName());
            
// 访问属性
            accessAttribute(e);
            strbuf.append(
">");
            strbuf.append(e.getTextTrim());
            
// 递归访问节点
            accessElement(e);
            strbuf.append(
"</");
            strbuf.append(e.getName());
            strbuf.append(
">");
        }

        
// 恰当换行成XML原有的格式
        if (iChild > 0{
            strbuf.append(System.getProperty(
"line.separator"));
        }

    }


    
/**
     * accessAttribute 访问结点的属性
     * 
     * 
@param e
     *            Element
     
*/

    
public static void accessAttribute(Element e) {
        List listAttributes 
= e.getAttributes();
        
int iAttributes = listAttributes.size();
        
for (int j = 0; j < iAttributes; j++{
            Attribute attribute 
= (Attribute) listAttributes.get(j);
            
// 问题:属性带有命名空间xsi:的怎么处理?
            
// <name xsi:type="Full">YuLimin</name>
            
// + attribute.getNamespacePrefix() + ":"
            strbuf.append("   " + attribute.getName() + "=\""
                    + attribute.getValue() + "\"");
        }

    }


    
/**
     * accessAdditionalNamespaces 访问附加的命名空间
     * 
     * 
@param e
     *            Element
     
*/

    
public static void accessAdditionalNamespaces(Element e) {
        List listAdditionalNamespaces 
= e.getAdditionalNamespaces();
        
int iAttributes = listAdditionalNamespaces.size();
        
for (int j = 0; j < iAttributes; j++{
            Namespace namespace 
= (Namespace) listAdditionalNamespaces.get(j);
            strbuf.append(
"   xmlns:" + namespace.getPrefix() + "=\""
                    + namespace.getURI() + "\"");
        }

    }

}


只有注册用户登录后才能发表评论。


网站导航: