0

I have an XML file like below .

<animals> <dog> <name>Rufus</name> <breed>labrador</breed> </dog> <dog> <name>Marty</name> <breed>whippet</breed> </dog> <cat name="Matilda"/> </animals>

And i need the output to be a filtered JSON . I got the XML converted to JSON with Jackson Library.

 {    
        animals:{ 
             dog:[{name:'Rufus',breed:'Labrador'},
                 {name:'Marty',breed:'whippet'}],
             cat:{name:'Matilda'}
        }
    }

But i need a formatted output , with selected nodes. Is there some jar which takes filters . say if give an exclude filter as

animals.dog=true

I get an output like the below

{    
        animals:{ 
                     cat:{name:'Matilda'}
                }
}
  • Read the XML into a model and then use gson to filter the field using an exclusion strategy - http://stackoverflow.com/questions/4802887/gson-how-to-exclude-specific-fields-from-serialization-without-annotations – Deepak Bala Feb 16 '15 at 13:01

1 Answers1

0

I know this isn't the exact scenario since I am just using a class here and not an XML, but it may give you what you need as far as filtering a node out:

public class Answer28534834 {

public static class Animal
{
    private ArrayList<Dog> dogs;
    private ArrayList<Cat> cats;

    public Animal()
    {
        dogs = new ArrayList<Dog>();
        cats = new ArrayList<Cat>();
    }

    public void addDog(Dog dog)
    {
        dogs.add(dog);
    }

    public ArrayList<Dog> getDogs()
    {
        return dogs;
    }

    public void addCat(Cat cat)
    {
        cats.add(cat);
    }

    public ArrayList<Cat> getCats()
    {
        return cats;
    }
}

public static class Dog
{
    String name;
    String breed;

    public Dog(String name, String breed)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public String getBreed()
    {
        return breed;
    }       
}

public static class Cat
{
    String name;

    public Cat(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}

public static class Example {
    private String fieldA;
    private String fieldB;
    private String fieldC;

    public Example(String a, String b, String c) 
    {
        this.fieldA = a;
        this.fieldB = b;
        this.fieldC = c;
    }

    public String getFieldA() {
        return fieldA;
    }

    public String getFieldB() {
        return fieldB;
    }

    public String getFieldC() {
        return fieldC;
    }
}

public static void main(String [] args) throws Exception
{
    ObjectMapper mapper = new ObjectMapper();

    Animal animalList = new Animal();
    animalList.addDog(new Dog("Rufus", "Labrador"));
    animalList.addDog(new Dog("Marty", "Whippet"));
    animalList.addCat(new Cat("Matilda"));

    JsonNode node = mapper.convertValue(animalList, JsonNode.class);

    if (node.isObject()) 
    {
        for (Iterator<Entry<String, JsonNode>> it = node.fields(); it.hasNext(); ) 
        {
            Map.Entry<String, JsonNode> field = it.next();
            String key = field.getKey();

            if (!"cats".equals(key)) 
            { 
                it.remove();
            }
        }
    }

    System.out.println(mapper.writeValueAsString(node));
}       

}
Michael
  • 519
  • 4
  • 14
  • It just gives us one level of iteration and we have to go to each and every node and check and filter it out ? My XML is gonna be really big with more inner nodes , wouldnt it take a performance hit checking like this . I'll have like 100K xml messages incoming in an hour and I've to filter it out in a dynamic way . Is it possible ? – John Thomas Feb 16 '15 at 09:44
  • Are you converting your XML to objects prior to serialization? There are a few different approaches I am seeing that could be used, including implementing JsonSerializable and using Filters. – Michael Feb 16 '15 at 17:56
  • I receive different types of XML , and none of them have a confirmed structure . So it would be very difficult for me to convert to XML to Object as i'll have to create pojo's for each and every parent and child. I guess filtering out JSON might be more efficient that filtering XML ? – John Thomas Feb 17 '15 at 03:23
  • Can you post your code you are using? It isn't a path I am familiar with, but I am willing to take a look – Michael Feb 17 '15 at 05:12