Subscribe For Free Updates!

We'll not spam mate! We promise.

Feb 16, 2013

Remove all empty tags from XML in Java , C#

Views:

Remove All Empty Tags From  XML in Java , C#
To Day I will show you how to remove all empty tags from XML File or a string Variable that contains  XML data, in both languages C# and Java.

In My Previous tutorial,  I show how to Remove Empty Lines from  a string or from XML Data, After removing empty tags from XML there will be Empty Line in XML data besides these tags. So you must go through this tutorial as well.

In this tutorial Again I will Use Regex Expression to identify empty tags from XML string or File and  replace these empty lines with my respected tags or string  or left them empty.

Let's Start,
Suppose We have XML file or String variable having XML data.
<ssn>111111111</ssn> <employment> <current> <address> <line1> <line2> <line3> <city> <state> <country> </country></state></city></line3></line2></line1></address> <phone> <phonenumber> <countrycode> </countrycode></phonenumber></phone> </current> <previous> <address> <line1> <line2> <line3> <city> <state> <country> </country></state></city></line3></line2></line1></address> <phone> <phonenumber> <countrycode> </countrycode></phonenumber></phone> </previous> </employment> <maritalstatus>Single</maritalstatus>

Lets we have String Variable Name "XML" Contains above XML File data

String xml = //get the XML xml = xml.replaceAll("<.*?/>", "");

This will remove empty tags like
"<city/>" but not "<Address></Address>".

For this change above line With below Line
xml = xml.replaceAll("<.*?></.*?>", ""); OUT PUT WILL BE:- <SSN>111111111</SSN> <MaritalStatus>SINGLE</MaritalStatus>

Plese Feel Free to Socializer This Post
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

2 comments:

  1. Business Phone Service is ideal for small business and entrepreneurs. It gives a professional image and make sure that you don't miss a single call. Few months back, I decided to buy Hosted pbx from Telcan. You can get more information here: Business Phone Service

    ReplyDelete
  2. use following regex, above will not work.

    <\\w+/>

    ReplyDelete

Become a Fan

visual studio learn