You are almost there. You have one of two options:

1. Use XmlOptions.setLoadReplaceDocumentElement(new
javax.xml.namespace.QName("http://www.myexample.com";, "Root")) 

to replace only the root element with one that matches what you have in
your Schema

2. Change the Schema to add elementFormDefault="qualified" so that

<p:Root xmlns:p="http://www.myexample.com";>
    <p:Element>
        <p:SubElement />
    </p:Element>
</p:Root>

is valid and your original XmlOptions works.

Radu

> -----Original Message-----
> From: beboner [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, April 15, 2008 7:23 AM
> To: user@xmlbeans.apache.org
> Subject: XmlBeans and Namespace
> 
> 
> Hi,
> I try to load a Xml file without namespace with XMLBeans.
> I write my XSD and generate all the classes.
> 
> My Xsd model contains the following content <xs:schema
>         xmlns="http://www.myexample.com";
>         xmlns:xs="http://www.w3.org/2001/XMLSchema";
>         targetNamespace="http://www.myexample.com";>
> etc...
> 
> My XML contains the following content (i have no access to 
> this file) :
> <?xml version="1.0" encoding="ISO-8859-1" ?> <Root>
>     <Element>
>         <SubElement />
>     </Element>
> </Root>
> 
> My Java file contain the following content :
> File xmlFile = new File("file.xml");
> // Load the Xml
> RootDocument rootDocument = RootDocument.Factory.parse(xmlFile);
> 
> I got the following error because my Xml doesnt contains the 
> namespace The document is not a [EMAIL PROTECTED]://www.myexample.com: 
> document element namespace mismatch expected 
> "http://www.myexample.com"; got "" (?:?)
> 
> Ok no problem, i used the XmlOptions object to set up this information
> 
> XmlOptions options = new XmlOptions();
> // Set the namespace
> options.setLoadSubstituteNamespaces(Collections.singletonMap("",
> "http://www.myexample.com";));
> // Load the Xml
> RootDocument rootDocument = 
> RootDocument.Factory.parse(xmlFile, options);
> 
> Now the Factory dont crash but when i try to get a child 
> element of my root element i got a null pointer
> rootDocument.getElement() == null because the Xml document is 
> now like that
> 
> <p:Root xmlns:p="http://www.myexample.com";>
>     <p:Element>
>         <p:SubElement />
>     </p:Element>
> </p:Root>
> 
> I have the impression that XMLbeans do not recognize the 
> element with a namespace.
> 
> I tried to use the given option to remove the prefix but dont 
> really work.
> The prefix is removed on the rootDocument but still persist 
> on child element.
> options.setSaveSuggestedPrefixes(Collections.singletonMap("htt
> p://www.myexample.com",
> ""));
> 
> Hope you can help me.
> I want just to put the namespace to the root element with the 
> given prefix like that <p:Root xmlns:p="http://www.myexample.com";>
>     <Element>
>         <SubElement />
>     </Element>
> </p:Root>
> 
> Thank you
> --
> View this message in context: 
> http://www.nabble.com/XmlBeans-and-Namespace-tp16700839p16700839.html
> Sent from the Xml Beans - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

Notice:  This email message, together with any attachments, may contain 
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated 
entities,  that may be confidential,  proprietary,  copyrighted  and/or legally 
privileged, and is intended solely for the use of the individual or entity 
named in this message. If you are not the intended recipient, and have received 
this message in error, please immediately return this by email and then delete 
it.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to