DocumentBuilder
public class test{ /**
* parse test.xml * @return TextContent of a node.
*/
public String getTextContent() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); String sPath = this.getClass().getResource("/")
.getPath() + File.separator + "test.xml"; Document document = builder.parse(sPath); NodeList nodeList = document.getElementsByTagName("test"); Node node = nodeList.item(0); return node.getTextContent(); } }
jsp
<%
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(pageContext.getServletContext().getResourceAsStream("/xml/test.xml"));
doc.normalize();
NodeList Xs = doc.getElementsByTagName("X");
String x = Xs.item(0).getFirstChild().getNodeValue();
x = new String(x.getBytes("gb2312"));
out.println(x);
%>
评论
发表评论