简体   繁体   中英

How can this XML file be read by JAXB?

I can already read and write XML using JAXB in my unittest but when I try to process an actual file

I get this error: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"resources"). Expected elements are <{}item>,<{}plurals>,<{tools:http://schemas.android.com/tools}resources>,<{}string> javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"resources"). Expected elements are <{}item>,<{}plurals>,<{tools:http://schemas.android.com/tools}resources>,<{}string>

The file looks like:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="de">

    <string name="name1" translatable="false">value 1</string>
    <string name="name2" translatable="false">value 2</string>
</resources>

What the unittest is able to write (and also read is):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:resources xmlns:ns2="tools:http://schemas.android.com/tools">
    <string name="name1" translatable="false">value1</string>
    <string name="name2" translatable="true">value2</string>
    <plurals>
        <item quantity="one">%d one</item>
        <item quantity="other">%d more</item>
    </plurals>
</ns2:resources>

It seems that xmlns:tools could cause the issue that's different to ns2:resources which is created for some reason.

The top level container element is annotated as:

@XmlRootElement(name="resources", namespace = "tools:http://schemas.android.com/tools")

The XmlRootElement doesn't have further options to set how could I replace the namespace "ns2" by "tools"?

You @XmlRootElement annotation is telling JAXB that the root element corresponding to your class is composed of the name resources and the namespace http://schemas.android.com/tools below is the corrected annodation.

@XmlRootElement(name="resources", namespace = "http://schemas.android.com/tools")

Your XML document needs to ensure that the prefix associated with the http://schemas.android.com/tools namespace is used to prefix the resources element.

<?xml version="1.0" encoding="utf-8"?>
<tools:resources xmlns:tools="http://schemas.android.com/tools" tools:locale="de">

    <string name="name1" translatable="false">value 1</string>
    <string name="name2" translatable="false">value 2</string>
</tools:resources>

or

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:resources xmlns:ns2="http://schemas.android.com/tools">
    <string name="name1" translatable="false">value1</string>
    <string name="name2" translatable="true">value2</string>
    <plurals>
        <item quantity="one">%d one</item>
        <item quantity="other">%d more</item>
    </plurals>
</ns2:resources>

UPDATE #1

Thanks for your response, the test doc is created programatically with ns2 as namespace can be read from the code, the problem is that I can't parse this format.

With the corrected @XmlRootElement information it doesn't matter what the prefix used is.

UPDATE #2

If the root element in your XML document is not namespace qualified then then @XmlRootElement annotation should not contain namespace information.

@XmlRootElement(name="resources")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM