当前位置:编程学习 > JAVA >>

庞果的测试用例感觉有问题呢.自己测的好好的,到他那就失败~

http://hero.pongo.cn/OnlineCompiler/Index?ID=75&ExamID=73


执行测试用例失败!解析这组数据出错了:<?xml version="1.0" ?><Books><Book><Name = "The C++ Programming Language" Author="Bjarne Stroustrup"></Book><Book><Name = "Effective C++" Author = "Scott Meyers"></Book></Books>
用字符串 或者读XML文件 都没问题 




public static void main(String[] args) {
// String in = "<?xml version=\"1.0\" ?>" +
// "<Books><Book><Name = \"The C++ Programming Language\" Author=\"Bjarne Stroustrup\">" +
// "</Book><Book><Name = \"Effective C++\" Author = \"Scott Meyers\"></Book></Books>";
// System.out.println(in);
System.out.println(ParsingXML(Fisread("E://test.xml")));
}
public static String ParsingXML(String in){
StringBuffer sb = new StringBuffer();
String[] xmlStrs = in.split("</");
for (int i = 0; i < xmlStrs.length; i++) {
xmlStrs[i] = xmlStrs[i].replace("<", "").replace(">", "").replace("/>", "");
}
sb.append(xmlStrs[xmlStrs.length-1]+"\n");
for (int i = 0; i < xmlStrs.length-2; i++) {
sb.append(" "+xmlStrs[xmlStrs.length-2]+" "+(i+1)+"\n");
if(i==0){
xmlStrs[i] = xmlStrs[i].replace(xmlStrs[i].substring(xmlStrs[i].indexOf("?"),xmlStrs[i].lastIndexOf("?")+1), "").replace(xmlStrs[xmlStrs.length-1], "").replace(xmlStrs[xmlStrs.length-2], "");
}else{
xmlStrs[i] = xmlStrs[i].replace(xmlStrs[xmlStrs.length-i], "").replace(xmlStrs[xmlStrs.length-2], "");
}
String[] pars = xmlStrs[i].split("\"");
for (int j = 0; j < pars.length-1;) {
sb.append("\t"+pars[j].trim().replace("=", ":").replace(" ", "")+pars[j+1]+"\n");
j+=2;
}
}
return sb.toString();
}
private static String Fisread(String path){
File f = new File(path);
if(!f.exists()){
return null;
}
 try {
 FileInputStream fis = new FileInputStream(f);
 byte[] b = new byte[(int)f.length()];
 fis.read(b);
fis.close();
return new String(b);
   } catch (IOException e) {
}
return null;
}
--------------------编程问答-------------------- 做过好几道题了,每次都不通过~
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,