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_content s('http:/www.whatever.com/file.xml');
// Basically get whatever is between <exchange> and </exchange>
preg_match_all('/<exchange >(.*)<\/ex change>/', $datafeed, $result, PREG_SET_ORDER);
for ($matchi = 0; $matchi < count($result); $matchi++) {
for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
echo $result[$matchi][$backrefi ];
}
}
?
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_content
// Basically get whatever is between <exchange> and </exchange>
preg_match_all('/<exchange
for ($matchi = 0; $matchi < count($result); $matchi++) {
for ($backrefi = 0; $backrefi < count($result[$matchi]); $backrefi++) {
echo $result[$matchi][$backrefi
}
}
?
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?
<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?
ASKER
Just tried your first solution and got:
Error Message: org/saxpath/SAXPathExcepti on
Error Message: org/saxpath/SAXPathExcepti
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<%@ 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.xer
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()
Element e = (Element) i.next();
exchange = e.getText() ;
}