0

I would like to extract a single value from a large xml file (part of a much larger xml project that I'm working on)

So the xml structure is like this:

<m:Report>
    <m:Messages>
        <m:report_type>
            <m:Report_Data> (last value)
                <m:Data_Item> (must be specific type)
                <m:Value> (want this)

After this all tags close properly

So right xpath query looks something like this:

XPathExpression expr = x.compile("//m:Report/m:Messages/m:Report_type/m:Report_Data[m:Data_Iten='PRICE'][last()]/m:Value/text()") ;

I'm not having much luck with this query, where am I going wrong?

A_Elric
  • 3,508
  • 13
  • 52
  • 85
  • If that's a very large XML file, you'd better use a SAX parser , or a StAX parser. see also: http://stackoverflow.com/questions/355909 – Pierre Jul 26 '12 at 18:47
  • By very large I meant about 250kb ~10k lines of xml. Think Sax / StAX would still be necessary? – A_Elric Jul 26 '12 at 18:49
  • Are you correctly handling namespaces? See here: http://stackoverflow.com/questions/6390339/how-to-query-xml-using-namespaces-in-java-with-xpath/6392700#6392700 – Wayne Jul 26 '12 at 19:14
  • That is a really good question, and I honestly have no idea. – A_Elric Jul 26 '12 at 19:26

1 Answers1

0

There are simply errors in your query. Report_type should be report_type and Data_Iten should be Data_Item:

XPathExpression expr = x.compile("//m:Report/m:Messages/m:report_type/m:Report_Data[m:Data_Item='PRICE'][last()]/m:Value/text()");

Assuming that you have a Data_Item in your input data with PRICE, then this should get you the <m:Value>.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • 1
    Or if those are typos in submitting the question, I would question the use of [last()]. It's not obviously wrong, but you haven't given enough information for us to know that it is right. – Michael Kay Jul 26 '12 at 22:38