【问题标题】:Adding namespace definition to xml using apache xmlbeans使用 apache xmlbeans 将命名空间定义添加到 xml
【发布时间】:2011-03-01 00:28:31
【问题描述】:

我需要将命名空间定义添加到一个元素,因为当使用 apache xmlbean 生成 xml 时它没有被添加。如何使用 xmlbeans API 实现这一目标?

【问题讨论】:

    标签: java xml xmlbeans


    【解决方案1】:

    我找到了问题的答案。 是这样的。

    XmlCursor cursor= targetObject.newCursor();
    cursor.toNextToken();
    cursor.insertNamespace("A", "namespace1");
    //For example
    cursor.insertNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    cursor.dispose();
    

    【讨论】:

    • 我必须调用 cursor.toNextToken() 两次,否则这就是我需要的。
    【解决方案2】:

    用途:

    XmlOptions.setSaveSuggestedPrefixes()
    
    XmlOptions xmlOptions = new XmlOptions();
    
    xmlOptions.setSavePrettyPrint();
    
    xmlOptions.setSavePrettyPrintIndent(4);
    
    xmlOptions.setSaveAggressiveNamespaces();
    
    HashMap<String, String> nsMap = new HashMap<String, String>();
    
    nsMap.put("namespace1","A");
    
    nsMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsi");
    
    xmlOptions.setSaveSuggestedPrefixes(nsMap);
    
    // Create your XmlObject
    
    <Your XmlObject>.save(new File("test.xml"),xmlOptions);
    

    【讨论】:

    • 对我不起作用——这只设置了建议的前缀。据我了解,最初的问题是关于向实际上不使用该命名空间的文档添加命名空间定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2017-09-21
    • 1970-01-01
    • 2010-12-22
    • 2013-09-21
    • 1970-01-01
    相关资源
    最近更新 更多