You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by je...@dofasco.ca on 2004/10/26 22:49:56 UTC

Adding namespace prefixes not working properly


I am trying to create an xml document with a specified prefix.  I am
attaching the code that i am using. I would expect taht once you define
the XmlOptions setSaveSuggestedPrefixes that it would apply the prefixes
when the xmlText method is called on the XmlObject.  What happens is
that the xml is generated without the prefixes, actually it is identical
to what was passed in.

Kudos to wohever may help. I've been banging myhead against this for
awhile.

Thanks

Jeremy


// Code
XmlObject xml = XmlObject.Factory.parse(new File("C:\\msg.xml"));

HashMap suggestedPrefixes = new HashMap();
suggestedPrefixes.put("http://www.compnay.ca/hamilton/CompanyXMLMessage"
, "foo");
XmlOptions opts = new XmlOptions();
opts.setSaveSuggestedPrefixes(suggestedPrefixes);

String output = xml.xmlText(opts);		
System.out.println(output);



//*********INPUT *******
<?xml version="1.0" encoding="UTF-8"?>
<NCI_MSG xmlns="http://www.compnay.ca/hamilton/CompanyXMLMessage" >
	<NCI>
		<NCI_HEADER>
			<NETWRK_IDENT_CD>G1PIDHDR</NETWRK_IDENT_CD>
			<SOURCE_NAME>String</SOURCE_NAME>
			<TARGT_NAME>String</TARGT_NAME>
	
<SENT_EST>2004-10-06T06:00:00.00-01:00</SENT_EST>
			<MSG_TYPE>String</MSG_TYPE>
		</NCI_HEADER>
		<NCI_XACTION_NAME>String</NCI_XACTION_NAME>
		<NCI_XACTION_TEXT>String</NCI_XACTION_TEXT>
	</NCI>
</NCI_MSG>

//********DESIRED OUTPUT *******************
<?xml version="1.0" encoding="UTF-8"?>
<foo:NCI_MSG xmlns="http://www.compnay.ca/hamilton/CompanyXMLMessage" >
	<foo:NCI>
		<foo:NCI_HEADER>
	
<foo:NETWRK_IDENT_CD>G1PIDHDR</foo:NETWRK_IDENT_CD>
			<foo:SOURCE_NAME>String</foo:SOURCE_NAME>
			<foo:TARGT_NAME>String</foo:TARGT_NAME>
	
<foo:SENT_EST>2004-10-06T06:00:00.00-01:00</foo:SENT_EST>
			<foo:MSG_TYPE>String</foo:MSG_TYPE>
		</foo:NCI_HEADER>
		<foo:NCI_XACTION_NAME>String</foo:NCI_XACTION_NAME>
		<foo:NCI_XACTION_TEXT>String</foo:NCI_XACTION_TEXT>
	</foo:NCI>
</foo:NCI_MSG>

Re: Adding namespace prefixes not working properly

Posted by Rashmi Banthia <rj...@gmail.com>.
As your input xml (msg.xml) is not namespace prefixed,
setSaveSuggestedPrefixes() will not make any difference.

However you can:
(1) Have msg.xml namespace prefixed, so whatever changes you make to
the document - namespace prefixes will be preserved.

OR	

(2) create a new Instance 
NciMsgDocument doc = NciMsgDocument.Factory.newInstance();

Then add XmlOptions with setSaveSuggestedPrefixes().
Well, in this case you need actually recreate the whole document.

I am not sure if there is a better solution.

-Rashmi


On Tue, 26 Oct 2004 16:49:56 -0400, jeremy_overton@dofasco.ca
<je...@dofasco.ca> wrote:
>  
>  
>  
> 
> I am trying to create an xml document with a specified prefix.  I am
> attaching the code that i am using. I would expect taht once you define the
> XmlOptions setSaveSuggestedPrefixes that it would apply the prefixes when
> the xmlText method is called on the XmlObject.  What happens is that the xml
> is generated without the prefixes, actually it is identical to what was
> passed in. 
> 
> Kudos to wohever may help. I've been banging myhead against this for awhile.
> 
> Thanks 
> 
> Jeremy 
>  
> 
> // Code 
> XmlObject xml = XmlObject.Factory.parse(new File("C:\\msg.xml")); 
> 
> HashMap suggestedPrefixes = new HashMap(); 
> suggestedPrefixes.put("http://www.compnay.ca/hamilton/CompanyXMLMessage",
> "foo"); 
> XmlOptions opts = new XmlOptions(); 
> opts.setSaveSuggestedPrefixes(suggestedPrefixes); 
> 
> String output = xml.xmlText(opts);              
> System.out.println(output); 
>  
>  
> 
> //*********INPUT ******* 
> <?xml version="1.0" encoding="UTF-8"?> 
> <NCI_MSG xmlns="http://www.compnay.ca/hamilton/CompanyXMLMessage" > 
>         <NCI> 
>                 <NCI_HEADER> 
>                         <NETWRK_IDENT_CD>G1PIDHDR</NETWRK_IDENT_CD> 
>                         <SOURCE_NAME>String</SOURCE_NAME> 
>                         <TARGT_NAME>String</TARGT_NAME> 
>                         <SENT_EST>2004-10-06T06:00:00.00-01:00</SENT_EST> 
>                         <MSG_TYPE>String</MSG_TYPE> 
>                 </NCI_HEADER> 
>                 <NCI_XACTION_NAME>String</NCI_XACTION_NAME> 
>                 <NCI_XACTION_TEXT>String</NCI_XACTION_TEXT> 
>         </NCI> 
> </NCI_MSG> 
> 
> //********DESIRED OUTPUT ******************* 
> <?xml version="1.0" encoding="UTF-8"?> 
> <foo:NCI_MSG xmlns="http://www.compnay.ca/hamilton/CompanyXMLMessage" > 
>         <foo:NCI> 
>                 <foo:NCI_HEADER> 
>                         <foo:NETWRK_IDENT_CD>G1PIDHDR</foo:NETWRK_IDENT_CD> 
>                         <foo:SOURCE_NAME>String</foo:SOURCE_NAME> 
>                         <foo:TARGT_NAME>String</foo:TARGT_NAME> 
>                        
> <foo:SENT_EST>2004-10-06T06:00:00.00-01:00</foo:SENT_EST> 
>                         <foo:MSG_TYPE>String</foo:MSG_TYPE> 
>                 </foo:NCI_HEADER> 
>                 <foo:NCI_XACTION_NAME>String</foo:NCI_XACTION_NAME> 
>                 <foo:NCI_XACTION_TEXT>String</foo:NCI_XACTION_TEXT> 
>         </foo:NCI> 
> </foo:NCI_MSG>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: user-help@xmlbeans.apache.org