JAXB ナメてました。
このあいだ
という記事をかきましたが、DOM 操作なんか必要なかった。もっとシンプルにかける。
package kiririmode.test.jaxb.main; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBElement; import javax.xml.bind.Marshaller; import javax.xml.namespace.QName; import kiririmode.test.jaxb.schema.part.Part; import kiririmode.test.jaxb.schema.whole.ObjectFactory; import kiririmode.test.jaxb.schema.whole.Whole; public class JAXBTest { private static final String JAXB_PACKAGE_WHOLE = "kiririmode.test.jaxb.schema.whole"; private static final String JAXB_PACKAGE_PART = "kiririmode.test.jaxb.schema.part"; public static void main(String[] args) { try { JAXBContext jc = JAXBContext.newInstance(JAXB_PACKAGE_WHOLE + ":" + JAXB_PACKAGE_PART); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ObjectFactory of = new ObjectFactory(); Whole whole = of.createWhole(); whole.setAaa("value aaa"); whole.setBbb("value bbb"); kiririmode.test.jaxb.schema.part.ObjectFactory partOf = new kiririmode.test.jaxb.schema.part.ObjectFactory(); for ( int i = 0; i < 3; ++i ) { Part part = partOf.createPart(); part.setPartA("part A" + i); part.setPartB("part B" + i); JAXBElement<Part> elem = new JAXBElement<Part>(new QName("part" + i), Part.class, part); whole.getAny().add(elem); } marshaller.marshal( whole, System.out ); } catch (Exception e) { e.printStackTrace(); } } }