do we need document object to use xpath

Hi,

I am reading as below

create document object as below.


Document document = builder.parse(
new FileInputStream("c:\\customers.xml"));



then use XPath by creating an xpath object using XPathFactory.
Reading names of all customers
String expression = "/Customers/Customer/firstname";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);


do we need document object to use xpath?
please advise
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
do we need document object to use xpath?
please advise
Yes. XPath works by inspecting a DOM
0
gudii9Author Commented:
does XPath do not work with SAX(works only DOM). Please advise
0
CEHJCommented:
Yes, only with DOM
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

gudii9Author Commented:
why does xpath inspect DOM in between. why does xpath inspect XML directly?
Please advice
0
CEHJCommented:
What do you mean by 'in between'?
0
gudii9Author Commented:
Document document = builder.parse(
new FileInputStream("c:\\customers.xml"));

I mean above step is extra in between step right(i.e building DOM from the XML) Please advise
0
CEHJCommented:
Well, we've just established that you need a DOM so if you haven't got one, then i don't see why taking all necessary steps to create one is in any way surprising ..?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.