|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| Question |
|
[x]
Attachment Details
|
||
|
[x]
The Solution Rating System
|
||
With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.
Your Input Matters If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support. Thank you! |
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: |
import javax.xml.xpath.*;
import org.xml.sax.InputSource;
import java.io.StringReader;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class EvaluateTest {
/**
* @param args
*/
public static void main(String[] args) {
String myxml = "<?xml version='1.0' encoding='utf-8' ?><mytest><ChannelList>" +
"<Channel><ChannelId>0001</ChannelId><ChannelName>Ch1</ChannelName></Channel>" +
"<Channel><ChannelId>0002</ChannelId><ChannelName>Ch2</ChannelName></Channel>" +
"<Channel><ChannelId>0003</ChannelId><ChannelName>Ch3</ChannelName></Channel>" +
"</ChannelList></mytest>";
//Get the Document object
Document doc = null;
try {
InputSource inputSource = new InputSource();
inputSource.setCharacterStream( new StringReader(myxml));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
doc = db.parse(inputSource);
//Iterate thru the Channel list
NodeList channellist = getSubElements(doc, "/mytest/ChannelList/Channel");
if (channellist==null)
System.out.println("the channel list is null");
for (int i=0, len=channellist.getLength(); i<len; i++) {
System.out.println("Channel #" + i);
String name = getTagValue(channellist.item(i), "//ChannelName/text()", -1);
System.out.println("Channel Name : " + name);
}
} catch ( Exception e ) {
e.printStackTrace();
return;
}
}
//Get the text value of a tag from a document object using XPath method
static public String getTagValue(Object doc, String xpathstr, int index) throws Exception {
String result = "";
if ((doc==null) || (xpathstr==null) || "".equals(xpathstr))
return result;
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
result = (String)xpath.evaluate(xpathstr, doc, XPathConstants.STRING);
if (result==null)
result = "";
return result;
}
static public NodeList getSubElements(Object doc, String xpathstr) throws Exception {
if ((doc==null) || (xpathstr==null) || "".equals(xpathstr))
return null;
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
Object result = xpath.evaluate(xpathstr, doc, XPathConstants.NODESET);
return (NodeList)result;
}
}
|
Advertisement
| Hall of Fame |