今天把校园网站首页再修改了下,实现了图片新闻,自动添加,自动读取。
==
主要实现:
1。从新闻内容提取图片地址
2。相关性信息保存到xml文件
3。前台读取xml
1。简单的学了学正则表达式,发现真的很好很强大,不过.net 和js 的正则表达式区别还是有些不同
Dim
reg
As
Regex
Dim m As Match
reg = New Regex( " <img.*?s+src=(?:[""'])?([^""']*)(?:[""'s])+.*?> " , RegexOptions.IgnoreCase)
m = reg.Match(mystr)
If m.Success Then
Return m.Groups( 1 ).Value
Else
Return " no "
End If
Dim m As Match
reg = New Regex( " <img.*?s+src=(?:[""'])?([^""']*)(?:[""'s])+.*?> " , RegexOptions.IgnoreCase)
m = reg.Match(mystr)
If m.Success Then
Return m.Groups( 1 ).Value
Else
Return " no "
End If
2。实现了xml读写函数类,发现XPath很好用
Public
Class XMLReadWrite
Class XMLReadWrite
Dim mXmlDoc As New System.Xml.XmlDocument
Public XmlFile As String
Public Sub New()Sub New(ByVal File As String)
MyClass.XmlFile = File
MyClass.mXmlDoc.Load(MyClass.XmlFile) '加载配置文件
End Sub
'功能:取得元素值
'参数:node--节点 element--元素名
'返回:元素值 字符型
' $--表示出错误
Public Function GetElement()Function GetElement(ByVal node As String, ByVal element As String) As String
On Error GoTo Err
Dim mXmlNode As System.Xml.XmlNode = mXmlDoc.SelectSingleNode("//" + node)
'读数据
Dim xmlNode As System.Xml.XmlNode = mXmlNode.SelectSingleNode(element)
Return xmlNode.InnerText.ToString
Err:
Return "$"
End Function
'
'功能:保存元素值
'参数:node--节点名称 element--元素名 val--值
'返回:True--保存成功 False--保存失败
Public Function SaveElement()Function SaveElement(ByVal node As String, ByVal element As String, ByVal val As String) As Boolean
On Error GoTo err
Dim mXmlNode As System.Xml.XmlNode = mXmlDoc.SelectSingleNode("//" + node)
Dim xmlNodeNew As System.Xml.XmlNode
xmlNodeNew = mXmlNode.SelectSingleNode(element)
xmlNodeNew.InnerText = val
mXmlDoc.Save(MyClass.XmlFile)
Return True
err:
Return False
End Function
End Class
Dim mXmlDoc As New System.Xml.XmlDocument
Public XmlFile As String
Public Sub New()Sub New(ByVal File As String)
MyClass.XmlFile = File
MyClass.mXmlDoc.Load(MyClass.XmlFile) '加载配置文件
End Sub
'功能:取得元素值
'参数:node--节点 element--元素名
'返回:元素值 字符型
' $--表示出错误
Public Function GetElement()Function GetElement(ByVal node As String, ByVal element As String) As String
On Error GoTo Err
Dim mXmlNode As System.Xml.XmlNode = mXmlDoc.SelectSingleNode("//" + node)
'读数据
Dim xmlNode As System.Xml.XmlNode = mXmlNode.SelectSingleNode(element)
Return xmlNode.InnerText.ToString
Err:
Return "$"
End Function
'
'功能:保存元素值
'参数:node--节点名称 element--元素名 val--值
'返回:True--保存成功 False--保存失败
Public Function SaveElement()Function SaveElement(ByVal node As String, ByVal element As String, ByVal val As String) As Boolean
On Error GoTo err
Dim mXmlNode As System.Xml.XmlNode = mXmlDoc.SelectSingleNode("//" + node)
Dim xmlNodeNew As System.Xml.XmlNode
xmlNodeNew = mXmlNode.SelectSingleNode(element)
xmlNodeNew.InnerText = val
mXmlDoc.Save(MyClass.XmlFile)
Return True
err:
Return False
End Function
End Class
3。js实现读取xml文件
var
oXmlDom
=
new
XmlDom();
oXmlDom.async = false ;
oXmlDom.load( " xml/1.xml " ); // xml路径
// alert(oXmlDom.xml);
var aImgLists = new Array();
aImgLists = oXmlDom.documentElement.selectNodes( " //imageunits/imageunit " );
for ( var i = 0 ; i < aImgLists.length; i ++ ) ... {
slideimages[i]=aImgLists[i].selectSingleNode("imageurl").text;
slidetext[i]=aImgLists[i].selectSingleNode("thetitle").text;
slidelinks[i]=aImgLists[i].selectSingleNode("tolink").text;
}
oXmlDom.async = false ;
oXmlDom.load( " xml/1.xml " ); // xml路径
// alert(oXmlDom.xml);
var aImgLists = new Array();
aImgLists = oXmlDom.documentElement.selectNodes( " //imageunits/imageunit " );
for ( var i = 0 ; i < aImgLists.length; i ++ ) ... {
slideimages[i]=aImgLists[i].selectSingleNode("imageurl").text;
slidetext[i]=aImgLists[i].selectSingleNode("thetitle").text;
slidelinks[i]=aImgLists[i].selectSingleNode("tolink").text;
}
天煞的脚本调试,一个变量写成了大写,调试了一晚上,TMD,还好弄出来了。
js脚本语言先天性要求浏览器支持,为了实现通用性,比解决问题复杂多了。唉,可怕的浏览器陷阱!!