????????????????????????????????????????????JAVA???????XML??
?????????????XML???y?????????????????????Ч????????????Google “??????XML” ???????????????????????????????????????????????????????????????????????
????????????????????????????Щ??
????Java???????????XML???????????????????DOM??SAX??Stax??Jaxb
????DOM ??Document Object Model ???????????????й??????ν???????С??XML??????????????????????????????????????????XML?????????????????????????????????е???棬????????????
????SAX??Simple API for XML Parsing ?????????????????????????????????????????????????????????н?????????????????API????Simple?????API?????????????д????????????????????API??DOM??????????????????XML?????????ú???OOME??????????дXML???????????Simple????????ɡ?
????Stax?? Streaming API for XML ???????????????????д?????????????????XML??????????????????????OOME?????????????????????????????д????????????????????????
????????Stax???????Σ?????ο????
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
xsr.nextTag(); // Advance to statements element
long i = 0;
String action = null;
while (xsr.hasNext()) {
if (xsr.next() == XMLStreamConstants.START_ELEMENT) {
if ("ContentItem".equals(xsr.getLocalName())) {
action = getAttributeValue(xsr?? "action");
} else if ("Data".equals(xsr.getLocalName())) {
i ++;
}
}
}
????JAXB??Java Architecture for XML Binding ???????????API???????XML???????????????????Java Bean????????????????????????????????????Java Bean????XML?????????????????????????JavaBean????????XML???????????????????????xjc ?????????????????JavaBean?????????Eclipse?????????????????????????????google????????????????????????????????????????????
JAXBContext jc = JAXBContext.newInstance("com.your.xml.datatype.bean"); // ????????JavaBean
// Create unmarshaller
Unmarshaller um = jc.createUnmarshaller();
// Unmarshal XML contents of the file myDoc.xml into your Java object
// instance
ObjectFactory objFac = new ObjectFactory(); // ????Bean??????????????????
objFac.createYourData(objFac.createYourDataType());
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("your.xml")); // ???????????XML??????
JAXBElement myJAXBObject = (JAXBElement) um.unmarshal(bis); // ???
YourDataType yourData = (YourDataType) myJAXBObject.getValue(); // ????????
// ??????дXML??????
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT?? Boolean.TRUE);
File outfile = new File("yourOutput.xml");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outfile)?? 4096);
m.marshal(myJAXBObject?? bos); // ???д?? myJAXBObject ???????????????????????
try {
bos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}