Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » XML Schema Definition (XSD) » java.lang.NullPointerException when adding Element
java.lang.NullPointerException when adding Element [message #56707] Thu, 30 December 2004 10:09 Go to next message
Eclipse UserFriend
Originally posted by: ooi.de.ibm.com

Hi,

I have a problem when adding a element to a schema. A NullPointerException
occurs although the schema and the element exist.What could the problem
be?
here is the class i am experimenting with.

The error is in the addElements() method.


public class Example {

private XSDSchema schema;
private File outputFile;
private DocumentBuilderFactory docFactory;
private Document document;
private String parentClassName;

private ArrayList currentProperties;

private String root;
private String xsd;
private String muws;

public Example (String inputParentClassName,String inputPathName){
parentClassName = inputParentClassName;
schema = XSDFactory.eINSTANCE.createXSDSchema();
outputFile = new File(inputPathName + "//" + parentClassName+".xsd");
createDocument();
initializeSchema();
initializeNamespaces();
//addImports();
addElements();
save();

}

private void createDocument(){
docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = docFactory.newDocumentBuilder();
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
}
document = (Document) builder.newDocument();
}

private void initializeSchema(){
Element schemaElement = (Element)
document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
schema.setElement(schemaElement);
document.appendChild(schemaElement);
}

private void initializeNamespaces(){
schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
java.util.Map qNamePrefixToNamespaceMap =
schema.getQNamePrefixToNamespaceMap();
root = "http://dmtf.org/v2.8/";
qNamePrefixToNamespaceMap.put("cim-root",root);
xsd= "http://www.w3.org/2001/XMLSchema";
qNamePrefixToNamespaceMap.put("xsd",xsd);
muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
qNamePrefixToNamespaceMap.put("muws",muws);
String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
parentClassName+".xsd";
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
schema.update();
}
private void addImports(){
XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
muwsImport.setNamespace(muws);
muwsImport.setSchemaLocation("\\import\\MUWS.xsd");
schema.getContents().add(muwsImport);

XSDImport rootImport = XSDFactory.eINSTANCE.createXSDImport();
rootImport.setNamespace(root);
rootImport.setSchemaLocation("\\import\\CIM_root.xsd");
schema.getContents().add(rootImport);

}

private void addElements(){
XSDElementDeclarationImpl element =(XSDElementDeclarationImpl)
XSDFactory.eINSTANCE.createXSDElementDeclaration();
element.setName("anElement");
schema.getContents().add(element);
}

public static void main(String[] args) {
Example example1 = new Example("Example","C:\\Documents and
Settings\\Administrator\\Desktop");
}

private void save(){
try {
Source source = new DOMSource(document);
Result result = new StreamResult(outputFile);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source,result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}





}
}

thank you very much.
Re: java.lang.NullPointerException when adding Element [message #56762 is a reply to message #56707] Thu, 30 December 2004 13:26 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------090207060905060305060804
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

HT,

You need to make these match:

private void initializeSchema(){
Element schemaElement = (Element)
document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001,
*"schema"*);
schema.setElement(schemaElement);
document.appendChild(schemaElement);
}

private void initializeNamespaces(){

schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
java.util.Map qNamePrefixToNamespaceMap =
schema.getQNamePrefixToNamespaceMap();
root = "http://dmtf.org/v2.8/";
qNamePrefixToNamespaceMap.put("cim-root",root);
xsd= "http://www.w3.org/2001/XMLSchema";
qNamePrefixToNamespaceMap.put(*"xsd"*,xsd);
muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
qNamePrefixToNamespaceMap.put("muws",muws);
String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
parentClassName+".xsd";

qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
schema.update();
}

I.e., you either need to use "xsd:schema" to create the element with a
prefix that matches the prefix you've defined or you need to use
put(null, xsd) to define the prefix according to the QName that will use it.

Is there some particularly compelling reason why you keep trying to
create the element manually? Doing schema.setDocument(document) will
avoid the mistakes you are making creating the element manually.


HT Ooi wrote:

> Hi,
>
> I have a problem when adding a element to a schema. A
> NullPointerException occurs although the schema and the element
> exist.What could the problem be?
> here is the class i am experimenting with.
>
> The error is in the addElements() method.
>
>
> public class Example {
>
> private XSDSchema schema;
> private File outputFile;
> private DocumentBuilderFactory docFactory;
> private Document document;
> private String parentClassName;
>
> private ArrayList currentProperties;
>
> private String root;
> private String xsd;
> private String muws;
>
> public Example (String inputParentClassName,String inputPathName){
> parentClassName = inputParentClassName;
> schema = XSDFactory.eINSTANCE.createXSDSchema();
> outputFile = new File(inputPathName + "//" +
> parentClassName+".xsd");
> createDocument();
> initializeSchema();
> initializeNamespaces();
> //addImports();
> addElements();
> save();
>
> }
>
> private void createDocument(){
> docFactory = DocumentBuilderFactory.newInstance();
> DocumentBuilder builder = null;
> try {
> builder = docFactory.newDocumentBuilder();
> } catch (ParserConfigurationException e1) {
> e1.printStackTrace();
> }
> document = (Document) builder.newDocument();
> }
>
> private void initializeSchema(){
> Element schemaElement = (Element)
> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
> schema.setElement(schemaElement);
> document.appendChild(schemaElement);
> }
>
> private void initializeNamespaces(){
>
> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>
> java.util.Map qNamePrefixToNamespaceMap =
> schema.getQNamePrefixToNamespaceMap();
> root = "http://dmtf.org/v2.8/";
> qNamePrefixToNamespaceMap.put("cim-root",root);
> xsd= "http://www.w3.org/2001/XMLSchema";
> qNamePrefixToNamespaceMap.put("xsd",xsd);
> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
> qNamePrefixToNamespaceMap.put("muws",muws);
> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
> parentClassName+".xsd";
>
> qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>
> schema.update();
> }
> private void addImports(){
> XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
> muwsImport.setNamespace(muws);
> muwsImport.setSchemaLocation("\\import\\MUWS.xsd");
> schema.getContents().add(muwsImport);
>
> XSDImport rootImport = XSDFactory.eINSTANCE.createXSDImport();
> rootImport.setNamespace(root);
> rootImport.setSchemaLocation("\\import\\CIM_root.xsd");
> schema.getContents().add(rootImport);
>
> }
>
> private void addElements(){
> XSDElementDeclarationImpl element =(XSDElementDeclarationImpl)
> XSDFactory.eINSTANCE.createXSDElementDeclaration();
> element.setName("anElement");
> schema.getContents().add(element);
> }
>
> public static void main(String[] args) {
> Example example1 = new Example("Example","C:\\Documents and
> Settings\\Administrator\\Desktop");
> }
>
> private void save(){
> try {
> Source source = new DOMSource(document);
> Result result = new StreamResult(outputFile);
> Transformer xformer =
> TransformerFactory.newInstance().newTransformer();
> xformer.transform(source,result);
> } catch (TransformerConfigurationException e) {
> e.printStackTrace();
> } catch (TransformerFactoryConfigurationError e) {
> e.printStackTrace();
> } catch (TransformerException e) {
> e.printStackTrace();
> }
>
>
>
>
>
> }
> }
>
> thank you very much.
>


--------------090207060905060305060804
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
HT,<br>
<br>
You need to make these match:<br>
<blockquote><small>
Re: java.lang.NullPointerException when adding Element [message #56789 is a reply to message #56762] Thu, 30 December 2004 15:32 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ooi.de.ibm.com

Hi Ed,

I used your suggestion,now using

private void initializeSchema(){
schema.setDocument(document);
}

instead of:

private void initializeSchema(){
Element schemaElement = (Element)
document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
schema.setElement(schemaElement);
document.appendChild(schemaElement);

}

i still got an exception at the same place. What would you advise?

Thank you again.




Ed Merks wrote:

> HT,

> You need to make these match:

> private void initializeSchema(){
> Element schemaElement = (Element)
> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001,
> *"schema"*);
> schema.setElement(schemaElement);
> document.appendChild(schemaElement);
> }

> private void initializeNamespaces(){

>
schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
> java.util.Map qNamePrefixToNamespaceMap =
> schema.getQNamePrefixToNamespaceMap();
> root = "http://dmtf.org/v2.8/";
> qNamePrefixToNamespaceMap.put("cim-root",root);
> xsd= "http://www.w3.org/2001/XMLSchema";
> qNamePrefixToNamespaceMap.put(*"xsd"*,xsd);
> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
> qNamePrefixToNamespaceMap.put("muws",muws);
> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
> parentClassName+".xsd";

>
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
> schema.update();
> }

> I.e., you either need to use "xsd:schema" to create the element with a
> prefix that matches the prefix you've defined or you need to use
> put(null, xsd) to define the prefix according to the QName that will use it.

> Is there some particularly compelling reason why you keep trying to
> create the element manually? Doing schema.setDocument(document) will
> avoid the mistakes you are making creating the element manually.


> HT Ooi wrote:

>> Hi,
>>
>> I have a problem when adding a element to a schema. A
>> NullPointerException occurs although the schema and the element
>> exist.What could the problem be?
>> here is the class i am experimenting with.
>>
>> The error is in the addElements() method.
>>
>>
>> public class Example {
>>
>> private XSDSchema schema;
>> private File outputFile;
>> private DocumentBuilderFactory docFactory;
>> private Document document;
>> private String parentClassName;
>>
>> private ArrayList currentProperties;
>>
>> private String root;
>> private String xsd;
>> private String muws;
>>
>> public Example (String inputParentClassName,String inputPathName){
>> parentClassName = inputParentClassName;
>> schema = XSDFactory.eINSTANCE.createXSDSchema();
>> outputFile = new File(inputPathName + "//" +
>> parentClassName+".xsd");
>> createDocument();
>> initializeSchema();
>> initializeNamespaces();
>> //addImports();
>> addElements();
>> save();
>>
>> }
>>
>> private void createDocument(){
>> docFactory = DocumentBuilderFactory.newInstance();
>> DocumentBuilder builder = null;
>> try {
>> builder = docFactory.newDocumentBuilder();
>> } catch (ParserConfigurationException e1) {
>> e1.printStackTrace();
>> }
>> document = (Document) builder.newDocument();
>> }
>>
>> private void initializeSchema(){
>> Element schemaElement = (Element)
>> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
>> schema.setElement(schemaElement);
>> document.appendChild(schemaElement);
>> }
>>
>> private void initializeNamespaces(){
>>
>> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>>
>> java.util.Map qNamePrefixToNamespaceMap =
>> schema.getQNamePrefixToNamespaceMap();
>> root = "http://dmtf.org/v2.8/";
>> qNamePrefixToNamespaceMap.put("cim-root",root);
>> xsd= "http://www.w3.org/2001/XMLSchema";
>> qNamePrefixToNamespaceMap.put("xsd",xsd);
>> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
>> qNamePrefixToNamespaceMap.put("muws",muws);
>> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
>> parentClassName+".xsd";
>>
>>
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>>
>> schema.update();
>> }
>> private void addImports(){
>> XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
>> muwsImport.setNamespace(muws);
>> muwsImport.setSchemaLocation("\import\MUWS.xsd");
>> schema.getContents().add(muwsImport);
>>
>> XSDImport rootImport = XSDFactory.eINSTANCE.createXSDImport();
>> rootImport.setNamespace(root);
>> rootImport.setSchemaLocation("\import\CIM_root.xsd");
>> schema.getContents().add(rootImport);
>>
>> }
>>
>> private void addElements(){
>> XSDElementDeclarationImpl element =(XSDElementDeclarationImpl)
>> XSDFactory.eINSTANCE.createXSDElementDeclaration();
>> element.setName("anElement");
>> schema.getContents().add(element);
>> }
>>
>> public static void main(String[] args) {
>> Example example1 = new Example("Example","C:\Documents and
>> Settings\Administrator\Desktop");
>> }
>>
>> private void save(){
>> try {
>> Source source = new DOMSource(document);
>> Result result = new StreamResult(outputFile);
>> Transformer xformer =
>> TransformerFactory.newInstance().newTransformer();
>> xformer.transform(source,result);
>> } catch (TransformerConfigurationException e) {
>> e.printStackTrace();
>> } catch (TransformerFactoryConfigurationError e) {
>> e.printStackTrace();
>> } catch (TransformerException e) {
>> e.printStackTrace();
>> }
>>
>>
>>
>>
>>
>> }
>> }
>>
>> thank you very much.
>>
Re: java.lang.NullPointerException when adding Element [message #56817 is a reply to message #56762] Thu, 30 December 2004 15:48 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: ooi.de.ibm.com

Hi Ed,

I used your suggestion,now using

private void initializeSchema(){
schema.setDocument(document);
}

instead of:

private void initializeSchema(){
Element schemaElement = (Element)
document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
schema.setElement(schemaElement);
document.appendChild(schemaElement);

}

i still got an exception at the same place. What would you advise?

Thank you again.




Ed Merks wrote:

> HT,

> You need to make these match:

> private void initializeSchema(){
> Element schemaElement = (Element)
> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001,
> *"schema"*);
> schema.setElement(schemaElement);
> document.appendChild(schemaElement);
> }

> private void initializeNamespaces(){

>
schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
> java.util.Map qNamePrefixToNamespaceMap =
> schema.getQNamePrefixToNamespaceMap();
> root = "http://dmtf.org/v2.8/";
> qNamePrefixToNamespaceMap.put("cim-root",root);
> xsd= "http://www.w3.org/2001/XMLSchema";
> qNamePrefixToNamespaceMap.put(*"xsd"*,xsd);
> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
> qNamePrefixToNamespaceMap.put("muws",muws);
> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
> parentClassName+".xsd";

>
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
> schema.update();
> }

> I.e., you either need to use "xsd:schema" to create the element with a
> prefix that matches the prefix you've defined or you need to use
> put(null, xsd) to define the prefix according to the QName that will use it.

> Is there some particularly compelling reason why you keep trying to
> create the element manually? Doing schema.setDocument(document) will
> avoid the mistakes you are making creating the element manually.


> HT Ooi wrote:

>> Hi,
>>
>> I have a problem when adding a element to a schema. A
>> NullPointerException occurs although the schema and the element
>> exist.What could the problem be?
>> here is the class i am experimenting with.
>>
>> The error is in the addElements() method.
>>
>>
>> public class Example {
>>
>> private XSDSchema schema;
>> private File outputFile;
>> private DocumentBuilderFactory docFactory;
>> private Document document;
>> private String parentClassName;
>>
>> private ArrayList currentProperties;
>>
>> private String root;
>> private String xsd;
>> private String muws;
>>
>> public Example (String inputParentClassName,String inputPathName){
>> parentClassName = inputParentClassName;
>> schema = XSDFactory.eINSTANCE.createXSDSchema();
>> outputFile = new File(inputPathName + "//" +
>> parentClassName+".xsd");
>> createDocument();
>> initializeSchema();
>> initializeNamespaces();
>> //addImports();
>> addElements();
>> save();
>>
>> }
>>
>> private void createDocument(){
>> docFactory = DocumentBuilderFactory.newInstance();
>> DocumentBuilder builder = null;
>> try {
>> builder = docFactory.newDocumentBuilder();
>> } catch (ParserConfigurationException e1) {
>> e1.printStackTrace();
>> }
>> document = (Document) builder.newDocument();
>> }
>>
>> private void initializeSchema(){
>> Element schemaElement = (Element)
>> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
>> schema.setElement(schemaElement);
>> document.appendChild(schemaElement);
>> }
>>
>> private void initializeNamespaces(){
>>
>> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>>
>> java.util.Map qNamePrefixToNamespaceMap =
>> schema.getQNamePrefixToNamespaceMap();
>> root = "http://dmtf.org/v2.8/";
>> qNamePrefixToNamespaceMap.put("cim-root",root);
>> xsd= "http://www.w3.org/2001/XMLSchema";
>> qNamePrefixToNamespaceMap.put("xsd",xsd);
>> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
>> qNamePrefixToNamespaceMap.put("muws",muws);
>> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
>> parentClassName+".xsd";
>>
>>
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>>
>> schema.update();
>> }
>> private void addImports(){
>> XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
>> muwsImport.setNamespace(muws);
>> muwsImport.setSchemaLocation("\import\MUWS.xsd");
>> schema.getContents().add(muwsImport);
>>
>> XSDImport rootImport = XSDFactory.eINSTANCE.createXSDImport();
>> rootImport.setNamespace(root);
>> rootImport.setSchemaLocation("\import\CIM_root.xsd");
>> schema.getContents().add(rootImport);
>>
>> }
>>
>> private void addElements(){
>> XSDElementDeclarationImpl element =(XSDElementDeclarationImpl)
>> XSDFactory.eINSTANCE.createXSDElementDeclaration();
>> element.setName("anElement");
>> schema.getContents().add(element);
>> }
>>
>> public static void main(String[] args) {
>> Example example1 = new Example("Example","C:\Documents and
>> Settings\Administrator\Desktop");
>> }
>>
>> private void save(){
>> try {
>> Source source = new DOMSource(document);
>> Result result = new StreamResult(outputFile);
>> Transformer xformer =
>> TransformerFactory.newInstance().newTransformer();
>> xformer.transform(source,result);
>> } catch (TransformerConfigurationException e) {
>> e.printStackTrace();
>> } catch (TransformerFactoryConfigurationError e) {
>> e.printStackTrace();
>> } catch (TransformerException e) {
>> e.printStackTrace();
>> }
>>
>>
>>
>>
>>
>> }
>> }
>>
>> thank you very much.
>>
Re: java.lang.NullPointerException when adding Element [message #56843 is a reply to message #56789] Thu, 30 December 2004 16:11 Go to previous message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------040600070305030002050200
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

HT,

Did you do this as we established in previous notes was necessary:

qNamePrefixToNamespaceMap.put
(xsdSchema.getSchemaForSchemaQNamePrefix(),
XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);

You probably just left the explicit "xsd" and never made sure that
xsdSchema.setSchemaForSchemaQNamePrefix("xsd") was called. (I changed
it to null to match "schema" as I explained.) All these different
things you are trying and having problems with are all variations on the
same theme: choosing a prefix, defining an xmlns declaration to bind
that prefix to the right schema for schema namespace, and using that
prefix consistently.

Your next problem you have will be that you are bypassing this code in
XSDResourceImpl.doSave when you are saving the Document directly, so you
need to ensure that the element is demand created using updateElement if
necessary.

protected void doSave(OutputStream os, Map options) throws IOException
{
XSDSchema xsdSchema = getSchema();
if (xsdSchema != null)
{
* Document document = xsdSchema.getDocument();
if (document == null)
{
xsdSchema.updateDocument();
document = xsdSchema.getDocument();
}

if (xsdSchema.getElement() == null)
{
xsdSchema.updateElement(); <<<<<
}*

doSerialize(os, document, options == null ? null :
(String)options.get(XSD_ENCODING));
}
}



HT Ooi wrote:

> Hi Ed,
>
> I used your suggestion,now using
> private void initializeSchema(){
> schema.setDocument(document);
> }
>
> instead of:
>
> private void initializeSchema(){
> Element schemaElement = (Element)
> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
> schema.setElement(schemaElement);
> document.appendChild(schemaElement);
>
> }
>
> i still got an exception at the same place. What would you advise?
>
> Thank you again.
>
>
>
>
> Ed Merks wrote:
>
>> HT,
>
>
>> You need to make these match:
>
>
>> private void initializeSchema(){
>> Element schemaElement = (Element)
>> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001,
>> *"schema"*);
>> schema.setElement(schemaElement);
>> document.appendChild(schemaElement);
>> }
>
>
>> private void initializeNamespaces(){
>
>
>>
>
> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>
>
>> java.util.Map qNamePrefixToNamespaceMap =
>> schema.getQNamePrefixToNamespaceMap();
>> root = "http://dmtf.org/v2.8/";
>> qNamePrefixToNamespaceMap.put("cim-root",root);
>> xsd= "http://www.w3.org/2001/XMLSchema";
>> qNamePrefixToNamespaceMap.put(*"xsd"*,xsd);
>> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
>> qNamePrefixToNamespaceMap.put("muws",muws);
>> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
>> parentClassName+".xsd";
>
>
>>
>
> qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>
>
>> schema.update();
>> }
>
>
>> I.e., you either need to use "xsd:schema" to create the element with
>> a prefix that matches the prefix you've defined or you need to use
>> put(null, xsd) to define the prefix according to the QName that will
>> use it.
>
>
>> Is there some particularly compelling reason why you keep trying to
>> create the element manually? Doing schema.setDocument(document) will
>> avoid the mistakes you are making creating the element manually.
>
>
>
>> HT Ooi wrote:
>
>
>>> Hi,
>>>
>>> I have a problem when adding a element to a schema. A
>>> NullPointerException occurs although the schema and the element
>>> exist.What could the problem be?
>>> here is the class i am experimenting with.
>>>
>>> The error is in the addElements() method.
>>>
>>>
>>> public class Example {
>>> private XSDSchema schema;
>>> private File outputFile;
>>> private DocumentBuilderFactory docFactory;
>>> private Document document;
>>> private String parentClassName;
>>> private ArrayList currentProperties;
>>> private String root;
>>> private String xsd;
>>> private String muws;
>>> public Example (String inputParentClassName,String
>>> inputPathName){
>>> parentClassName = inputParentClassName;
>>> schema = XSDFactory.eINSTANCE.createXSDSchema();
>>> outputFile = new File(inputPathName + "//" +
>>> parentClassName+".xsd");
>>> createDocument();
>>> initializeSchema();
>>> initializeNamespaces();
>>> //addImports();
>>> addElements();
>>> save();
>>> }
>>> private void createDocument(){
>>> docFactory = DocumentBuilderFactory.newInstance();
>>> DocumentBuilder builder = null;
>>> try {
>>> builder = docFactory.newDocumentBuilder();
>>> } catch (ParserConfigurationException e1) {
>>> e1.printStackTrace();
>>> }
>>> document = (Document) builder.newDocument(); }
>>> private void initializeSchema(){
>>> Element schemaElement = (Element)
>>> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
>>> schema.setElement(schemaElement);
>>> document.appendChild(schemaElement);
>>> }
>>> private void initializeNamespaces(){
>>>
>>> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>>>
>>> java.util.Map qNamePrefixToNamespaceMap =
>>> schema.getQNamePrefixToNamespaceMap();
>>> root = "http://dmtf.org/v2.8/";
>>> qNamePrefixToNamespaceMap.put("cim-root",root);
>>> xsd= "http://www.w3.org/2001/XMLSchema";
>>> qNamePrefixToNamespaceMap.put("xsd",xsd);
>>> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
>>> qNamePrefixToNamespaceMap.put("muws",muws);
>>> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
>>> parentClassName+".xsd";
>>>
>>
> qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>
>
>>>
>>> schema.update();
>>> }
>>> private void addImports(){
>>> XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
>>> muwsImport.setNamespace(muws);
>>> muwsImport.setSchemaLocation("\import\MUWS.xsd");
>>> schema.getContents().add(muwsImport);
>>> XSDImport rootImport =
>>> XSDFactory.eINSTANCE.createXSDImport();
>>> rootImport.setNamespace(root);
>>> rootImport.setSchemaLocation("\import\CIM_root.xsd");
>>> schema.getContents().add(rootImport);
>>> }
>>> private void addElements(){
>>> XSDElementDeclarationImpl element
>>> =(XSDElementDeclarationImpl)
>>> XSDFactory.eINSTANCE.createXSDElementDeclaration();
>>> element.setName("anElement");
>>> schema.getContents().add(element);
>>> }
>>>
>>> public static void main(String[] args) {
>>> Example example1 = new Example("Example","C:\Documents and
>>> Settings\Administrator\Desktop");
>>> }
>>> private void save(){
>>> try {
>>> Source source = new DOMSource(document);
>>> Result result = new StreamResult(outputFile);
>>> Transformer xformer =
>>> TransformerFactory.newInstance().newTransformer();
>>> xformer.transform(source,result);
>>> } catch (TransformerConfigurationException e) {
>>> e.printStackTrace();
>>> } catch (TransformerFactoryConfigurationError e) {
>>> e.printStackTrace();
>>> } catch (TransformerException e) {
>>> e.printStackTrace();
>>> }
>>>
>>> }
>>> }
>>>
>>> thank you very much.
>>>
>
>


--------------040600070305030002050200
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
HT,<br>
<br>
Did you do this as we established in previous notes was necessary:<br>
<blockquote>qNamePrefixToNamespaceMap.put<br>
Re: java.lang.NullPointerException when adding Element [message #593424 is a reply to message #56707] Thu, 30 December 2004 13:26 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33252
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------090207060905060305060804
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

HT,

You need to make these match:

private void initializeSchema(){
Element schemaElement = (Element)
document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001,
*"schema"*);
schema.setElement(schemaElement);
document.appendChild(schemaElement);
}

private void initializeNamespaces(){

schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
java.util.Map qNamePrefixToNamespaceMap =
schema.getQNamePrefixToNamespaceMap();
root = "http://dmtf.org/v2.8/";
qNamePrefixToNamespaceMap.put("cim-root",root);
xsd= "http://www.w3.org/2001/XMLSchema";
qNamePrefixToNamespaceMap.put(*"xsd"*,xsd);
muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
qNamePrefixToNamespaceMap.put("muws",muws);
String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
parentClassName+".xsd";

qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
schema.update();
}

I.e., you either need to use "xsd:schema" to create the element with a
prefix that matches the prefix you've defined or you need to use
put(null, xsd) to define the prefix according to the QName that will use it.

Is there some particularly compelling reason why you keep trying to
create the element manually? Doing schema.setDocument(document) will
avoid the mistakes you are making creating the element manually.


HT Ooi wrote:

> Hi,
>
> I have a problem when adding a element to a schema. A
> NullPointerException occurs although the schema and the element
> exist.What could the problem be?
> here is the class i am experimenting with.
>
> The error is in the addElements() method.
>
>
> public class Example {
>
> private XSDSchema schema;
> private File outputFile;
> private DocumentBuilderFactory docFactory;
> private Document document;
> private String parentClassName;
>
> private ArrayList currentProperties;
>
> private String root;
> private String xsd;
> private String muws;
>
> public Example (String inputParentClassName,String inputPathName){
> parentClassName = inputParentClassName;
> schema = XSDFactory.eINSTANCE.createXSDSchema();
> outputFile = new File(inputPathName + "//" +
> parentClassName+".xsd");
> createDocument();
> initializeSchema();
> initializeNamespaces();
> //addImports();
> addElements();
> save();
>
> }
>
> private void createDocument(){
> docFactory = DocumentBuilderFactory.newInstance();
> DocumentBuilder builder = null;
> try {
> builder = docFactory.newDocumentBuilder();
> } catch (ParserConfigurationException e1) {
> e1.printStackTrace();
> }
> document = (Document) builder.newDocument();
> }
>
> private void initializeSchema(){
> Element schemaElement = (Element)
> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
> schema.setElement(schemaElement);
> document.appendChild(schemaElement);
> }
>
> private void initializeNamespaces(){
>
> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>
> java.util.Map qNamePrefixToNamespaceMap =
> schema.getQNamePrefixToNamespaceMap();
> root = "http://dmtf.org/v2.8/";
> qNamePrefixToNamespaceMap.put("cim-root",root);
> xsd= "http://www.w3.org/2001/XMLSchema";
> qNamePrefixToNamespaceMap.put("xsd",xsd);
> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
> qNamePrefixToNamespaceMap.put("muws",muws);
> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
> parentClassName+".xsd";
>
> qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>
> schema.update();
> }
> private void addImports(){
> XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
> muwsImport.setNamespace(muws);
> muwsImport.setSchemaLocation("\\import\\MUWS.xsd");
> schema.getContents().add(muwsImport);
>
> XSDImport rootImport = XSDFactory.eINSTANCE.createXSDImport();
> rootImport.setNamespace(root);
> rootImport.setSchemaLocation("\\import\\CIM_root.xsd");
> schema.getContents().add(rootImport);
>
> }
>
> private void addElements(){
> XSDElementDeclarationImpl element =(XSDElementDeclarationImpl)
> XSDFactory.eINSTANCE.createXSDElementDeclaration();
> element.setName("anElement");
> schema.getContents().add(element);
> }
>
> public static void main(String[] args) {
> Example example1 = new Example("Example","C:\\Documents and
> Settings\\Administrator\\Desktop");
> }
>
> private void save(){
> try {
> Source source = new DOMSource(document);
> Result result = new StreamResult(outputFile);
> Transformer xformer =
> TransformerFactory.newInstance().newTransformer();
> xformer.transform(source,result);
> } catch (TransformerConfigurationException e) {
> e.printStackTrace();
> } catch (TransformerFactoryConfigurationError e) {
> e.printStackTrace();
> } catch (TransformerException e) {
> e.printStackTrace();
> }
>
>
>
>
>
> }
> }
>
> thank you very much.
>


--------------090207060905060305060804
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
HT,<br>
<br>
You need to make these match:<br>
<blockquote><small>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: java.lang.NullPointerException when adding Element [message #593439 is a reply to message #56762] Thu, 30 December 2004 15:32 Go to previous message
Eclipse UserFriend
Originally posted by: ooi.de.ibm.com

Hi Ed,

I used your suggestion,now using

private void initializeSchema(){
schema.setDocument(document);
}

instead of:

private void initializeSchema(){
Element schemaElement = (Element)
document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
schema.setElement(schemaElement);
document.appendChild(schemaElement);

}

i still got an exception at the same place. What would you advise?

Thank you again.




Ed Merks wrote:

> HT,

> You need to make these match:

> private void initializeSchema(){
> Element schemaElement = (Element)
> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001,
> *"schema"*);
> schema.setElement(schemaElement);
> document.appendChild(schemaElement);
> }

> private void initializeNamespaces(){

>
schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
> java.util.Map qNamePrefixToNamespaceMap =
> schema.getQNamePrefixToNamespaceMap();
> root = "http://dmtf.org/v2.8/";
> qNamePrefixToNamespaceMap.put("cim-root",root);
> xsd= "http://www.w3.org/2001/XMLSchema";
> qNamePrefixToNamespaceMap.put(*"xsd"*,xsd);
> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
> qNamePrefixToNamespaceMap.put("muws",muws);
> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
> parentClassName+".xsd";

>
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
> schema.update();
> }

> I.e., you either need to use "xsd:schema" to create the element with a
> prefix that matches the prefix you've defined or you need to use
> put(null, xsd) to define the prefix according to the QName that will use it.

> Is there some particularly compelling reason why you keep trying to
> create the element manually? Doing schema.setDocument(document) will
> avoid the mistakes you are making creating the element manually.


> HT Ooi wrote:

>> Hi,
>>
>> I have a problem when adding a element to a schema. A
>> NullPointerException occurs although the schema and the element
>> exist.What could the problem be?
>> here is the class i am experimenting with.
>>
>> The error is in the addElements() method.
>>
>>
>> public class Example {
>>
>> private XSDSchema schema;
>> private File outputFile;
>> private DocumentBuilderFactory docFactory;
>> private Document document;
>> private String parentClassName;
>>
>> private ArrayList currentProperties;
>>
>> private String root;
>> private String xsd;
>> private String muws;
>>
>> public Example (String inputParentClassName,String inputPathName){
>> parentClassName = inputParentClassName;
>> schema = XSDFactory.eINSTANCE.createXSDSchema();
>> outputFile = new File(inputPathName + "//" +
>> parentClassName+".xsd");
>> createDocument();
>> initializeSchema();
>> initializeNamespaces();
>> //addImports();
>> addElements();
>> save();
>>
>> }
>>
>> private void createDocument(){
>> docFactory = DocumentBuilderFactory.newInstance();
>> DocumentBuilder builder = null;
>> try {
>> builder = docFactory.newDocumentBuilder();
>> } catch (ParserConfigurationException e1) {
>> e1.printStackTrace();
>> }
>> document = (Document) builder.newDocument();
>> }
>>
>> private void initializeSchema(){
>> Element schemaElement = (Element)
>> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
>> schema.setElement(schemaElement);
>> document.appendChild(schemaElement);
>> }
>>
>> private void initializeNamespaces(){
>>
>> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>>
>> java.util.Map qNamePrefixToNamespaceMap =
>> schema.getQNamePrefixToNamespaceMap();
>> root = "http://dmtf.org/v2.8/";
>> qNamePrefixToNamespaceMap.put("cim-root",root);
>> xsd= "http://www.w3.org/2001/XMLSchema";
>> qNamePrefixToNamespaceMap.put("xsd",xsd);
>> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
>> qNamePrefixToNamespaceMap.put("muws",muws);
>> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
>> parentClassName+".xsd";
>>
>>
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>>
>> schema.update();
>> }
>> private void addImports(){
>> XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
>> muwsImport.setNamespace(muws);
>> muwsImport.setSchemaLocation("\import\MUWS.xsd");
>> schema.getContents().add(muwsImport);
>>
>> XSDImport rootImport = XSDFactory.eINSTANCE.createXSDImport();
>> rootImport.setNamespace(root);
>> rootImport.setSchemaLocation("\import\CIM_root.xsd");
>> schema.getContents().add(rootImport);
>>
>> }
>>
>> private void addElements(){
>> XSDElementDeclarationImpl element =(XSDElementDeclarationImpl)
>> XSDFactory.eINSTANCE.createXSDElementDeclaration();
>> element.setName("anElement");
>> schema.getContents().add(element);
>> }
>>
>> public static void main(String[] args) {
>> Example example1 = new Example("Example","C:\Documents and
>> Settings\Administrator\Desktop");
>> }
>>
>> private void save(){
>> try {
>> Source source = new DOMSource(document);
>> Result result = new StreamResult(outputFile);
>> Transformer xformer =
>> TransformerFactory.newInstance().newTransformer();
>> xformer.transform(source,result);
>> } catch (TransformerConfigurationException e) {
>> e.printStackTrace();
>> } catch (TransformerFactoryConfigurationError e) {
>> e.printStackTrace();
>> } catch (TransformerException e) {
>> e.printStackTrace();
>> }
>>
>>
>>
>>
>>
>> }
>> }
>>
>> thank you very much.
>>
Re: java.lang.NullPointerException when adding Element [message #593455 is a reply to message #56762] Thu, 30 December 2004 15:48 Go to previous message
Eclipse UserFriend
Originally posted by: ooi.de.ibm.com

Hi Ed,

I used your suggestion,now using

private void initializeSchema(){
schema.setDocument(document);
}

instead of:

private void initializeSchema(){
Element schemaElement = (Element)
document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
schema.setElement(schemaElement);
document.appendChild(schemaElement);

}

i still got an exception at the same place. What would you advise?

Thank you again.




Ed Merks wrote:

> HT,

> You need to make these match:

> private void initializeSchema(){
> Element schemaElement = (Element)
> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001,
> *"schema"*);
> schema.setElement(schemaElement);
> document.appendChild(schemaElement);
> }

> private void initializeNamespaces(){

>
schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
> java.util.Map qNamePrefixToNamespaceMap =
> schema.getQNamePrefixToNamespaceMap();
> root = "http://dmtf.org/v2.8/";
> qNamePrefixToNamespaceMap.put("cim-root",root);
> xsd= "http://www.w3.org/2001/XMLSchema";
> qNamePrefixToNamespaceMap.put(*"xsd"*,xsd);
> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
> qNamePrefixToNamespaceMap.put("muws",muws);
> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
> parentClassName+".xsd";

>
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
> schema.update();
> }

> I.e., you either need to use "xsd:schema" to create the element with a
> prefix that matches the prefix you've defined or you need to use
> put(null, xsd) to define the prefix according to the QName that will use it.

> Is there some particularly compelling reason why you keep trying to
> create the element manually? Doing schema.setDocument(document) will
> avoid the mistakes you are making creating the element manually.


> HT Ooi wrote:

>> Hi,
>>
>> I have a problem when adding a element to a schema. A
>> NullPointerException occurs although the schema and the element
>> exist.What could the problem be?
>> here is the class i am experimenting with.
>>
>> The error is in the addElements() method.
>>
>>
>> public class Example {
>>
>> private XSDSchema schema;
>> private File outputFile;
>> private DocumentBuilderFactory docFactory;
>> private Document document;
>> private String parentClassName;
>>
>> private ArrayList currentProperties;
>>
>> private String root;
>> private String xsd;
>> private String muws;
>>
>> public Example (String inputParentClassName,String inputPathName){
>> parentClassName = inputParentClassName;
>> schema = XSDFactory.eINSTANCE.createXSDSchema();
>> outputFile = new File(inputPathName + "//" +
>> parentClassName+".xsd");
>> createDocument();
>> initializeSchema();
>> initializeNamespaces();
>> //addImports();
>> addElements();
>> save();
>>
>> }
>>
>> private void createDocument(){
>> docFactory = DocumentBuilderFactory.newInstance();
>> DocumentBuilder builder = null;
>> try {
>> builder = docFactory.newDocumentBuilder();
>> } catch (ParserConfigurationException e1) {
>> e1.printStackTrace();
>> }
>> document = (Document) builder.newDocument();
>> }
>>
>> private void initializeSchema(){
>> Element schemaElement = (Element)
>> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
>> schema.setElement(schemaElement);
>> document.appendChild(schemaElement);
>> }
>>
>> private void initializeNamespaces(){
>>
>> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>>
>> java.util.Map qNamePrefixToNamespaceMap =
>> schema.getQNamePrefixToNamespaceMap();
>> root = "http://dmtf.org/v2.8/";
>> qNamePrefixToNamespaceMap.put("cim-root",root);
>> xsd= "http://www.w3.org/2001/XMLSchema";
>> qNamePrefixToNamespaceMap.put("xsd",xsd);
>> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
>> qNamePrefixToNamespaceMap.put("muws",muws);
>> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
>> parentClassName+".xsd";
>>
>>
qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>>
>> schema.update();
>> }
>> private void addImports(){
>> XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
>> muwsImport.setNamespace(muws);
>> muwsImport.setSchemaLocation("\import\MUWS.xsd");
>> schema.getContents().add(muwsImport);
>>
>> XSDImport rootImport = XSDFactory.eINSTANCE.createXSDImport();
>> rootImport.setNamespace(root);
>> rootImport.setSchemaLocation("\import\CIM_root.xsd");
>> schema.getContents().add(rootImport);
>>
>> }
>>
>> private void addElements(){
>> XSDElementDeclarationImpl element =(XSDElementDeclarationImpl)
>> XSDFactory.eINSTANCE.createXSDElementDeclaration();
>> element.setName("anElement");
>> schema.getContents().add(element);
>> }
>>
>> public static void main(String[] args) {
>> Example example1 = new Example("Example","C:\Documents and
>> Settings\Administrator\Desktop");
>> }
>>
>> private void save(){
>> try {
>> Source source = new DOMSource(document);
>> Result result = new StreamResult(outputFile);
>> Transformer xformer =
>> TransformerFactory.newInstance().newTransformer();
>> xformer.transform(source,result);
>> } catch (TransformerConfigurationException e) {
>> e.printStackTrace();
>> } catch (TransformerFactoryConfigurationError e) {
>> e.printStackTrace();
>> } catch (TransformerException e) {
>> e.printStackTrace();
>> }
>>
>>
>>
>>
>>
>> }
>> }
>>
>> thank you very much.
>>
Re: java.lang.NullPointerException when adding Element [message #593467 is a reply to message #56789] Thu, 30 December 2004 16:11 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33252
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------040600070305030002050200
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

HT,

Did you do this as we established in previous notes was necessary:

qNamePrefixToNamespaceMap.put
(xsdSchema.getSchemaForSchemaQNamePrefix(),
XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001);

You probably just left the explicit "xsd" and never made sure that
xsdSchema.setSchemaForSchemaQNamePrefix("xsd") was called. (I changed
it to null to match "schema" as I explained.) All these different
things you are trying and having problems with are all variations on the
same theme: choosing a prefix, defining an xmlns declaration to bind
that prefix to the right schema for schema namespace, and using that
prefix consistently.

Your next problem you have will be that you are bypassing this code in
XSDResourceImpl.doSave when you are saving the Document directly, so you
need to ensure that the element is demand created using updateElement if
necessary.

protected void doSave(OutputStream os, Map options) throws IOException
{
XSDSchema xsdSchema = getSchema();
if (xsdSchema != null)
{
* Document document = xsdSchema.getDocument();
if (document == null)
{
xsdSchema.updateDocument();
document = xsdSchema.getDocument();
}

if (xsdSchema.getElement() == null)
{
xsdSchema.updateElement(); <<<<<
}*

doSerialize(os, document, options == null ? null :
(String)options.get(XSD_ENCODING));
}
}



HT Ooi wrote:

> Hi Ed,
>
> I used your suggestion,now using
> private void initializeSchema(){
> schema.setDocument(document);
> }
>
> instead of:
>
> private void initializeSchema(){
> Element schemaElement = (Element)
> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
> schema.setElement(schemaElement);
> document.appendChild(schemaElement);
>
> }
>
> i still got an exception at the same place. What would you advise?
>
> Thank you again.
>
>
>
>
> Ed Merks wrote:
>
>> HT,
>
>
>> You need to make these match:
>
>
>> private void initializeSchema(){
>> Element schemaElement = (Element)
>> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001,
>> *"schema"*);
>> schema.setElement(schemaElement);
>> document.appendChild(schemaElement);
>> }
>
>
>> private void initializeNamespaces(){
>
>
>>
>
> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>
>
>> java.util.Map qNamePrefixToNamespaceMap =
>> schema.getQNamePrefixToNamespaceMap();
>> root = "http://dmtf.org/v2.8/";
>> qNamePrefixToNamespaceMap.put("cim-root",root);
>> xsd= "http://www.w3.org/2001/XMLSchema";
>> qNamePrefixToNamespaceMap.put(*"xsd"*,xsd);
>> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
>> qNamePrefixToNamespaceMap.put("muws",muws);
>> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
>> parentClassName+".xsd";
>
>
>>
>
> qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>
>
>> schema.update();
>> }
>
>
>> I.e., you either need to use "xsd:schema" to create the element with
>> a prefix that matches the prefix you've defined or you need to use
>> put(null, xsd) to define the prefix according to the QName that will
>> use it.
>
>
>> Is there some particularly compelling reason why you keep trying to
>> create the element manually? Doing schema.setDocument(document) will
>> avoid the mistakes you are making creating the element manually.
>
>
>
>> HT Ooi wrote:
>
>
>>> Hi,
>>>
>>> I have a problem when adding a element to a schema. A
>>> NullPointerException occurs although the schema and the element
>>> exist.What could the problem be?
>>> here is the class i am experimenting with.
>>>
>>> The error is in the addElements() method.
>>>
>>>
>>> public class Example {
>>> private XSDSchema schema;
>>> private File outputFile;
>>> private DocumentBuilderFactory docFactory;
>>> private Document document;
>>> private String parentClassName;
>>> private ArrayList currentProperties;
>>> private String root;
>>> private String xsd;
>>> private String muws;
>>> public Example (String inputParentClassName,String
>>> inputPathName){
>>> parentClassName = inputParentClassName;
>>> schema = XSDFactory.eINSTANCE.createXSDSchema();
>>> outputFile = new File(inputPathName + "//" +
>>> parentClassName+".xsd");
>>> createDocument();
>>> initializeSchema();
>>> initializeNamespaces();
>>> //addImports();
>>> addElements();
>>> save();
>>> }
>>> private void createDocument(){
>>> docFactory = DocumentBuilderFactory.newInstance();
>>> DocumentBuilder builder = null;
>>> try {
>>> builder = docFactory.newDocumentBuilder();
>>> } catch (ParserConfigurationException e1) {
>>> e1.printStackTrace();
>>> }
>>> document = (Document) builder.newDocument(); }
>>> private void initializeSchema(){
>>> Element schemaElement = (Element)
>>> document.createElementNS(XSDUtil.SCHEMA_FOR_SCHEMA_URI_2001, "schema");
>>> schema.setElement(schemaElement);
>>> document.appendChild(schemaElement);
>>> }
>>> private void initializeNamespaces(){
>>>
>>> schema.setTargetNamespace("http://dmtf.org/v2.8/"+parentClassName+".xsd");
>>>
>>> java.util.Map qNamePrefixToNamespaceMap =
>>> schema.getQNamePrefixToNamespaceMap();
>>> root = "http://dmtf.org/v2.8/";
>>> qNamePrefixToNamespaceMap.put("cim-root",root);
>>> xsd= "http://www.w3.org/2001/XMLSchema";
>>> qNamePrefixToNamespaceMap.put("xsd",xsd);
>>> muws = "http://docs.oasis-open.org/wsdm/2004/XX/muws/schema";
>>> qNamePrefixToNamespaceMap.put("muws",muws);
>>> String currentParentXSDnamespace = "http://dmtf.org/v2.8/"+
>>> parentClassName+".xsd";
>>>
>>
> qNamePrefixToNamespaceMap.put(parentClassName+"-xsd",currentParentXSDnamespace);
>
>
>>>
>>> schema.update();
>>> }
>>> private void addImports(){
>>> XSDImport muwsImport = XSDFactory.eINSTANCE.createXSDImport();
>>> muwsImport.setNamespace(muws);
>>> muwsImport.setSchemaLocation("\import\MUWS.xsd");
>>> schema.getContents().add(muwsImport);
>>> XSDImport rootImport =
>>> XSDFactory.eINSTANCE.createXSDImport();
>>> rootImport.setNamespace(root);
>>> rootImport.setSchemaLocation("\import\CIM_root.xsd");
>>> schema.getContents().add(rootImport);
>>> }
>>> private void addElements(){
>>> XSDElementDeclarationImpl element
>>> =(XSDElementDeclarationImpl)
>>> XSDFactory.eINSTANCE.createXSDElementDeclaration();
>>> element.setName("anElement");
>>> schema.getContents().add(element);
>>> }
>>>
>>> public static void main(String[] args) {
>>> Example example1 = new Example("Example","C:\Documents and
>>> Settings\Administrator\Desktop");
>>> }
>>> private void save(){
>>> try {
>>> Source source = new DOMSource(document);
>>> Result result = new StreamResult(outputFile);
>>> Transformer xformer =
>>> TransformerFactory.newInstance().newTransformer();
>>> xformer.transform(source,result);
>>> } catch (TransformerConfigurationException e) {
>>> e.printStackTrace();
>>> } catch (TransformerFactoryConfigurationError e) {
>>> e.printStackTrace();
>>> } catch (TransformerException e) {
>>> e.printStackTrace();
>>> }
>>>
>>> }
>>> }
>>>
>>> thank you very much.
>>>
>
>


--------------040600070305030002050200
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
HT,<br>
<br>
Did you do this as we established in previous notes was necessary:<br>
<blockquote>qNamePrefixToNamespaceMap.put<br>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:XSDConstrainingFacet.isConstraintSatisfied
Next Topic:Performance issues with loading xsds
Goto Forum:
  


Current Time: Thu Nov 07 05:09:21 GMT 2024

Powered by FUDForum. Page generated in 0.04147 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top