我想用 XML Beans 创建一个特定的 xml 文档。所以我已经达到了这样一个可以接受的结果:
<Req accessId="1234" ver="1.1">
<Loc id="007" maxNr="5">
<Altitude x="123" y="987" type="ABC"/>
</Loc>
</Req>
现在需要 XML Documents 的典型 XML Header,但我没有找到使用 XMLBeans 的解决方案。这是我需要的结果:
<?xml version="1.0" encoding="UTF-8"?>
<Req accessId="1234" ver="1.1">
<Loc id="007" maxNr="5">
<Altitude x="123" y="987" type="ABC"/>
</Loc>
</Req>
在控制台上创建 XML 结果的 Java 代码:
XmlOptions options = new XmlOptions();
Map<String, String> substNameSpaces = new HashMap<String, String>();
substNameSpaces.put("ext", null);
options.setSavePrettyPrint();
options.setLoadSubstituteNamespaces(substNameSpaces);
options.setUseDefaultNamespace();
options.setSaveOuter();
// ReqDocument
ReqDocument doc = ReqDocument.Factory.newInstance(options);
// Req
Req req = doc.addNewReq();
req.setAccessId("1234");
req.setVer("1.1");
// Loc
Loc locValReq = req.addNewLoc();
loc.setId("007");
loc.setMaxNr(5);
// Altitude
Location location = loc.addNewAltitude();
location.setX(123);
location.setY(987);
location.setType("ABC");
// Outline
System.out.println(req.xmlText(options));
有谁能够帮我?