-1

I have a String which is like this:

<Hello version="100" xsi:schemaLocation="http://www.hello.org/abcss-4_2 http://www.hello.org/v4-2/abcss-4-2.xsd">
 <DataHolder numberOfFields="67">
  <ClientField name="target" pptype="aligning" dataType="string">
   <Value value="panel_3646"/>
   <Value value="panel_3653"/>
  </ClientField>
  <ClientField name="category_count" pptype="symetrical" dataType="double"/>
  <ClientField name="pme.age" pptype="symetrical" dataType="double"/>
  <ClientField name="pme.gender" pptype="aligning" dataType="string">
   <Value value="F"/>
   <Value value="TRE"/>
  </ClientField>
 </DataHolder>
</Hello>

I need to remove this key=value:: xsi:schemaLocation="http://www.hello.org/abcss-4_2 http://www.hello.org/v4-2/abcss-4-2.xsd pair from my above String and after the removal it will be like this:

<Hello version="100">
 <DataHolder numberOfFields="67">
  <ClientField name="target" pptype="aligning" dataType="string">
   <Value value="panel_3646"/>
   <Value value="panel_3653"/>
  </ClientField>
  <ClientField name="category_count" pptype="symetrical" dataType="double"/>
  <ClientField name="pme.age" pptype="symetrical" dataType="double"/>
  <ClientField name="pme.gender" pptype="aligning" dataType="string">
   <Value value=""/>
   <Value value="F   "/>
   <Value value="NA"/>
  </ClientField>
 </DataHolder>
</Hello>

I tried this and it doesn't work:

String textToRemove = "xsi:schemaLocation=\"http://www.hello.org/abcss-4_2 http://www.hello.org/v4-2/abcss-4-2.xsd\"";
String data = readFromFile("path", "filename.xml");
data = data.replaceAll("(?i)\\b" + textToRemove + "\\b", "");
System.out.println(data);

I tried XML parser way like this which is appending XML tag in front of the file which I dont want:

Element rootElement = document.getDocumentElement();
rootElement.removeAttribute("xsi:schemaLocation");

Transformer transformer = TransformerFactory.newInstance().newTransformer();
Result output = new StreamResult(new File("output.txt"));
Source input = new DOMSource(document);

transformer.transform(input, output);

Update:-

This is the full stuff.

<Hello version="100" xmlns="http://www.hello.org/abcss-4_2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.hello.org/abcss-4_2 http://www.hello.org/v4-2/abcss-4-2.xsd">
john
  • 11,311
  • 40
  • 131
  • 251
  • You have a XML file, _why_ would you ever want to remove the `xsi:schemaLocation` part? – Tunaki Sep 17 '15 at 21:22
  • Bcoz we dont need that for now. We cannot access outside link from our production environment so we need to remove it. – john Sep 17 '15 at 21:23
  • This is not "a string" and what you're doing is peculiar. Try reasoning your actions to avoid the X-Y problem. Also, use an XML parser if at all possible. – Reut Sharabani Sep 17 '15 at 21:23
  • If I use an XML parser, it is appending XML tag just top of this file. I don't need that tag because I need to copy paste this string into another XML file at a particular location. – john Sep 17 '15 at 21:24
  • If there is any way I can avoid adding xml tag to output.txt file, I can go XML parsing route. – john Sep 17 '15 at 21:33

1 Answers1

1

Updated to match newer requirements with a [proof] (https://regex101.com/r/iP8sD3/1).

String textToRemove = "xsi:schemaLocation=\"http://www.hello.org/abcss-4_2 http://www.hello.org/v4-2/abcss-4-2.xsd\"";
String data = readFromFile("path", "filename.xml");
data = data.replaceAll(textToRemove, "");
System.out.println(data);
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51