Wiki Xml
From Fxp Wiki
Lire un fichier xml depuis un jsp Parse an xml file from a jsp
NOT TRANSLATED YET (see French please)
userTable.xml
Joe Some Dude
xml.jsp
//Page Directives
<%@ page language="java" %> <%@ page import="javax.xml.parsers.*" %> <%@ page import="org.w3c.dom.Document" %> <%@ page import="org.w3c.dom.Element" %> <%@ page import="org.w3c.dom.DOMException" %> <%@ page import="java.net.URL" %> <%@ page import="java.io.InputStream" %> <%@ page import="java.io.IOException" %>
<%! String fileName= "http://localhost:8080/JSPWiki2/userTable.xml";%>
<%! String msg="Parse Successful!";%>
<%! String nodeValue="No Value";%>
<%! String userID="";%>
<%! String pwd = "";%>
<%! int listLength;%>
//HTML Header
//Init Page
<%
//Declare variables Document document; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Initialise variables: msg="Parse Successful!"; routeURL = "default.htm";
//Read XML file
try { //Open the file for reading: URL u = new URL(fileName); InputStream inputXML = u.openStream();
//Build document: DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(inputXML);
//Generate the NodeList; org.w3c.dom.NodeList nodeList = document.getElementsByTagName("user"); listLength = nodeList.getLength();
//Search for User's Record
outer:
for (int i=0; i //Get userID attribute:
Element curElm = (Element)nodeList.item(i);
String curUserID = curElm.getAttribute("userID");
out.print(curUserID);
//Get pwd attribute:
String curPwd = curElm.getAttribute("pwd");
out.print(curPwd);
if (curUserID.equals("Joe") )
{ pwd = curPwd;
userID = curUserID;
break outer;
} //end if
}//end for
//Exception Handling
}catch(Exception e)
{
msg = msg + e.toString();
}
%>
Message: <%=msg%>
User: <%=userID%>
Pass: <%=pwd%>
//Page Routing
[[CategoryXml];
Informatique]
Categories: Wikis | Java
