qq_36989169
菜鸟一小枚Sun
2018-06-18 09:47
采纳率: 33.3%
浏览 1.1k
已采纳

dom4j,标签属性添加不上问题

1.问题是:xmlns属性为什么没有加进去呢?
直接放代码(结果图在后面)

 public static void main(String[] args){
        XMLWriter writer=null;
        FileOutputStream out=null;
        try {
            Document doc = DocumentHelper.createDocument();
            Element Cole = doc.addElement("Cole");
            Cole.addAttribute("xmlns","http://www.gzrst.gov.cn/DA");
            Cole.addAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
            Cole.addAttribute("xsi:schemaLocation","http://www.gzrst.gov.cn/DA SIP.xsd");

            out = new FileOutputStream("d:/test2.xml");
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            writer = new XMLWriter(out,format);
            writer.write(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

2.结果:
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • gf0515
    Fatal Error 2018-06-18 13:15
    已采纳

    改一句就可以了:

     Document doc = DocumentHelper.createDocument();
                Element Cole = doc.addElement("Cole");
    
                Namespace namespace = Namespace.get("http://www.gzrst.gov.cn/DA");
    
    //            Cole.addAttribute("xmlns","http://www.gzrst.gov.cn/DA");
                Cole.add(namespace);
                Cole.addAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
                Cole.addAttribute("xsi:schemaLocation","http://www.gzrst.gov.cn/DA SIP.xsd");
    
    点赞 评论
  • xiazhongwei_csdn
    ablejava 2018-06-18 10:36

    点赞 评论
  • qq_39372246
    qq_39372246 2018-06-18 11:02

    重写onEnd()和onStart()方法

    public class XmlElementHandler implements ElementHandler {

    @Override
    public void onEnd(ElementPath arg0) {
    Element ele = arg0.getCurrent();
    String name = ele.getName();
    try
    {
    //保存"MT"标签的属性到内存
    if("MT".equals(name))
    {
    String mTvalue = ele.getTextTrim();
    ParaCollect.getInstance().put("MT", mTvalue);
    }
    //保存"SubNo"标签的属性到内存
    if("SubNo".equals(name))
    {
    String subNovalue = ele.getTextTrim();
    ParaCollect.getInstance().put("localPeriods", subNovalue);
    }
    //保存"Freq"标签的属性到内存
    if("Freq".equals(name))
    {
    String Freqvalue = ele.getTextTrim();
    ParaCollect.getInstance().put("localFreq", Freqvalue);
    }
    //保存"SubCode"标签的属性到内存
    if("SubCode".equals(name))
    {
    String SubCodevalue = ele.getTextTrim();
    ParaCollect.getInstance().put("localSubCode", SubCodevalue);
    }
    } catch (Exception e) {
    }
    }
    @Override
    public void onStart(ElementPath arg0) { }
    }

    获取标签属性

    String xmlps = “xml文件路径”;

    /*************解析xml中标签SubNo的属性 begin************************/
    SAXReader reader;
    try
    {
    InputStream in= new FileInputStream(new File(xmlps));
    ElementHandler manElementHandler=(ElementHandler) new XmlElementHandler();
    reader=new SAXReader();
    reader.addHandler("/Document/SubNo", manElementHandler);
    reader.addHandler("/Document/Freq", manElementHandler);
    reader.addHandler("/Document/SubCode", manElementHandler);
    reader.read(in);
    }
    catch (Exception e)
    {
    log.error("公共检查解析xml异常", e);
    }

    点赞 评论

相关推荐