smart-fun / XmlToJson

Android Library for converting XML to JSON and JSON to XML

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

question: how to disable parse TAG attributes?

boomsya opened this issue · comments

commented

example XML

<?xml version="1.0" encoding="utf-8"?>
<library id="007">
    <book>James Bond</book>
    <book2>Book for the dummies</book2>
</library>

need to skip all tags attributes like this:

{
   "library":{
      "book": "James Bond",
      "book2":"Book for the dummies"
      }
}

Hello !!

there is no way at the moment to skip / ignore/ disable tags or attributes, but this is a very good idea!

I will add this to my TODO list !!

Probably to be consistent it will be something like "skipAttribute(String path)" or "skipTag(String path)". Does it sound great for you?

Arnaud.

commented

yes, thanks 👍

Hello,

I have updated the library and the documentation. Please integrate version 1.3.2
compile 'com.github.smart-fun:XmlToJson:1.3.2'

Here is the documentation about this new feature:

Skip a Tag or an Attribute

If you are not interrested in getting all the content of the XML, you can skip some Tags or some Attributes. Like for other methods you have to provide the path for the element to skip. You can use skipTag and skipAttribute as many times as you need.

<?xml version="1.0" encoding="utf-8"?>
<library>
    <owner>John Doe</owner>
    <book id="007">James Bond</book>
    <book id="000">Book for the dummies</book>
</library>
XmlToJson xmlToJson = new XmlToJson.Builder(xml)
    .skipTag("/library/owner")
    .skipAttribute("/library/book/id")
    .build();    
{  
   "library":{  
      "book":[  
         {  
            "content":"James Bond"
         },
         {  
            "content":"Book for the dummies"
         }
      ]
   }
}

enjoy!

Arnaud.

commented

wowww! thanks

commented

but this solution not good for my case:

<sda_data><profile_info><ROWSET xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
 <ROW>
  <REGNUMBER>...</REGNUMBER>
  <NAME>...</NAME>
  <SURNAME>...</SURNAME>
  <PATRONYMIC>...</PATRONYMIC>
  <NATIONALITY>...</NATIONALITY>
  <ISPENTION>...</ISPENTION>
  <SEXNAME>...</SEXNAME>
  <ISTAXPAYER>...</ISTAXPAYER>
  <BIRTHDATE>..</BIRTHDATE>
  <ANKETDATE xsi:nil = "true"/>
  <PHONE>...</PHONE>
  <FAX xsi:nil = "true"/>
  <EMAIL>...</EMAIL>
  <ISMIGRANT>...</ISMIGRANT>
 </ROW>
</ROWSET>
</profile_info>
<addresses_list><ROWSET xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
 <ROW>
  <TYPEID>1</TYPEID>
  <TYPENAME>...</TYPENAME>
  <KOATUU xsi:nil = "true"/>
  <COUNTRY>...</COUNTRY>
  <POSTCODE xsi:nil = "true"/>
  <REGION xsi:nil = "true"/>
  <DISTRICT xsi:nil = "true"/>
  <CITY>...</CITY>
  <STREET xsi:nil = "true"/>
  <BUILDING xsi:nil = "true"/>
  <BLOCK xsi:nil = "true"/>
  <APPARTMENT xsi:nil = "true"/>
  <NOTE xsi:nil = "true"/>
 </ROW>
 <ROW>
  <TYPEID>2</TYPEID>
  <TYPENAME>.../TYPENAME>
  <KOATUU xsi:nil = "true"/>
  <COUNTRY>...</COUNTRY>
  <POSTCODE>...</POSTCODE>
  <REGION xsi:nil = "true"/>
  <DISTRICT xsi:nil = "true"/>
  <CITY>...</CITY>
  <STREET>...</STREET>
  <BUILDING>...</BUILDING>
  <BLOCK xsi:nil = "true"/>
  <APPARTMENT>...</APPARTMENT>
  <NOTE xsi:nil = "true"/>
 </ROW>
 <ROW>
  <TYPEID>3</TYPEID>
  <TYPENAME>...</TYPENAME>
  <KOATUU xsi:nil = "true"/>
  <COUNTRY>...</COUNTRY>
  <POSTCODE>...</POSTCODE>
  <REGION xsi:nil = "true"/>
  <DISTRICT xsi:nil = "true"/>
  <CITY>...</CITY>
  <STREET>...</STREET>
  <BUILDING>...</BUILDING>
  <BLOCK xsi:nil = "true"/>
  <APPARTMENT>...</APPARTMENT>
  <NOTE xsi:nil = "true"/>
 </ROW>
</ROWSET>
</addresses_list>
<docs_list><ROWSET xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">
 <ROW>
  <TYPEID>1</TYPEID>
  <TYPENAME>...</TYPENAME>
  <NAME xsi:nil = "true"/>
  <DOCSERIA xsi:nil = "true"/>
  <DOCNUMBER>...</DOCNUMBER>
  <ISSUEDDATE xsi:nil = "true"/>
  <ISSUEDWHO xsi:nil = "true"/>
  <STARTDT xsi:nil = "true"/>
  <STOPDT xsi:nil = "true"/>
  <STATUSNAME>...</STATUSNAME>
  <NOTE xsi:nil = "true"/>
 </ROW>
 <ROW>
  <TYPEID>2</TYPEID>
  <TYPENAME>...</TYPENAME>
  <NAME xsi:nil = "true"/>
  <DOCSERIA>...</DOCSERIA>
  <DOCNUMBER>...</DOCNUMBER>
  <ISSUEDDATE>...</ISSUEDDATE>
  <ISSUEDWHO>...</ISSUEDWHO>
  <STARTDT xsi:nil = "true"/>
  <STOPDT xsi:nil = "true"/>
  <STATUSNAME>...</STATUSNAME>
  <NOTE xsi:nil = "true"/>
 </ROW>
</ROWSET>
...

i need delete all attributes like:
xmlns:xsi
xsi:nil = "true"

because code:
Gson gson = new Gson();
info = gson.fromJson(xmlToJson.toString(), INFO.class);

cant convert to gson :(
as you can see i have difficult format. It is not full file content :)
and i dont want add many rules like this:
.skipTag("/sda_data/statuses_info/ROWSET/xmlns")
.skipTag("/sda_data/statuses_info/ROWSET/ROW/NAME/xsi")
.skipTag("/sda_data/statuses_info/ROWSET/ROW/REGION/xsi")
.skipTag("/sda_data/statuses_info/ROWSET/ROW/ISSUEDWHO/xsi")
.skipTag("/sda_data/statuses_info/ROWSET/ROW/NOTE/xsi")
....

In this case you'd better remove xsi:nil = "true" from the XML String before using it, like doing:

xmlString = xmlString.replaceAll("xsi:nil = "true"", "");

commented

yes :) in do that now
i tried to find ideal solution :) because i dont know what attributes there will be generated by server in XML

anyway thanks

my pleasure. You helped to improve the library :)