62,621
社区成员
发帖
与我相关
我的任务
分享
public class AxiomDemo {
public static void writeToFile(OutputStream os, byte[] b) {//写内容写到文件里面
try {
os.write(b, 0, b.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void writeToFile(OutputStream os, OMElement om) {
try {
String str = om.toStringWithConsume();
byte[] b = str.getBytes();
//os.write(b, 0, b.length);
writeToFile(os, b);
} catch (XMLStreamException e) {
// TODO: handle exception
}
}
public static OMElement readQuestionXml(InputStream is) {
OMElement om = null;
try {
XMLStreamReader parser = XMLInputFactory.newInstance()
.createXMLStreamReader(is);
StAXOMBuilder builder = new StAXOMBuilder(parser);
// get the root element
om = builder.getDocumentElement();
} catch (XMLStreamException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return om;
}
public static void handlerQuestionXml(OutputStream os, OMElement om)
throws OMException, XMLStreamException {
Iterator iter = om.getChildElements();
long start = System.currentTimeMillis();
while (iter.hasNext()){
OMElement omquestion = (OMElement)iter.next();
QName q_content = new QName("qcontent");
QName q_answers = new QName("answers");
QName q_answer = new QName("answer");
QName q_acontent = new QName("acontent");
OMElement om_qContent = omquestion.getFirstChildWithName(q_content);
OMElement om_acontent = omquestion.getFirstChildWithName(q_answers).getFirstChildWithName(q_answer).getFirstChildWithName(q_acontent);
String content = om_qContent.getText();
//System.out.println(content);
String acontent = om_acontent.getText();
//&& acontent.length() <=20
if(content.length() <=15 && acontent.length() <=20){
//writeToFile(os, omquestion);
String question = omquestion.toStringWithConsume()+"\r\n";
writeToFile(os, question.getBytes());
} //System.out.println(omquestion.toString());
}
long end = System.currentTimeMillis();
System.out.println("title time:"+(end-start));
}
public static void main(String[] args) {
String filePath = "D:/QuanTa/AxiomDemo/xmlfile/1.xml";
String outFilePath = "D:/question2.xml";
try {
InputStream is = new FileInputStream(new File(filePath));
OutputStream os = new FileOutputStream(new File(outFilePath));
OMElement om = readQuestionXml(is);
System.out.println(om);
handlerQuestionXml(os, om);
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
System.out.println(e.getMessage());
} catch (XMLStreamException e) {
// TODO: handle exception
}
}
}