有一个我无法控制的XML生产软件,它违反了约定的模式,所生成的XML标签仅具有首字母大写(Tagname
)而不是驼峰式({{ 1}})。
我有一个架构,我需要对这些文件进行预处理,然后将标签规范化为相应位置中的预期标签,然后再将其提供给解析器。
以下是打印示例模式并模拟不良情况的程序:
TagName
输出:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "tESTeLEM")
public static class TestElem {
public String tEstElem;
}
public static void main(final String[] args) throws Exception {
final JAXBContext jc = JAXBContext.newInstance(TestElem.class);
final Schema schema = genSchema(jc);
System.out.println();
final TestElem testElem = new TestElem();
testElem.tEstElem = "value";
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Marshaller ma = jc.createMarshaller();
ma.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
ma.marshal(testElem, new TeeOutputStream(out, new CloseShieldOutputStream(System.out)));
System.out.println();
final String xml = new String(out.toByteArray(), "UTF-8").toLowerCase();
System.out.println(xml);
final Unmarshaller unm = jc.createUnmarshaller();
unm.unmarshal(new StringReader(xml));
}
private static Schema genSchema(final JAXBContext jc) throws Exception {
final List<ByteArrayOutputStream> outs = new ArrayList<ByteArrayOutputStream>();
jc.generateSchema(new SchemaOutputResolver() {
@Override
public Result createOutput(final String namespaceUri, final String suggestedFileName)
throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final TeeOutputStream out2 =
new TeeOutputStream(out, new CloseShieldOutputStream(System.out));
outs.add(out);
final StreamResult streamResult = new StreamResult(out2);
streamResult.setSystemId("");
return streamResult;
}
});
final StreamSource[] sources = new StreamSource[outs.size()];
for (int i = 0; i < outs.size(); i++) {
final ByteArrayOutputStream out = outs.get(i);
sources[i] = new StreamSource(new ByteArrayInputStream(out.toByteArray()), "");
}
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
return sf.newSchema(sources);
}