最近做了一个非常简单的在线考试系统:JSP+Servlet+mysql+ajax+存储过程,把一些遇到的问题发到这里记录一下
2010年12月22日星期三
2010年5月2日星期日
C#中的SqlBulkCopy将批量数据写入数据库
注意:ACCESS数据库不支持此操作
DataSet ds = new DataSet();
ds.ReadXml(new StringReader(sbuf.ToString())); //从字符串装载XML
DataTable mytable = ds.Tables[0];
//MessageBox.Show(mytable.Rows[1]["msgid"].ToString()); //将XML装载到DataSet后,可通过行和列来进行访问
SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connStr, SqlBulkCopyOptions.UseInternalTransaction);
sqlbulkcopy.DestinationTableName = "inbox";
sqlbulkcopy.WriteToServer(ds.Tables[0]);
C#将XML中的数据存储到ACCESS数据库
using (OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=msg.mdb"))
{
cn.Open();
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
XmlDocument xml = new XmlDocument(); //装载XML数据
xml.InnerXml = sbuf.ToString();
XmlNodeList xn = xml.GetElementsByTagName("message");
foreach (XmlElement xe in xn)
{
cmd.CommandText = string.Format(
"INSERT INTO inbox([msgid],[teacher],[content],[msgdate],[userid]) VALUES ('{0}', '{1}', '{2}', '{3}','{4}')",
xe.ChildNodes[0].InnerText.Trim(),
xe.ChildNodes[1].InnerText.Trim(),
xe.ChildNodes[2].InnerText.Trim(),
xe.ChildNodes[3].InnerText.Trim(),
username
);
cmd.ExecuteNonQuery();
}
}
cn.Close();
}
2010年5月1日星期六
C#中的XSLT转换
StringReader txtReader;
txtReader = new StringReader(cleanedMarkUp);
XPathDocument xdoc = new XPathDocument(txtReader);
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("msg.xsl");
XmlTextWriter writer = new XmlTextWriter(new StreamWriter("d:/r.xml"));
xslt.Transform(xdoc, null, writer);
writer.Close();
XSLT中for-each的select语法问题
在一个程序中,要用到如下的XSL用于XSLT转换
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="utf-8"/>
<xsl:template match="/">
<messages>
<xsl:for-each select="//tr[@bgcolor='#f3f2f1' and position()>=3 and position()<last()]">
<message>
<msgid>
<xsl:value-of select="td[1]/div/input/@value"/>
</msgid>
<teacher>
<xsl:value-of select="td[3]/div"/>
</teacher>
<content>
<xsl:value-of select="td[4]/div"/>
</content>
<timestamp>
<xsl:value-of select="td[6]/div"/>
</timestamp>
</message>
</xsl:for-each>
</messages>
</xsl:template>
</xsl:stylesheet>
在.NET中报错,出错信息为:
未处理 System.Xml.Xsl.XslLoadException
Message="XSLT 编译错误。"
Source="System.Data.SqlXml"
LineNumber=6
LinePosition=81
SourceUri="file:///D:/myprog/C%23/jxlltEasy/jxlltEasy/bin/Debug/msg.xsl"
StackTrace:
在 System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include)
在 System.Xml.Xsl.Xslt.XsltLoader.Load(Compiler compiler, Object stylesheet, XmlResolver xmlResolver)
在 System.Xml.Xsl.Xslt.Compiler.Compile(Object stylesheet, XmlResolver xmlResolver, QilExpression& qil)
在 System.Xml.Xsl.XslCompiledTransform.CompileXsltToQil(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
在 System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
在 System.Xml.Xsl.XslCompiledTransform.Load(String stylesheetUri)
在 jxlltEasy.jxlltForm.btnRefresh_Click(Object sender, EventArgs e) 位置 D:\myprog\C#\jxlltEasy\jxlltEasy\jxlltForm.cs:行号 120
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 jxlltEasy.Program.Main() 位置 D:\myprog\C#\jxlltEasy\jxlltEasy\Program.cs:行号 18
在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
在 System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Xml.XmlException
Message="“<”(十六进制值 0x3C)是无效的属性字符。 行 6,位置 81。"
Source="System.Xml"
LineNumber=6
LinePosition=81
SourceUri="file:///D:/myprog/C%23/jxlltEasy/jxlltEasy/bin/Debug/msg.xsl"
StackTrace:
在 System.Xml.XmlTextReaderImpl.Throw(Exception e)
在 System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
在 System.Xml.XmlTextReaderImpl.Throw(Int32 pos, String res, String[] args)
在 System.Xml.XmlTextReaderImpl.ParseAttributeValueSlow(Int32 curPos, Char quoteChar, NodeData attr)
在 System.Xml.XmlTextReaderImpl.ParseAttributes()
在 System.Xml.XmlTextReaderImpl.ParseElement()
在 System.Xml.XmlTextReaderImpl.ParseElementContent()
在 System.Xml.XmlTextReaderImpl.Read()
在 System.Xml.Xsl.Xslt.XsltInput.ReadNextSiblingHelper()
在 System.Xml.Xsl.Xslt.XsltInput.ReadNextSibling()
在 System.Xml.Xsl.Xslt.XsltInput.MoveToFirstChildAny()
在 System.Xml.Xsl.Xslt.XsltInput.MoveToFirstChild()
在 System.Xml.Xsl.Xslt.XsltLoader.LoadInstructions(List`1 content, InstructionFlags flags)
在 System.Xml.Xsl.Xslt.XsltLoader.LoadLiteralResultElement(Boolean asStylesheet)
在 System.Xml.Xsl.Xslt.XsltLoader.LoadInstructions(List`1 content, InstructionFlags flags)
在 System.Xml.Xsl.Xslt.XsltLoader.LoadTemplate(NsDecl stylesheetNsList)
在 System.Xml.Xsl.Xslt.XsltLoader.LoadRealStylesheet()
在 System.Xml.Xsl.Xslt.XsltLoader.LoadDocument()
在 System.Xml.Xsl.Xslt.XsltLoader.LoadStylesheet(XmlReader reader, Boolean include)
InnerException:
在Java中,出错信息为:
Fatal Error] :6:80: The value of attribute "select" associated with an element type "xsl:for-each" must not contain the '<' character.
Exception in thread "main" org.xml.sax.SAXParseException: The value of attribute "select" associated with an element type "xsl:for-each" must not contain the '<' character.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at cn.ldsoft.xpath.XMLHelper.parseXMLFromInputSource(XMLHelper.java:70)
at cn.ldsoft.xpath.XMLHelper.parseXMLFromFile(XMLHelper.java:62)
at cn.ldsoft.simple.Test.main(Test.java:22)
通过查阅资料得知,
合法的过滤运算符:
- = (等于)
- != (不等于)
- < (小于)
- > (大于)
将position()<last()改为position() < last(), 问题解决
使用C#、XML和XPATH从HTML中提取特定标记的值
StringReader txtReader;
txtReader = new StringReader(cleanedMarkUp);
XPathDocument xdoc = new XPathDocument(txtReader);
XPathNavigator nav = xdoc.CreateNavigator();
nav.MoveToRoot();
XPathExpression expr = nav.Compile("//tr[@bgcolor='#f3f2f1']");
XPathNodeIterator node = nav.Select(expr);
while (node.MoveNext())
MessageBox.Show(node.Current.Value);
2009年2月23日星期一
使用XPATH从XML/XHTML中提取数据
import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;public class Evaluation {
public static String extractString(String xmlStr, String xpath)
throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xp = factory.newXPath();
XPathExpression xpe = xp.compile(xpath);
InputSource is = new InputSource(new StringReader(xmlStr));
String result = xpe.evaluate(is);
return result;
}public static NodeList extractNodeList(String xmlStr, String xpath)
throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xp = factory.newXPath();
XPathExpression xpe = xp.compile(xpath);
InputSource is = new InputSource(new StringReader(xmlStr));
NodeList result = (NodeList) xpe.evaluate(is, XPathConstants.NODESET);
return result;
}public static String extractString(Node node, String xpath)
throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xp = factory.newXPath();
XPathExpression xpe = xp.compile(xpath);
return xpe.evaluate(node);
}
}
2008年6月14日星期六
Orangevolt EclipseXSLT 插件
构建在Eclipse WTP之上的XSLT支持插件,包括一个可定制的XML目录大纲,一个xpath查询视图等等。
安装:
在Eclipse中选择 Help > Software Updates > Find and Install ,添加下面的URL进行安装
除了XML编辑、XSLT转换功能之外,EclipseXSLT还提供了一个很好的XPATH Navigator视图。
XPath Navigator 视图是一个出色的特性。可以使用它从 XML 源文件创建 XPath 表达式。插件将针对 XML 文件的 DOM 表示执行这个 XPath 表达式。
选择 Window > Show View > Other...。
展开 Orangevolt 来显示 XPath Navigator 并单击 OK。
现在应该会看到 XPath Navigator 视图。
从 XML Source Document 下拉框中选择要处理的xml文件(该XML应该首先用Orangevolt XML编辑器打开,否则不能选择),我打开的XML文件内容如下。
在 XPath Expression 文本框中输入XPATH表达式。单击 Navigator 中的 Run 图标。 可以修改 XPath 表达式来生成不同的结果。
2008年5月10日星期六
编写AJAX程序时遇到的XML问题
日前在做一个JSP程序时用到了AJAX技术,需要由ASP生成一个XML文件发送给浏览器。以前用ASP做这个工作的时候没有遇到问题。
生成XML的JSP文件如下:
<%@ page language="java" pageEncoding="gb2312"%>
<%
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
response.setHeader("content-type","text/xml;charset=gb2312");
out.println("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
out.println("<docroot><node1>中国</node1></docroot>");
%>
这样FireFox就会说"xml处理指令不在外部实体的开始部分" ,而IE就说是java错误云云。通过google和看JSP转换成的java文件,在文件开始处多了回车换行。改成下面的就可以了
修改以后的JSP页面如下:
<%@ page language="java" pageEncoding="gb2312"%><%
response.setHeader("pragma","no-cache");
response.setHeader("cache-control","no-cache");
response.setHeader("expires","0");
response.setHeader("content-type","text/xml;charset=gb2312");
out.println("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
out.println("<docroot><node1>中国</node1></docroot>");
%>
类似输出流的问题在使用jspSmartUpload组件下载文件时同样也会遇到。