Searching through XML document

Discussion in 'XML' started by RiseY3K@gmail.com, Sep 8, 2005.

  1. Guest

    Hi,

    I am fairly new to XML, any help is appreciated.

    I would like to add a search box on my site, that will allow me to
    search through NAME of the xml-list like the one below, and display a
    user with their information. Name can be entered as firstname, lastname
    or both.

    <EMPLOYEE_LIST>
    <EMPLOYEE>
    <NAME>Kevin A Freeman</NAME>
    <DOB>08/23/68</DOB>
    <HIRE>07/01/97</HIRE>
    <PIC>images/KevinFreeman.jpg</PIC>
    <POSITION>Sales Manager</POSITION>
    </EMPLOYEE>
    <EMPLOYEE>
    <NAME>Cindy Onkalavia</NAME>
    <DOB>09/16/74</DOB>
    <HIRE>07/01/97</HIRE>
    <PIC>images/CindyOnkalavia.jpg</PIC>
    <POSITION>Auditor</POSITION>
    </EMPLOYEE>
     
    , Sep 8, 2005
    #1
    1. Advertising

  2. Guest

    wrote:

    > I would like to add a search box on my site, that will allow me to
    > search through NAME of the xml-list like the one below,


    Use a database instead - this is what they _do_.

    You _could_ build this with XML. But you can't just grab a pre-existing
    example, as you could with a SQL database. So for "simple" cases, it's
    more work.

    XML is also _NOT_ a searchable format for storing huge quantities of
    data (at least not through the DOM/XPath route, which is what you'll be
    using if you build simpel XML sites with simple XML tool (i.e. free
    stuff that works in PHP)). XML whole-document searches are horribly
    slow. So if this is a big list, then it's a poor performing search.

    One day we'll use XML databases with XQuery, just as we currently use
    simple cheap SQL tools like MySQL - This might already have happened.
    But these will be storing their searchable indices in some internal
    non-XML format and limiting the XML format you show here to data
    loading at most.
     
    , Sep 8, 2005
    #2
    1. Advertising

  3. Stylus Studio, Sep 8, 2005
    #3
  4. William Park Guest

    wrote:
    > Hi,
    >
    > I am fairly new to XML, any help is appreciated.
    >
    > I would like to add a search box on my site, that will allow me to
    > search through NAME of the xml-list like the one below, and display a
    > user with their information. Name can be entered as firstname, lastname
    > or both.
    >
    > <EMPLOYEE_LIST>
    > <EMPLOYEE>
    > <NAME>Kevin A Freeman</NAME>
    > <DOB>08/23/68</DOB>
    > <HIRE>07/01/97</HIRE>
    > <PIC>images/KevinFreeman.jpg</PIC>
    > <POSITION>Sales Manager</POSITION>
    > </EMPLOYEE>
    > <EMPLOYEE>
    > <NAME>Cindy Onkalavia</NAME>
    > <DOB>09/16/74</DOB>
    > <HIRE>07/01/97</HIRE>
    > <PIC>images/CindyOnkalavia.jpg</PIC>
    > <POSITION>Auditor</POSITION>
    > </EMPLOYEE>
    > .
    > .
    > <EMPLOYEE>
    > <NAME>Kevin Lee</NAME>
    > <DOB>03/25/81</DOB>
    > <HIRE>01/13/05</HIRE>
    > <PIC>images/KevinLee.jpg</PIC>
    > <POSITION>MIT</POSITION>
    > </EMPLOYEE>
    > </EMPLOYEE_LIST>


    Text file, SQL, anything is better than XML. But, since you're stuck
    with XML format, you might try something like

    1. awk -v RS='</?EMPLOYEE>' "/Kevin/" file.xml

    2. csplit file.xml '/<EMPLOYEE>/' '{*}'
    for i in xx*; do
    grep -q "Kevin" $i && cat $i
    done

    3. Use shell interface to Expat XML parser,

    start()
    {
    case $1 in
    EMPLOYEE) unset name dob hire pic position ;;
    esac
    }
    data()
    {
    tag=${XML_TAG_STACK[0]}
    case $tag in
    NAME|DOB|HIRE|PIC|POSITION) strcat $tag $1 ;;
    esac
    }
    end()
    {
    case $1 in
    EMPLOYEE)
    if [[ $NAME == *Kevin* ]]; then
    cat <<+ EOF
    NAME=$NAME
    DOB=$DOB
    HIRE=$HIRE
    PIC=$PIC
    POSITION=$POSITION
    EOF
    fi
    ;;
    esac
    }
    expat -s start -d data -e end < file.xml

    --
    William Park <>, Toronto, Canada
    ThinFlash: Linux thin-client on USB key (flash) drive
    http://home.eol.ca/~parkw/thinflash.html
    BashDiff: Super Bash shell
    http://freshmeat.net/projects/bashdiff/
     
    William Park, Sep 8, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Hertz
    Replies:
    0
    Views:
    328
    Michael Hertz
    Sep 19, 2004
  2. Tony Prichard
    Replies:
    0
    Views:
    734
    Tony Prichard
    Dec 12, 2003
  3. Manish Hatwalne
    Replies:
    1
    Views:
    401
    Martin Honnen
    Jul 13, 2004
  4. Michael Hertz
    Replies:
    3
    Views:
    444
    Peter Flynn
    Sep 22, 2004
  5. hagar
    Replies:
    1
    Views:
    383
    Simon Brooke
    Oct 19, 2006
Loading...

Share This Page