Showing posts with label plist. Show all posts
Showing posts with label plist. Show all posts

Monday, March 11, 2013

XMLPropertyListConfiguration / Plist Parser example

No comments:

XMLPropertyListConfiguration / Plist Parser example



XMLPropertyListConfiguration is having a fantastic API to work with plist files. But there is no much example for this library. After some googling and library analysis, it came to know that it is very very easy to parse the files !. Here is the example.


    @Test
    public void test() {
        try {
            String plistFile = "/Users/plistFile/info.plist";
            XMLPropertyListConfiguration plist = new XMLPropertyListConfiguration();

            HashMap life = new HashMap();
            life.put("Home", "home.png");
            life.put("Office", "browse.png");
            life.put("School", "mycart.png");
            life.put("College", "offers.png");
            plist.addProperty("Life", life);
           
            HashMap phones = new HashMap();
            phones.put("Nokia", "8000");
            phones.put("Samsung", "4000");

            List<String> imagesArray = new ArrayList<String>();
            imagesArray.add("icon1.png");
            imagesArray.add("icon2.png");
            imagesArray.add("icon3.png");
            phones.put("PhoneIcons", imagesArray);
            plist.addProperty("Phones", phones);
           
            List<String> imagesArray1 = new ArrayList<String>();
            imagesArray1.add("icon1.png");
            imagesArray1.add("icon2.png");
            imagesArray1.add("icon3.png");
                       
            Map dashBoard = new HashMap();
            dashBoard.put("imgArray", imagesArray1);

            plist.addProperty("ImageArrayBoard", dashBoard);
            plist.addProperty("ImageArray.innnerLevelTag", "IamInnerLevelTag");
            plist.save(plistFile);

            System.out.println("File saved .... ");
        } catch (Exception e) {
            System.out.println(e.getLocalizedMessage());
        }
    }

Thats All, Happy Coding :)

Ref : http://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration/plist/XMLPropertyListConfiguration.html
Read More

Thursday, November 29, 2012

Plist File Parsing Using Java

No comments:
Plist File Parsing Using Java

Below example illustrates , how to iterate over a plist file Nodes using apache's   XMLPropertyListConfiguration

  public void infoPlistConversion(String plistPath) throws Exception {
        XMLPropertyListConfiguration conf = new XMLPropertyListConfiguration(plistPath);
        System.out.println("root node children size => "+ conf.getRootNode().getChildrenCount());
        System.out.println("conf root => " + conf.getRootNode().getChild(0).getName());
        System.out.println("conf.getRoot().getChild(0).getValue() => " + conf.getRootNode().getChild(0).getValue());
        List children = conf.getRoot().getChildren();
        printChildrenValue(children);
    }
   
    private void printChildrenValue(List children) {
        if (children instanceof List) {
            List<PListNode> configs = (List<PListNode>) children;
            for (PListNode pListNode : configs) {
                // sub nodes
                if (pListNode.getValue() == null && pListNode.getChildrenCount() > 1) {
                    List pListNodeChildren = pListNode.getChildren();
                    System.out.println("-------------------------------------------------");
                    System.out.println("plist Name root => "+ pListNode.getName());
                    printChildrenValue(pListNodeChildren);
                // just print values
                } else {
                    // if it is ConfigParam and elements are not null
                    System.out.println("plist Name => "+ pListNode.getName());
                    System.out.println("plist Value => "+ pListNode.getValue());
                }
            }
        }
    }

--
Thank you.


Regards,
Kaleeswaran.S

Read More