Link to home
Start Free TrialLog in
Avatar of fox_statton
fox_statton

asked on

Simple regex function

Hi all,
I m trying to open an xml file using JSP and get some values:

I know this is PHP code, but its the only way I could figure out what to do...

<?
$datafeed=file_get_contents('http:/www.whatever.com/file.xml');

// Basically get whatever is between <exchange> and </exchange>

preg_match_all('/<exchange>(.*)<\/exchange>/', $datafeed, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
      for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
            echo $result[$matchi][$backrefi];
      }
}

?


Avatar of bloodredsun
bloodredsun
Flag of Australia image

You can use JSTL

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

<c:import var='xml' url='file.xml'/>
<x:parse xml="${xml}" var="doc" />
<x:out select="$doc/exchange"/>

or you could use this java code to get the data from the string

String page = "" ;//your xml
SAXBuilder builder = new SAXBuilder("org.apache.xerces.parsers.SAXParser");//use SAX
Document doc = builder.build(new InputSource(new StringReader( page)));

List exchangeList = (List)XPath.selectNode( doc , "//exchange") ;
String exchange = "" ;
//loop over list
for (Iterator i = studentNodeList.iterator(); i.hasNext();) {
      Element e = (Element) i.next();
      exchange = e.getText() ;
}
Avatar of fox_statton
fox_statton

ASKER

I guess what Im asking, or was trying to do is load an XML file that is in the format:

<shareprice>
  <exchange>London</exchange>
  <price>432.00</price>
  <date>26 Jan</date>
  <time>10:32am</time>
  <change>-5.00</change>
  <percent>-0.35</percent>
  <volume>58066</volume>
  </shareprice>

And then set each of the nodes as a variable and write them out, any ideas?
Just tried your first solution and got:

Error Message: org/saxpath/SAXPathException
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial