Searching through XML document

R

RiseY3K

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>
 
D

dingbat

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.
 
W

William Park

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 <[email protected]>, 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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top