Dear subscribers,
I've got the following problem:
I want to generate a dom tree using the xerces dom implementation from scratch. The document I want to create should be valid to two XML Schemes. Therefore I want to create a namespace definition and a namespace alias in the root element. The document should then look like the following sample (please regard especially the namespace alias adlcp):
<?xml version="1.0" encoding="UTF-8"?> <manifest xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2" xmlns:xsi="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd
http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd
http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd">
<organizations/>
<resources>
<resource adlcp:scormtype="asset">
<metadata>
<adlcp:location/>
<metadata/>
</resource>
<resource adlcp:scormtype="asset">
<metadata>
<adlcp:location/>
<metadata/>
</resource>
</resources>
</manifest>
To generate a document like this I modified the DOMGenerate.java as follows:
public class DOMGenerate { public static void main( String[] argv ) { try { Document doc=new DocumentImpl((org.w3c.dom.DocumentType) null);
Element root=doc.createElementNS("http://www.imsproject.org/xsd/imscp_rootv1p1p2", "manifest");
Attr attr=doc.createAttributeNS("http://www.imsproject.org/xsd/imscp_rootv1p1p2", "xsi:schemaLocation");
attr.setValue("http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd");
root.setAttributeNode(attr);
doc.appendChild(root);
Element organizations=doc.createElementNS("http://www.imsproject.org/xsd/imscp_rootv1p1p2", "organizations");
root.appendChild(organizations);
Element resources=doc.createElementNS("http://www.imsproject.org/xsd/imscp_rootv1p1p2", "resources");
root.appendChild(resources);
Element resource=doc.createElementNS("http://www.imsproject.org/xsd/imscp_rootv1p1p2", "resource");
attr=doc.createAttributeNS("http://www.adlnet.org/xsd/adlcp_rootv1p2", "adlcp:scormtype");
attr.setValue("asset");
resource.setAttributeNode(attr);
resources.appendChild(resource);
Element metadata=doc.createElementNS("http://www.imsproject.org/xsd/imscp_rootv1p1p2", "metadata");
resource.appendChild(metadata);
Element location=doc.createElementNS("http://www.adlnet.org/xsd/adlcp_rootv1p2", "adlcp:location");
metadata.appendChild(location);
resource=doc.createElementNS("http://www.imsproject.org/xsd/imscp_rootv1p1p2", "resource");
attr=doc.createAttributeNS("http://www.adlnet.org/xsd/adlcp_rootv1p2", "adlcp:scormtype");
attr.setValue("asset");
resource.setAttributeNode(attr);
resources.appendChild(resource);
metadata=doc.createElementNS("http://www.imsproject.org/xsd/imscp_rootv1p1p2", "metadata");
resource.appendChild(metadata);
location=doc.createElementNS("http://www.adlnet.org/xsd/adlcp_rootv1p2", "adlcp:location");
metadata.appendChild(location);
((DocumentImpl)doc).normalizeDocument();
OutputFormat format = new OutputFormat( doc ); //Serialize DOM
format.setIndenting(true);
format.setIndent(4);
StringWriter stringOut = new StringWriter(); //Writer will be a String
XMLSerializer serial = new XMLSerializer( stringOut, format );
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize( doc.getDocumentElement() );
System.out.println( "\n\n" );
System.out.println( stringOut.toString() ); //Spit out DOM as a String
} catch ( Exception ex ) {
ex.printStackTrace();
}
}
}
But this program returns a different XML file (please regard especially the namespace alias adlcp):
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns="http://www.imsproject.org/xsd/imscp_rootv1p1p2"
xmlns:xsi="http://www.imsproject.org/xsd/imscp_rootv1p1p2" xsi:schemaLocation="http://www.imsproject.org/xsd/imscp_rootv1p1p2 imscp_rootv1p1p2.xsd http://www.imsglobal.org/xsd/imsmd_rootv1p2p1 imsmd_rootv1p2p1.xsd http://www.adlnet.org/xsd/adlcp_rootv1p2 adlcp_rootv1p2.xsd">
<organizations/>
<resources>
<resource adlcp:scormtype="asset" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2">
<metadata>
<adlcp:location/>
</metadata>
</resource>
<resource NS1:scormtype="asset" xmlns:NS1="http://www.adlnet.org/xsd/adlcp_rootv1p2">
<metadata>
<adlcp:location xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_rootv1p2"/>
</metadata>
</resource>
</resources>
</manifest>
My questions:
How do I have to change the program to generate a document like the sample above?
-> How do I create the namespace alias "adlcp" in the root element?
Does anyone have suggestions?
Many thanks, Stefan
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
