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.
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.
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.
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.
Change your regular expression to
XmlString=XmlString.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim();
if(XmlString .contains("<?xml")){
XmlString = XmlString.substring(XmlString.indexOf("?>")+2);
}
Another way would we to do it like this:
XmlString = XmlString.substring(XmlString.indexOf("<Template>"))
edit: indexOf() instead of firstIndexOf()
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....
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With