XML

Use Notepad++ to view and edit XML documents

Notepad++ is a free source code editor and Notepad replacement that supports several formats. In its basic edition it can already do some nice things like:

  • collapse structured texts like XML on a certain hierarchy level with the key combination "Alt"+X, where X is a number between 1 and 8. Uncollapse with "Shift"+"Alt"+X.
  • Extended search and replace with e.g. line breaks or search and replace using regular expressions.
  • Vertical selection and simultaneous editing of text holding down the "Alt" key.
  • and many more...
  • If you download the XML Tools Plugin you can also pretty print XML documents without witespaces. Just select it from the plugins menu or use the keyboard shortcut "Ctrl"+"Alt"+"Shift"+"B". Check out also the other features provided with the plugin.

    Taggings:

    Wiener Linien API

    The challenge was to get a public transport route, by sending time, date, start and destination address. The problem was solved by using the "Wiener Linien API" (http://akirk.github.com/Wiener-Linien-API/). The PHP-Code sends a XML-request with all details of the appointment and get a XML-respond with the transport connection.

    For building a request the PHP-function "buildRequest()" can be used.
    The following example illustrates a query from "Seckendorfstrasse 4" to "Wiedner Hauptstr. 8":

    $params = array();
    $params["outputCoords"] = "WGS84";
    $params["from"] = "Seckendorfstrasse 4";
    $params["to"] = "Wiedner Hauptstr. 8";
    $params["year"] = "2011";
    $params["month"] = "12";
    $params["day"] = "14";
    $params["hour"] = "17";
    $params["minute"] = "00";
    $params["deparr"] = "arr";
    $params["modality"] = "pt";
    $params["sourceFrom"] = "gps";
    $params["sourceTo"] = "stoplist";
    $req = buildRequest("api_get_route", $params);

    Afterwards, the XML-request must be sent to the following webservice: http://webservice.qando.at/2.0/webservice.ft.

    $response = httpPost("http://webservice.qando.at/2.0/webservice.ft", $req);

    Finally, the XML-response must be load into a Document Object Model (DOM). A DOM presents an XML document as a tree-structure, which makes it easy to interpret and evaluate the local public transport route.

    $dom = new DOMDocument();
    $dom->formatOutput = true;
    $dom->preserveWhiteSpace = false;
    $dom->loadXML($response);

    The complete DOMDocument manual is available on the website: http://php.net/manual/de/class.domdocument.php.

    To print out the XML-response of the public transport route, use the following PHP-code:

    echo "Response: " .$dom->saveXML(). "";

    Taggings:

    Converting XML to JSON and JSON to XML by using Javascript or Java

     There are several tested solutions available on the Internet already: JAVASCRIPT Solution approach:  It provides 

    • XML to JSON

       Download Skript: http://www.thomasfrank.se/downloadableJS/xml2json.js  Usage as documented on this site: <head><script type="text/javascript" src="xml2json.js"></script>...</head>Then it can be called in these different variants: 

    1. V1: /*A simple call - myXML is a string containing your XML:*/ myJsonObject=xml2json.parser(myXML);
    2. V2: /*A 2:nd, optional, parameter is "tags not to convert" - for example <b> and <i>:*/ myJsonObject=xml2json.parser(myXML,'b,i')
    3. V3: /*A 3:rd, optional, parameter gives us a string showing us the JSON structure instead of the actual JSON object: */ myString=xml2json.parser(myXML,'','html');

     V1 ("compact") can be used for output without linebreaks or tabbing. V2 for normal output with linebreaks ant tabbing. V3 for HTML representation.JAVA solution approach  (which is solution I have already tested and which is provided by Json.org):    The Source files are available at http://json.org/java/ for many JSON issues in Java.    The relevant class is called XML and is viewable at:  

      It provides 

    • XML to JSON
    • JSON to XML

     

    1. Create your class
    2. Import org.json.XML;
    3. Prepare an xml input value: String xmlValue = "<..>";
    4. /*XML to JSON*/ Type: JSONObject jsonResult = XML.toJSONObject(xmlValue);
    5. /*JSON to XML*/ Type: String xmlResult = XML.toString(jsonResult);

      Naturally, the XML has to be well-formed and valid to produce a JSON object. The JSON objcect istself has to be valid again. It can be assumed that always correct outputs for valid inputs are retrieved according its high adoption in real world usage. 

    Taggings:

    Subscribe to XML