如何将后缀添加到XmlRootElement

时间:2014-05-18 02:57:38

标签: java jaxb

我试过这个:

package-info.java

@XmlSchema(xmlns= 
{
    @XmlNs(prefix="Person", namespaceURI="sample.url.something"),
    @XmlNs(prefix="xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance")
})

爪哇

@XmlRootElement(name = "Person:sampleData")
public class Person {

    private static String path = "files/test.xml";

    @XmlElement()
    public String Name;

    @XmlElement()
    public int Age;

    public Person(){}

    public Person(String name, int age){
        this.Name = name;
        this.Age = age;
    }

    public static String PersonToXMLString(Person person) throws JAXBException
    {
        JAXBContext jc = JAXBContext.newInstance(Person.class);
        StringWriter sw = new StringWriter();
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "somelocation.xsd");
        marshaller.marshal(person, sw);
        return sw.toString();
    }

    public static Person XMLStringToPerson() throws JAXBException
    {
        JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Person Person = (Person) unmarshaller.unmarshal(new File(path));
        return Person;
    }

    public static void WriteXMLStringFile(String xml) throws IOException
    {
        File file = new File(path);
        try (FileOutputStream fop = new FileOutputStream(file)) {
            if (!file.exists()) {
                file.createNewFile();
            }
            byte[] contentInBytes = xml.getBytes();
            fop.write(contentInBytes);
            fop.flush();
            fop.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String ReadXmlStringFromFile() throws IOException 
    {
        BufferedReader br = new BufferedReader(new FileReader(new File(path)));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line.trim());
        }
        return sb.toString();
    }

    public static void main(String[] args) throws JAXBException, IOException
    {       
        Person user = new Person("User",23);

        String xml = user.PersonToXMLString(user);
        System.out.println(xml);
        user.WriteXMLStringFile(xml);

        xml = user.ReadXmlStringFromFile();
        //used to unmarshall xml to Person object
        Person person = user.XMLStringToPerson();
        System.out.println(person.Name);
    }
}

XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Person:sampleData xmlns:Person="sample.url.something" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Name>User</Name>
    <Age>23</Age>
</Person:sampleData>

如果我这样做,我会在解组时遇到异常:

线程“main”中的异常javax.xml.bind.UnmarshalException:意外元素(uri:“sample.url.something”,local:“sampleData”)。 预期元素是&lt; {} Person:sampleData&gt;`

仅供参考:我无法修改XML。

感激不尽的任何帮助!

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

<强> package-info.java

您的@XmlSchema注释在package-info课程中应如下所示。由于elementFormDefault被指定为UNQUALIFIED,因此命名空间将仅应用于全局元素(在JAXB中对应于@XmlRootElement)。请注意,在编组XML时,不需要使用JAXB impl来使用@XmlSchema中包含的前缀。

@XmlSchema(
    elementFormDefault=XmlNsForm.UNQUALIFIED,
    namespace="sample.url.something",
    xmlns={
        @XmlNs(prefix="Person", namespaceURI="sample.url.something")
    }
)
package com.example;

import javax.xml.bind.annotation.*;

<强>人

@XmlRootElement注释不应包含前缀。

 package com.example;

 import javax.xml.bind.annotation.*;

 @XmlRootElement(name="sampleData")
 public class Person {

了解更多信息

您可以在我的博客上阅读有关控制JAXB中名称空间前缀的更多信息: