作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经试过了:
@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 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”)。预期的元素是 <{}Person:sampleData>`
仅供引用:我无法修改 XML。
感谢收到的任何帮助!
最佳答案
您可以执行以下操作:
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 中控制命名空间前缀的更多信息:
关于java - 如何为 XmlRootElement 添加后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718135/
我是一名优秀的程序员,十分优秀!