//我需要创建以下XML。我搜索了很多,但无法理解如何创建这种类型的xml:
<CommandInfoDTO ClassName="DomainDTO">
<Domains>
<a:string>kochar.com</a:string>
<a:string>yahoo.com</a:string>
</Domains>
</CommandInfoDTO>
//我创建了具有单个节点的xml,因此我使用了JaxB和Marshaller
@XmlRootElement(name = "CommandInfoDTO")
@XmlAccessorType(XmlAccessType.FIELD)
public class EnforcePasswordDTO {
@XmlAttribute(name = "ClassName")
String className = "EnforcePasswordDTO";
@XmlElement(name = "AllowSpecialCharacters")
boolean AllowSpecialCharacters = false;
@XmlElement(name = "ChangeFrequency")
String changeFrequency = null;
}
JAXBContext jc = JAXBContext.newInstance(EnforcePasswordDTO.class);
Marshaller marshaller = jc.createMarshaller();
<CommandInfoDTO ClassName="EnforcePasswordDTO">
<AllowSpecialCharacters>true</AllowSpecialCharacters>
<ChangeFrequency>5</ChangeFrequency>
</CommandInfoDTO>
//但是我如何创建具有冒号的xml并且还有节点数。
答案 0 :(得分:2)
要在元素名称中编组(输出)冒号(:
),您只需在相应的JAXB注释的name
参数中指定它。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {
@XmlElement(name="notANamespacePrefix:bar")
String bar;
}
<强>输出强>
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<notANamespacePrefix:bar>Hello World</notANamespacePrefix:bar>
</foo>
无法胜任
如果您尝试在元素名称中解组包含冒号的XML,其中冒号前面的部分与命名空间前缀不对应,如下所示:
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum22088353/input.xml");
Foo uFoo = (Foo) unmarshaller.unmarshal(xml);
然后你会得到如下例外:
Exception in thread "main" javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException; systemId: file:/Users/bdoughan/GIT/EclipseLink-Trunk3/Scratch/src/forum22088353/input.xml; lineNumber: 3; columnNumber: 30; The prefix "notANamespacePrefix" for element "notANamespacePrefix:bar" is not bound.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:335)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:162)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:171)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:189)
at forum22088353.Demo.main(Demo.java:21)
将采取哪些措施
您可以将带有SAX的JAXB用于带冒号字符的属性进程元素名称。这是因为默认情况下,SAX解析器不支持名称空间。
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xmlReader = sp.getXMLReader();
Unmarshaller unmarshaller = jc.createUnmarshaller();
UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
xmlReader.setContentHandler(unmarshallerHandler);
xmlReader.parse(new InputSource("src/forum22088353/input.xml"));
Foo uFoo = (Foo) unmarshallerHandler.getResult();