我需要使用jscript用今天的日期更改节点值
XML文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<scraping Test>
<General>
<FormatVersion>1</FormatVersion>
<FromDate>2/28/2019 00:00:00</FromDate>
<ToDate>2/28/2019</ToDate>
</General>
</scraping Test>
Jscript(尝试2种方法)
var dom = new ActiveXObject("Microsoft.XMLDOM");
pathToXML = "Y:\Analysis33.xml" ;
dom.loadxml(pathToXML);
mydate = date();
myVar1 = mydate & " 00:00:00" ;
myVar2 = mydate ;
nNode = xmlDoc.selectsinglenode ("FromDate") ;
nNode.text = myvar1 ;
strResult = xmldoc.save(pathtoxml) ;
我尝试过MSXML2.DOM也类似的代码,但是没有用。
var objXML = new ActiveXObject("MSXML2.DOMDocument");
答案 0 :(得分:0)
Biggest problem is your XML is invalid and unparseable by any recipe of JScript sorcery until you fix it. Get rid of the illegal space in your <scraping Test>
tag. Make it <scrapingTest>
or <root>
.
You also need to select the text()
node in your XPath, then modify the resulting node's .data
property.
Other than that, fix all the cAmElCaSe disagreement in your code and get your date formatting under control. Using this XML:
<?xml version="1.0" encoding="ISO-8859-1"?>
<scrapingTest>
<General>
<FormatVersion>1</FormatVersion>
<FromDate>2/28/2019 00:00:00</FromDate>
<ToDate>2/28/2019</ToDate>
</General>
</scrapingTest>
This code works for me to parse and modify it:
var dom = WSH.CreateObject("MSXML2.DOMDocument.6.0"),
pathToXML = WSH.Arguments(0);
dom.load(pathToXML);
var nNode = dom.selectSingleNode("//FromDate/text()"),
today = new Date();
nNode.data = [today.getMonth(), today.getDate(), today.getFullYear()].join('/') + ' 00:00:00';
dom.save(pathToXML);