`

XML文件合法性解析

xml 
阅读更多
package huawei;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
<Document>
    <Head/>
    <Head>I am head.</Head1>
    <Body>I am body.<></Body>
    <Body1>I am body.<Body1>
    <Body2>I am body.
    <Body3 A="A" A="B">I am body.</Body3>
     <Nobody><Nobody/>
</Document
*/








/**
* 请注意不要修改包名、类名,否则将导致考试成绩失效
*/
public class JavaTest {

/**
* 该函数解析XML文件,并判断Xml结构是否有异常。
*
* @param filePath
*            Xml源文件
*
* @return 0 Xml文件结构正常 >0 Xml结构异常,返回值为结构异常的行号(第一行错误,返回值为1)
*/
public int parseXml(String filePath) {

// !!!由于阅卷时会单独执行本函数,因此确保函数能独立读文件,读文件操作不要放到函数外!!!
// 读文件示例如下,可以自行修改
BufferedReader br = null;

try {
br = new BufferedReader(new FileReader(filePath));

// TODO: Please add your code here!
List<Integer> every_rows = new ArrayList<Integer>();
int totalRowsNum = 0;
String line = br.readLine();
String string = "";
while (line != null) {
// process the line
every_rows.add(line.trim().length());
string += line.trim();
// read next line
line = br.readLine();
}
List<String> ary_lists = new ArrayList<String>();
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == '<') {
for (int j = i + 1; j < string.length(); j++) {
if (string.charAt(j) == '>') {
if (j == i + 1) {
for (int h = 0; h < every_rows.size(); h++) {
totalRowsNum += every_rows.get(h);
if (i < totalRowsNum) {
return h + 1;
}
}
}
ary_lists.add(string.substring(i, j + 1));
for (int z = j + 1; z < string.length(); z++) {
if (string.charAt(z) == '<') {
String str = string.substring(j + 1, z);
if (str.length() != 0) {
Integer str1 = str.length();
ary_lists.add(str1.toString());
}
break;
}
}
break;
} else if (j == string.length() - 1
&& string.charAt(j) != '>') {
for (int h = 0; h < every_rows.size(); h++) {
totalRowsNum += every_rows.get(h);
if (j + 1 <= totalRowsNum) {
return h + 1;
}
}
}
}
}
}

List<String> pro_lists = new ArrayList<String>();
int space_GPS = 0;
for (int i = 0; i < ary_lists.size(); i++) {
for (int j = 0; j < ary_lists.get(i).length(); j++) {
if (ary_lists.get(i).charAt(j) == ' ') {
space_GPS = j;
pro_lists.add(ary_lists.get(i).substring(space_GPS,
ary_lists.get(i).length() - 1));
break;
}
}
}

for (int i = 0; i < pro_lists.size(); i++) {
String[] str_pronum = {};
String string2 = pro_lists.get(i);
if (string2.contains(",")) {
String stringarray1[] = string2.split("=");
str_pronum = Arrays.copyOf(str_pronum,
str_pronum.length + 1);
str_pronum[str_pronum.length - 1] = stringarray1[0];
for (int j = 1; j < stringarray1.length - 1; j++) {
String stringarray2[] = stringarray1[j].split(",");
for (int z = 0; z < stringarray2.length; z++) {
if ((z + 1) % 2 == 0) {
str_pronum = Arrays.copyOf(str_pronum,
str_pronum.length + 1);
str_pronum[str_pronum.length - 1] = stringarray2[z];
}
}
}
} else {
String stringarray1[] = string2.split("=");

str_pronum = Arrays.copyOf(str_pronum,
str_pronum.length + 1);
str_pronum[str_pronum.length - 1] = stringarray1[0];
for (int j = 1; j < stringarray1.length - 1; j++) {
String stringarray2[] = stringarray1[j].split(" ");
for (int z = 0; z < stringarray2.length; z++) {
if ((z + 1) % 2 == 0) {
str_pronum = Arrays.copyOf(str_pronum,
str_pronum.length + 1);
str_pronum[str_pronum.length - 1] = stringarray2[z];
}
}
}
}

int errornum = 0;
for (int j = 0; j < str_pronum.length; j++) {
if (str_pronum.length != 1) {
for (int z = 0; z < str_pronum.length - 1 - z; z++) {
if (str_pronum[z].trim().equals(
str_pronum[z + 1].trim())) {
for (int n = 0; n < ary_lists.size(); n++) {
if (ary_lists.get(n).contains(
pro_lists.get(i))) {
for (int m = 0; m < n; m++) {
errornum += ary_lists.get(m)
.length();
}
for (int h = 0; h < every_rows.size(); h++) {
totalRowsNum += every_rows.get(h);
if (errornum < totalRowsNum) {
return h + 2;
}
}
}
}
}
}
}
}
}

List<String> ary_lists1 = new ArrayList<String>();
int count;
for (int i = 0; i < ary_lists.size(); i++) {
count = 0;
for (int j = 0; j < ary_lists.get(i).length(); j++) {
if (ary_lists.get(i).charAt(j) == ' ') {
ary_lists1.add(ary_lists.get(i).substring(0, j) + ">");
count = 1;
break;
}
}
if (count == 0) {
ary_lists1.add(ary_lists.get(i));
}
}

int total_num = 0;
boolean[] panduan = new boolean[ary_lists1.size()];
for (int i = 0; i < ary_lists1.size(); i++) {
if (panduan[i] == false) {
if (ary_lists1.get(i).contains("<")) {
for (int j = i + 1; j < ary_lists1.size(); j++) {
if (!ary_lists1.get(j).contains("<")) {
panduan[j] = true;
}
if (panduan[j] == false) {
if (ary_lists1
.get(j)
.substring(1,
ary_lists1.get(j).length() - 1)
.contains("/")) {
if (("/" + ary_lists1.get(i).subSequence(1,
ary_lists1.get(i).length() - 1))
.equals(ary_lists1
.get(j)
.substring(
1,
ary_lists1.get(j)
.length() - 1))) {
panduan[i] = true;
panduan[j] = true;
break;
}
}
}
}
} else {
return 1;
}
}
}

for (int i = 0; i < ary_lists1.size(); i++) {
if (panduan[i] == true) {
if (ary_lists.get(i).contains("<")) {
total_num += ary_lists.get(i).length();
} else {
total_num += Integer.parseInt(ary_lists.get(i));
}
} else {
for (int h = 0; h < every_rows.size(); h++) {
totalRowsNum += every_rows.get(h);
if (total_num + 1 < totalRowsNum) {
return h + 1;
}
}
}
}
} catch (Exception e) {
System.out.println("open file execption : " + e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
System.out.println("close file execption : " + e);
}
}
}

return 0;
}

public static void main(String[] args) {
JavaTest c = new JavaTest();
int result = c.parseXml("g.xml");
if (result == 0) {
System.out.println("Xml file OK.");
} else {
System.out.println("Xml file error at line: " + result);
}
}

}
分享到:
评论
Global site tag (gtag.js) - Google Analytics