This is Interesting: Free Magazines for Graphics designers and webmasters
Home > Archive > Microsoft XML > September 2007 > Determing/Replacing the value for a child node
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Determing/Replacing the value for a child node
|
|
|
| I am trying to eventually replace a child node with another, but right now
just trying to extract the text value out of it.
My XML file:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2"
xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
..
..
..
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
</Principal>
</Principals>
</Task>
---
My VBscript:
Dim XmlDocument, StoreNum
Set XmlDocument = CreateObject("Msxml2.DOMDocument.3.0")
XmlDocument.async = False
XmlDocument.load ("c:\scripts\war45minmf")
XmlDocument.setProperty "SelectionLanguage", "XPath"
Set StoreNum = XmlDocument.selectSingleNode("Task/Prinicipals/Principal")
If Not StoreNum Is Nothing Then
WScript.Echo "STORENUM: " & StoreNum.text
Else
'insert error trap
WScript.Echo "Parse error: " & XmlDocument.parseError.reason
End If
----
I just get a Parse error message box, and nothing else. Eventually, I would
like the XML to look like this with replacing the User node with GroupId:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2"
xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
..
..
..
<Principals>
<Principal id="Author">
<RunLevel>LeastPrivilege</RunLevel>
<GroupId>Users</GroupId>
</Principal>
</Principals>
</Task>
---
Any thoughts/suggestions?
Thank you in advance!
| |
| Martin Honnen 2007-09-27, 6:18 pm |
| SaHiL wrote:
> I am trying to eventually replace a child node with another, but right now
> just trying to extract the text value out of it.
>
> My XML file:
>
> <?xml version="1.0" encoding="UTF-16"?>
> <Task version="1.2"
> xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
> .
> .
> .
> <Principals>
> <Principal id="Author">
> <UserId>S-1-5-18</UserId>
> </Principal>
> </Principals>
> </Task>
Your script has typos for the element names in the XPath expression,
furthermore you need to take care of the default namespace in the XML so
try the following:
Dim XmlDoc, UserId, GroupId
Set XmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
XmlDoc.async = False
If XmlDoc.load("file1.xml") Then
XmlDoc.setProperty "SelectionLanguage", "XPath"
XmlDoc.setProperty "SelectionNamespaces", "xmlns:df='" &
XmlDoc.documentElement.namespaceURI & "'"
Set UserId =
XmlDoc.selectSingleNode("df:Task/df:Principals/df:Principal/df:UserId")
If Not UserId Is Nothing Then
WScript.Echo "Found user id " & UserId.text
Set GroupId = XmlDoc.createNode(1, "GroupId",
UserId.ParentNode.NamespaceURI)
GroupId.text = "Users"
UserId.ParentNode.ReplaceChild GroupId, UserId
XmlDoc.save "file2.xml"
Else
WScript.Echo "UserId element not found."
End If
Else
WScript.Echo "Error parsing XML: " & XmlDoc.parseError.reason
End If
Result looks like this:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2"
xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Principals>
<Principal id="Author">
<GroupId>Users</GroupId></Principal>
</Principals>
</Task>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
| |
|
| Thanks Martin.
That is exactly what I was trying to do. However it turns out Vista has some
very twisted ideas about trying to manually edit the task file.
But your help is greatly appreciated!
Cheers.
"Martin Honnen" wrote:
> SaHiL wrote:
>
> Your script has typos for the element names in the XPath expression,
> furthermore you need to take care of the default namespace in the XML so
> try the following:
>
> Dim XmlDoc, UserId, GroupId
> Set XmlDoc = CreateObject("Msxml2.DOMDocument.3.0")
> XmlDoc.async = False
> If XmlDoc.load("file1.xml") Then
> XmlDoc.setProperty "SelectionLanguage", "XPath"
> XmlDoc.setProperty "SelectionNamespaces", "xmlns:df='" &
> XmlDoc.documentElement.namespaceURI & "'"
> Set UserId =
> XmlDoc.selectSingleNode("df:Task/df:Principals/df:Principal/df:UserId")
> If Not UserId Is Nothing Then
> WScript.Echo "Found user id " & UserId.text
> Set GroupId = XmlDoc.createNode(1, "GroupId",
> UserId.ParentNode.NamespaceURI)
> GroupId.text = "Users"
> UserId.ParentNode.ReplaceChild GroupId, UserId
> XmlDoc.save "file2.xml"
> Else
> WScript.Echo "UserId element not found."
> End If
> Else
> WScript.Echo "Error parsing XML: " & XmlDoc.parseError.reason
> End If
>
>
> Result looks like this:
>
> <?xml version="1.0" encoding="UTF-16"?>
> <Task version="1.2"
> xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
> <Principals>
> <Principal id="Author">
> <GroupId>Users</GroupId></Principal>
> </Principals>
> </Task>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
>
|
|
|
| | Copyright 2003 - 2008 forum4designers.com Software forum Computer Hardware reviews |
|