Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove Xml version string from String

Tags:

java

I have read Xml File using code given below-

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



        BufferedReader br = new BufferedReader(new FileReader(new  File(resourcePath+ "SubIndicatorTemplate.xml")));
        String line;
        StringBuilder sb = new StringBuilder();

        while((line=br.readLine())!= null){
            sb.append(line.trim());
        }

        XmlString=sb.toString();

Now i get XmlString sting in the format given below-

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Template><Caption Name="Book Details"/><Data Type="one"/><Titles><Title Name="Book no" Type="Numeric"/><Title Name="Book name" Type="Text"/></Titles></Template>

I want to remove <?xml version="1.0" encoding="UTF-8" standalone="no"?> from above string.So i have written code as

XmlString=XmlString.replaceAll("<?xml*?>", "").trim();

But still XmlString are same. So please help me to remove version information from XmlString.

like image 710
Viraj Dhamal Avatar asked Feb 20 '13 07:02

Viraj Dhamal


People also ask

How do I remove an XML tag?

Remove a Text Node xml is loaded into xmlDoc. Set the variable x to be the first title element node. Set the variable y to be the text node to remove. Remove the element node by using the removeChild() method from the parent node.

How to remove XML tag from XML file in Java?

In the code snippet below we start by loading an XML document and display its original contents. After that we find the element <row> , and remove the <address> element from it. To get the first child we are using the getChild() method from the root Element object. To remove an element we use the removeChild() method.

What does XML version mean?

Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing arbitrary data. It defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.


4 Answers

Change your regular expression to

XmlString=XmlString.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
like image 192
Achrome Avatar answered Oct 19 '22 20:10

Achrome


if(XmlString .contains("<?xml")){
  XmlString = XmlString.substring(XmlString.indexOf("?>")+2);
}
like image 29
Richie Avatar answered Oct 19 '22 19:10

Richie


Another way would we to do it like this:

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

edit: indexOf() instead of firstIndexOf()

like image 3
frederikdebacker Avatar answered Oct 19 '22 20:10

frederikdebacker


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....
}
like image 2
Cheung Avatar answered Oct 19 '22 18:10

Cheung