Jono
Jono

Reputation: 18118

defininging schemaLocation in java SimpleXML lib?

Is there a way to define the SchemaLocation using the java SimpleXML library?

i followed the API spec here by manually trying to add it as a namespace from http://simple.sourceforge.net/download/stream/doc/javadoc/

Using:

@NamespaceList({

        @Namespace(reference="http://hello/stock", prefix="stk"),
        @Namespace(reference="http://hello/basket", prefix="bsk"),
        @Namespace(reference="http://hello/location", prefix="loc"),
        @Namespace(reference="http://hello/common", prefix="cmn"),
        @Namespace(reference="schemaLocation:http://hello/stock stock-v1.xsd", prefix="xsi"),
})
public class Response{
//
}

but whenever i try parsing an XML file that will get converted into this POJO class, it says it cannot find the SchemaLocation?

Error below:

org.simpleframework.xml.core.AttributeException: Attribute 'schemaLocation' does not have a match in class com.hello.model.Response at line 1

the xml i am trying to parse is this:

<Stock 
 xmlns:stk="hello/stock" 
 xmlns:bsk="hello/basket"
 xmlns:loc="hello/location" 
 xmlns:cmn="hello/common" 
 xsi:schemaLocation="hello/stock stock-v1.xsd" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 uri="http://hello/stock/" 
 version="1" 
 id="" 
 brand="ford">

..xml data here

</Stock>

Thanks

Upvotes: 1

Views: 1475

Answers (2)

Maher Abuthraa
Maher Abuthraa

Reputation: 17833

As alex mentioned... move it to Attribute. here is a working sample:

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Namespace;
import org.simpleframework.xml.Root;

/**
 *
 * @author Maher
 */
@Root(name = "stock")
public class Stock {
    @Attribute(name = "schemaLocation")
    @Namespace(reference = "http://www.w3.org/2001/XMLSchema-instance", prefix = "xsi")
    private String mSchemaLocation;

    @Element(name = "child")
    private String mChild;

    public Stock() {
        setSchemaLocation("urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd");
        setChild("Hi from Apipas!");
    }

    private void setSchemaLocation(String schemaLocation) {
        this.mSchemaLocation = schemaLocation;
    }

    public void setChild(String child) {
        this.mChild = child;
    }
}

output:

<stock xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <child>Hi from Apipas!</child>
</stock>

Good luck,'.

Upvotes: 2

Alex
Alex

Reputation: 11

It is basically not a namespace anymore for SimpleXML and is treated as an Attribute. See the full answere here.

I hope this helps!

Upvotes: 0

Related Questions