Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中从xml中删除单词/标记_Java_Arrays_String_Replace_Split - Fatal编程技术网

在java中从xml中删除单词/标记

在java中从xml中删除单词/标记,java,arrays,string,replace,split,Java,Arrays,String,Replace,Split,当我运行xml文件并在表单字段中输入新数据时,我试图从xml文件中删除/删除最后一个标记-但它似乎仍然保留-并且没有被删除/删除 XML数据集: asdfasdf asdfasdf asdfasdf -- -- ---- -- -- -- asdfasd fasdfasdf asdfdasf -- -- ---- -- -- -- 代码: String fileName=“src/smer.xml”; 字符串测试字=“家里有人能帮我吗?似乎您的XML数据集在以下行无效:。你验证了你的XM

当我运行xml文件并在表单字段中输入新数据时,我试图从xml文件中删除/删除最后一个标记-但它似乎仍然保留-并且没有被删除/删除

XML数据集:

asdfasdf
asdfasdf
asdfasdf
-- -- ---- 
--
--
--
asdfasd
fasdfasdf
asdfdasf
-- -- ---- 
--
--
--
代码:
String fileName=“src/smer.xml”;

字符串测试字=“家里有人能帮我吗?似乎您的XML数据集在以下行无效:
。你验证了你的XML吗?是的,它可以工作,但即使没有它,在我开始插入更多标记之前,我也不会使用XML删除最后一个标记,直到我在最后插入另一个标记。阿加尼修复了代码…现在怎么办?如果你是说你的问题解决了,将解决方案作为答案发布,以便将来的读者受益。家里有人能帮我吗?您的XML数据集在以下行中似乎无效:
。你验证了你的XML吗?是的,它可以工作,但即使没有它,在我开始插入更多标记之前,我也不会使用XML删除最后一个标记,直到我在最后插入另一个标记。阿加尼修复了代码…现在怎么办?如果你的意思是你的问题已经解决,请将解决方案作为答案发布,以便未来的读者受益。
<?xml version="1.0" encoding="UTF-8"?>
<models>
<details>
<firstname firstname_id='1'>asdfasdf</firstname>
<middlenames middlenames_id='1'>asdfasdf</middlenames>
<lastname lastname_id='1'>asdfasdf</lastname>
<date date_id='1'>-- -- ---- </date>
<jsexuals jsexuals_id='1'>--</jsexuals>
<height height_id='1'>--</height>
<weight weight_id='1'>--</weight>
<ethnicity ethnicity_id='1'></nationality>
<nationality nationality_id='1'></nationality>
<height height_id='1'></height>
<weight weight_id='1'></weight>
<hair hair_id='1'></hair>
<eyes eyes_id='1'></eyes>
<measurements measurements_id='1'></measurements>
<servicesoffered servicesoffered_id='1'></servicesoffered>
<address address_id='1'></address>
<city city_id='1'></city>
<postalcode postalcode_id='1'></postalcode>
<landlinephone landlinephone_id='1'></landlinephone>
<mobilephone mobilephone_id='1'></mobilephone>
<businessphone businessphone_id='1'></businessphone>
</details>
</models>

<details>
<firstname firstname_id='2'>asdfasd</firstname>
<middlenames middlenames_id='2'>fasdfasdf</middlenames>
<lastname lastname_id='2'>asdfdasf</lastname>
<date date_id='2'>-- -- ---- </date>
<jsexuals jsexuals_id='2'>--</jsexuals>
<height height_id='2'>--</height>
<weight weight_id='2'>--</weight>
<ethnicity ethnicity_id='2'></nationality>
<nationality nationality_id='2'></nationality>
<height height_id='2'></height>
<weight weight_id='2'></weight>
<hair hair_id='2'></hair>
<eyes eyes_id='2'></eyes>
<measurements measurements_id='2'></measurements>
<servicesoffered servicesoffered_id='2'></servicesoffered>
<address address_id='2'></address>
<city city_id='2'></city>
<postalcode postalcode_id='2'></postalcode>
<landlinephone landlinephone_id='2'></landlinephone>
<mobilephone mobilephone_id='2'></mobilephone>
<businessphone businessphone_id='2'></businessphone>
</details>
String fileName = "src/sperm.xml";
String testWord = "</details"; //CHANGE THIS IF YOU WANT
String testWord1 = "</models>";
int wordCntr = 0;
int tLen = testWord.length();
boolean check;
boolean check1;
String l="";
String m  ="";
String l1="";
String root="</models>";

try {
    DOMOutputter output = new DOMOutputter();

    FileInputStream fstream = new FileInputStream(fileName);
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    BufferedReader br1 = new BufferedReader(new InputStreamReader(fstream));
    String strLine;
    String strLine1;
    String temp="";
    while((strLine = br.readLine()) != null){
        check = strLine.toLowerCase().contains(testWord.toLowerCase());
        // strLine = strLine.replace("<details>", " ");
        if(check){
            String[] lineWords = strLine.split("\\s+");
            for(String w : lineWords){
                if(w.length() >= tLen){
                    String word = w.substring(0,tLen).trim();
                    if(word.equalsIgnoreCase(testWord)){
                        wordCntr++;
                    }
                }
            }
        }
    }
    while((strLine1 = br1.readLine()) != null){
        //check to see whether testWord occurs at least once in the line of text
        //  check1 = strLine1.toLowerCase().contains(testWord);
        strLine1 = strLine1.replace(testWord1, " ");
        check1 = strLine1.toLowerCase().contains(testWord1);
        if(check1){
            String[] lineWords1 = strLine1.split("\\s+");
            for(String v : lineWords1){
                if(v.equalsIgnoreCase(testWord1)){
                    strLine1 = strLine1.replace(testWord1, " "); // I replace</models> in this instant with empty strings
                }
            }
        }
    }