用dom4j实现XML的遍历,增加namespace, 设置standalone="yes" 等操作

最近用DOM4j实现了对XML的解析,遍历XML element, 增加declare namespace, 设置XML standalone="yes", 和写入XML等操作的操作:

(1) 遍历XML element和每个element attributes:

      首先获取XML document的root element节点后,然后定义一个递归调用的方法,来实现XML element的遍历:

        

private void traverseElement(Element element,StringBuffer strbuf) throws DocumentException{
     StringBuffer interbuf = new StringBuffer(strbuf);
     StringBuffer interbuf2 = new StringBuffer(strbuf);  
     for (Iterator j = element.elementIterator(); j.hasNext();) {  //通过调用elementIterator()方法实现对child element遍历
       Element obj = (Element) j.next();
       System.out.println(interbuf.toString()+obj.getName());
       for ( Iterator i = obj.attributeIterator(); i.hasNext(); ) {  //遍历element的attribute
           Attribute attribute = (Attribute) i.next();  
           System.out.print(" 元素的属性名和值:"+attribute.getName()+"="+attribute.getStringValue());  
       }  
       if(obj.attributeCount()>0) System.out.println("");
       if(getElementChild(obj)) {
           traverseElement(obj,new StringBuffer(interbuf2.append("--")));
           interbuf2.setLength(interbuf2.length()-2);
       }
       
   }  
 }

(2) 增加declare namespace:

         首先要获取XML中已经声明的namespace, 这些namespace的prfix可能是ns2, ns3, ns4...等等    

   List<Namespace> declareNamespaces = rootElm.declaredNamespaces();  //获取XML document已经声明的namespace
   for (Namespace ns: declareNamespaces){
       System.out.println("namespace prefix:"+ ns.getPrefix()+", namespace URI:"+ns.getURI());
   }
   int nslen = root.declaredNamespaces().size() + 1;
   nsPrefix = "ns" + nslen;
   root.addNamespace(nsPrefix, GEN_CDRNS); //root: rootelement, nsPrefix:XML namespace前缀, GEN_CDRNS: 新增namespace的URI


(3)  设置XML standalone="yes"

  在dom4j中不支持直接在outputFormat中设置standalone属性的方法,可以用过改写XMLWriter中的writeDeclaration()方法来实现:

public class StandaloneWriter extends XMLWriter {
    public StandaloneWriter(OutputStream out, OutputFormat format)
            throws UnsupportedEncodingException
        {
            super(out,format);
        }
    
    protected void writeDeclaration()
            throws IOException
        {
           OutputFormat format = getOutputFormat();
            String encoding = format.getEncoding();
            if(!format.isSuppressDeclaration())
            {
                writer.write("<?xml version=\"1.0\"");
                if(encoding.equals("UTF8"))
                {
                    if(!format.isOmitEncoding())
                        writer.write(" encoding=\"UTF-8\"");
                } else
                {
                    if(!format.isOmitEncoding())
                        writer.write(" encoding=\"" + encoding + "\"");
                    
                }
                writer.write(" standalone=\"yes\"");
                writer.write("?>");
                if(format.isNewLineAfterDeclaration())
                    println();
            }
        }
}
那么在写XML的时候,使用StandaloneWriter对象,就能将standalone="yes" 写入到XML declare中

OutputFormat format = OutputFormat.createPrettyPrint();
StandaloneWriter writer = new StandaloneWriter(new FileOutputStream(filePath),format);  
writer.write(document); 


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>





  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值