Adding namespace definition to xml using apache xmlbeans

后端 未结 2 1624
心在旅途
心在旅途 2021-01-06 18:46

I need to add namespace defintion to an element since its not getting added when when xml is generated using apache xmlbean. How do I acheieve this using xmlbeans API?

2条回答
  •  有刺的猬
    2021-01-06 19:27

    Use:

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

提交回复
热议问题