gpt4 book ai didi

java - 如何为 XmlRootElement 添加后缀

转载 作者:行者123 更新时间:2023-11-30 11:18:39 25 4
gpt4 key购买 nike

我已经试过了:

包信息.java

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

Java

@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”)。预期的元素是 <{}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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com