Showing posts with label parser. Show all posts
Showing posts with label parser. 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, August 16, 2012

Jdom CRUD Operations

No comments:
Hi,

Jdom CRUD Operations

Here is sample code to do some operations like
   
//Adding elements
    public void addElementAtXpath() {
        try {
            XPath xpath = XPath.newInstance("publishers");
            xpath.addNamespace(root_.getNamespace());
            Element publisherNode = (Element) xpath.selectSingleNode(root_);
            org.jdom.Element element = new Element("kalees");
            element.addContent(createElement(CI_FILE_RELEASE_OVERRIDE_AUTH_NODE, TRUE));
            publisherNode.addContent(element);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
// Changing attributes
    public void changeAttributeValue() {
        try {
            XPath xpath = XPath.newInstance("scm");
            xpath.addNamespace(root_.getNamespace());
            Element scmNode = (Element) xpath.selectSingleNode(root_);
            scmNode.setAttribute("class", "Kalees");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
//Deleting Elements
    public void deleteElementAtXpath() {
        //deleting a node...
        // deleting content of a node
        //triggerNode.removeContent();
        try {
            String childName = "hudson.plugins.emailext.ExtendedEmailPublisher";
            XPath xpath = XPath.newInstance("publishers");
            xpath.addNamespace(root_.getNamespace());
            Element triggerNode = (Element) xpath.selectSingleNode(root_); // if it is null then element is not present
            triggerNode.removeChild(childName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Element createElement(String nodeName, String NodeValue) {
        org.jdom.Element element = new Element(nodeName);
        if (NodeValue != null) {
            element.addContent(NodeValue);
        }
        return element;
    }
--
Thank you.


Regards,
Kaleeswaran.S

Read More

Thursday, August 2, 2012

JDOM - How to insert an element, after another

No comments:

JDOM - How to insert an element, after another

    private Document document_ = null;
    private Element root_ = null;

    @Test
    public void generateXml() {
        //Url path of the file
        String filePath = "http://172.................. .xml";
        testXmlGeneration(filePath);
    }
   
    private void testXmlGeneration(String url) {
        try {
            SAXBuilder builder = new SAXBuilder();
            // giving xml file url to jdom for parsing
            document_ = builder.build(url);
            root_ = document_.getRootElement();

            // creating necessary elements which is to be appended at a particular point of the location in xml
            org.jdom.Element element = new Element("release");
            element.addContent(createElement("override", "true"));
            element.addContent(createElement("url", "http://outside.out.coml"));
            element.addContent(createElement("username", "kaleeswaran"));
            element.addContent(createElement("password", "xm0QczqM6k9x10Os1z0k="));
            element.addContent(createElement("rpackage", "PackageRed"));
            element.addContent(createElement("release", "release"));
            element.addContent(createElement("file__patterns", null).addContent(createElement("hudson.plugins.collabnet.documentuploader.FilePattern", "*.zip")));
           
            // i want to add the generated elements after the publish tag in the xml
            XPath xpath = XPath.newInstance("publish");
            xpath.addNamespace(root_.getNamespace());
            Element pullisherNode = (Element) xpath.selectSingleNode(root_);
            // here specify the index value where the element should be added and pass the element
            pullisherNode.getContent().add(3, element);

           
           
            File dest = new File("/Users/kaleeswaran/Desktop/gonfig.xml");
            InputStream configAsStream = getConfigAsStream();
            ciManager.streamToFile(dest, configAsStream) ;
            System.out.println("new value added =======> " + url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   
    public static Element createElement(String nodeName, String NodeValue) {
        org.jdom.Element element = new Element(nodeName);
        if (NodeValue != null) {
            element.addContent(NodeValue);
        }
        return element;
    }
   
    public InputStream getConfigAsStream() throws IOException {
        XMLOutputter xmlOutput = new XMLOutputter();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        xmlOutput.output(document_, outputStream);
       
        return new ByteArrayInputStream(outputStream.toByteArray());
    }
--
Thank you.


Regards,
Kaleeswaran.S

Read More