Coder Social home page Coder Social logo

sh-xml's Introduction

sh-xml

A (very) naive XML parser for sh

-- inspired by this StackOverflow Q/A : http://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash

Usage

First, copy xml_parser.sh in a place at your conveniance, then include it in your own script

. /path/to/xml_parser.sh

To parse a XML file, you need to create a function in which all the informations of all XML entities found will be sent. Then, you juste have to call the parse_xmlfunction with, as first parameter, the name of the "treatment" function created bellow and the path to the XML file, as second argument :

do_xml() {
  # ...
}
parse_xml "do_xml" /path/to/the/file.xml

In this example, the do_xml function will be called for every XML entity. In sh-xml, an XML entity correspond to the current tag found, and the following content. So, in your treatment function, you have access to the following variables :

  • XML_ENTITY: The current XML entity
  • XML_CONTENT : Data found after the current XML entity
  • XML_TAG_NAME : Name of the current tag. If the current tag is a close tag, the heading "/" is present in the tag name
  • XML_TAG_TYPE : Type of the current tag. The value can be "OPEN", "CLOSE", "EMPTY", "COMMENT" or "INSTRUCTION"
  • XML_COMMENT : If the current tag is of type "COMMENT", this variable contains the text of the comment
  • XML_PATH : Full XPath path of the current tag

You can also use these helper function:

has_attribute

This function take, as first parameter, the name of an attribute, and indicate if it exist within the current XML entity. Thus, this function return 1 if the attribute exist, 0 otherwise.

Example

do_xml() {
  has_attribute "password"
  if [ "$?" = "1" ] ; then
    # do something with the current XML entity
  fi
}

get_attribute_value

This function allow you to get the value of the attribute which name is passed as parameter

Example

do_xml() {
  PASSWORD_VALUE=$(get_attribute_value "password")
  # Do something with PASSWORD_VALUE
}

set_attribute_value

This function allow you to set the value of the given attribute. If the attribute exist for the current XML entity, its value is updated.

Example

do_xml() {
  set_attribute_value "password" "mYp4sSw0rD"
}

print_entity

This function allow you to print the current XML entity to stdout

Example

print_xml() {
   print_entity
}
parse_xml "print_xml" /path/to/sample.xml

terminate_parser

Terminate parsing. The end of the XML file will not be read.

Example

do_xml() {
  if [ "$XML_PATH" = "/stop/reading/here" ] ; then
    # You can either access all data for this entity
    terminate_parser
  end
}

Tests

This project use shUnit2 to perform unit tests. Just run make to run the tests suite. All tests are in the tests directory.

sh-xml's People

Contributors

glejeune avatar

Stargazers

Hugh JF Chen avatar Sergey Voloshchuk avatar John Murowaniecki avatar Troy Howard avatar kyzh avatar

Watchers

 avatar Troy Howard avatar James Cloos avatar  avatar

Forkers

hughjfchen

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.