how to remove Xml version string from String

后端 未结 5 1987
情书的邮戳
情书的邮戳 2021-02-05 12:11

I have read Xml File using code given below-

  String XmlString = \"\";
  String resourcePath=FilePathHelper.getResourceFilePath(request);



        BufferedRea         


        
相关标签:
5条回答
  • 2021-02-05 12:59

    substring with first index of '>' + 1 ( +1 to get to next char), wont it give you end of xml version? Though it is not clean way of doing this.

    0 讨论(0)
  • 2021-02-05 13:03

    Change your regular expression to

    XmlString=XmlString.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
    
    0 讨论(0)
  • 2021-02-05 13:11
    String xmlString = readXml();
    int p1 = xmlString.indexOf("<?xml ");
    int p2 = xmlString.indexOf("?>");
    if ( p1 != -1 && p2 != -1 && p2>p1){
       xmlString=xmlString.substring(0,p1)+xmlString(p2+2);
    }
    
    private String readXml(){
       //TOTO from file or any-pipeline read,convert to String....
    }
    
    0 讨论(0)
  • 2021-02-05 13:12

    Another way would we to do it like this:

    XmlString = XmlString.substring(XmlString.indexOf("<Template>"))
    

    edit: indexOf() instead of firstIndexOf()

    0 讨论(0)
  • 2021-02-05 13:14
    if(XmlString .contains("<?xml")){
      XmlString = XmlString.substring(XmlString.indexOf("?>")+2);
    }
    
    0 讨论(0)
提交回复
热议问题