开发者

jdom removes duplicate namespace declaration (xmloutputter)

jdom seems to remove duplicate namespace declarations. This is a problem when a XML document is embedded into another XML structure, such as for example in the OAI-PHM (open archive initiative). This can be a problem when the surrounding xml is only a container and the embedded document gets extracted later.

Here is some code. The embedded xml is contained in the string with the same name. It declares the xsi namespace. We construct a jdom container, also declaring the xsi namespace. We parse and embed the string. When we print the whole thing the inner xsi namepsace is gone.

public static final Namespace OAI_PMH= Namespace.getNamespace(          "http://www.openarchives.org/OAI/2.0/");
public static final Namespace XSI    = Namespace.getNamespace("xsi",    "http://www.w3.org/2001/XMLSchema-instance");
public static final String SCHEMA_LOCATION = "http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd";
public static final String ROOT_NAME       = "OAI-PMH";


String embeddedxml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 开发者_如何学Go<myxml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"  xsi:schemaLocation=\""
+ "http://www.isotc211.org/2005/gmd" 
+ " http://www.ngdc.noaa.gov/metadata/published/xsd/schema/gmd/gmd.xsd" 
+ " http://www.isotc211.org/2005/gmx"
+ " http://www.ngdc.noaa.gov/metadata/published/xsd/schema/gmx/gmx.xsd\">\"" 
+ "</myxml>";

// loadstring omitted (parse embeddedxml into jdom)
Element xml = loadString(embeddedxml ,false);

Element root = new Element(ROOT_NAME, OAI_PMH);
root.setAttribute("schemaLocation", SCHEMA_LOCATION, XSI);

// insert embedded xml into container structure
root.addContent(xml);


XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());


// will see that the xsi namespace declaration from embeddedxml is gone
out.output(root,System.out);

I think that XMLoutputter is responsible for this behaviour. Any hints how I can make it preserve the duplicate namepspace?

thanks

Kurt


Something is missing in your code: The declaration of final static String ROOT_NAME is not shown and Element xml ist not used after initialization.

If ROOT_NAME is initialized with "myxml" somewhere else, then the solution to your problem is, that you just don't add the xml element to your document, and the result looks as if you did so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜