Giter Site home page Giter Site logo

mysimplexmlelement's Introduction

MySimpleXmlElement

2015-10-24 -> 2021-03-05

What is it?

Yet another implementation of php's SimpleXmlElement class.

MySimpleXmlElement is part of the universe framework.

Install

Using the planet installer via light-cli

lt install Ling.MySimpleXmlElement

Using the uni tool

uni import Ling/MySimpleXmlElement

Why ?

I didn't like the api for two main reasons:

  • struggling with CDATA
  • handling errors

MySimpleXmlElement is much more simpler (and probably way less powerful too) and fits my needs. We can simply flag a content as CDATA and it "works" as expected.

For errors handling, basically, there is no error, so there is no special handling.

How does it work?

The big picture: there are two objects:

  • the MySimpleXmlElement, which has 5 properties

    • name
    • value (null for self closing elements)
    • elements (recursion)
    • attributes
    • useCDATA (the flag that I missed with \SimpleXmlElement)
  • the MySimpleXmlBuilder, which renders a root MySimpleXmlElement. It handles the xml declaration for you.

Example

In this example, I'm emulating an imaginary rss feed manually with the MySimpleXmlElement class.

<?php

use Ling\MySimpleXmlElement\MySimpleXmlBuilder;
use Ling\MySimpleXmlElement\MySimpleXmlElement;



require_once "bigbang.php";


echo MySimpleXmlBuilder::create()->render(
    MySimpleXmlElement::create('rss')
        ->setAttributes(['version' => '2.0'])
        ->addElement(
            MySimpleXmlElement::create('channel')
                ->createChildReturn('title', 'Liftoff News', true) // true here means use CDATA
                ->createChildReturn('link', 'http://liftoff.msfc.nasa.gov/') 
                ->createChildReturn('description', 'Liftoff to Space Exploration.', true)
                ->createChildReturn('language', 'en-us')
                ->addElement(
                    MySimpleXmlElement::create('item')
                        ->createChildReturn('title', 'Star City', true)
                        ->createChildReturn('link', 'http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp')
                        ->createChildReturn('description', 'How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia\'s <a href="http://howe.iki.rssi.ru/GCTC/gctc_e.htm">Star City</a>.', true)
                        ->createChildReturn('guid', 'http://liftoff.msfc.nasa.gov/2003/06/03.html#item573')
                        ->createChildReturn('pubDate', 'Tue, 03 Jun 2003 09:39:21 GMT')
                )
                ->addElement(
                    MySimpleXmlElement::create('item')
                        ->createChildReturn('description', 'Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a <a href="http://science.nasa.gov/headlines/y2003/30may_solareclipse.htm">partial eclipse of the Sun</a> on Saturday, May 31st.', true)
                        ->createChildReturn('guid', 'http://liftoff.msfc.nasa.gov/2003/05/30.html#item572')
                        ->createChildReturn('pubDate', 'Fri, 30 May 2003 11:06:42 GMT')
                )
                
        )
);

Find more explanations on the bigbang autoloader here.

Geek Notes

addElement, createChild and createChildReturn

addElement and createChild do basically the same thing, but createChild does actually a lot more. Not only createChild adds an element to its parent, but it also can set the element name, value and CDATA flag in one line.

createChild and createChildReturn are two variations of the same method. createChild returns the child, while createChildReturn returns the parent.

Having those 3 methods at our fingertips gives us some appreciable flexibility when writing code.

self closing element (empty element) vs start tag -- end tag

To create a self closing element, make its value the empty string.

History Log

  • 1.1.4 -- 2021-05-31

    • Removing trailing plus in lpi-deps file (to work with Light_PlanetInstaller:2.0.0 api
  • 1.1.3 -- 2021-03-05

    • update README.md, add install alternative
  • 1.1.2 -- 2020-12-08

    • Fix lpi-deps not using natsort.
  • 1.1.1 -- 2020-12-04

    • Add lpi-deps.byml file
  • 1.1.0 -- 2015-10-25

    • An empty string value represents an empty element (self closing tag)
    • Fix self closing tag bug
  • 1.0.0 -- 2015-10-24

    • initial commit

mysimplexmlelement's People

Contributors

lingtalfi avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.