Parsing XML content

  • Thread starter Thread starter Mike [MCP VB]
  • Start date Start date
M

Mike [MCP VB]

Hi, I hope this is the right place to ask.

I have an xml file thus:

<?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="urn:myurl-org:ns2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:myurl-org:ns2
...\Schemas\schema.xsd" type="Message">
<id root="7DC0FFE7-1224-4598-8F20-3B7662D416A1"/>
<creationTime value="2004"/>
<acknowledgement typeCode="AE">
<messageRef>
<id root="04074443-DE90-4FE8-9D4A-DBF9A7E81FFA"/>
</messageRef>
</acknowledgement>
</Message>

I wish to be able to parse the file such that I can return the root
attribute of the id node within messageRef.

Unfortunately, I cannot do this using msxml. getElementByTagName doesn't
work because a default namespace is provided. In fact, I cannot loop through
the nodes either, as msxml only suggests the document has one node <Message
/>, but no children.

I am considering swapping over from VB6 to VB.NET to accomplish this task,
as I've heard the system.xml namespace contains a revised implementation of
the DOM object that will allow use of getElementByTagName with default
namespaces.

Could anyone proffer the code to return the above attribute (as string, or
ixmlattributenode)?

Many thanks in advance.

Mike
 
Did you really intend to use this as an xml file? If not please post
complete xml file....but you probably should find a more on topic group.

Errors:
line 2, :
error (1102): tag uses GI for an undeclared element: Message
line 2, :
error (1203): attribute can't be checked because element is not defined:
Message
line 2, :
error (1203): attribute can't be checked because element is not defined:
Message
line 2, :
error (1203): attribute can't be checked because element is not defined:
Message
line 2, :
error (1203): attribute can't be checked because element is not defined:
Message
line 3, :
error (1102): tag uses GI for an undeclared element: id
line 3, :
error (1203): attribute can't be checked because element is not defined:
id
line 3, :
error (1150): enclosing tag undefined or lacks content model; can't
check child: id
line 4, :
error (1102): tag uses GI for an undeclared element: creationTime
line 4, :
error (1203): attribute can't be checked because element is not defined:
creationTime
line 4, :
error (1150): enclosing tag undefined or lacks content model; can't
check child: creationTime
line 5, :
error (1203): attribute can't be checked because element is not defined:
acknowledgement
line 5, :
error (1102): tag uses GI for an undeclared element: acknowledgement
line 6, :
error (1102): tag uses GI for an undeclared element: messageRef
line 7, :
error (1102): tag uses GI for an undeclared element: id
line 7, :
error (1203): attribute can't be checked because element is not defined:
id
line 7, :
error (1150): enclosing tag undefined or lacks content model; can't
check child: id
line 8, :
error (1103): end tag uses GI for an undeclared element: messageRef
line 8, :
error (1150): enclosing tag undefined or lacks content model; can't
check child: messageRef
line 9, :
error (1103): end tag uses GI for an undeclared element: acknowledgement
line 9, :
error (1150): enclosing tag undefined or lacks content model; can't
check child: acknowledgement
line 10, :
error (1103): end tag uses GI for an undeclared element: Message
line 11, :
error (402): EOF encountered; no doctype declaration found: Message

--------------------------------------------------------------------------------

Original Document:
line 2: <Message xmlns="urn:myurl-org:ns2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:myurl-org:ns2 ..\Schemas\schema.xsd"
type="Message">
line 3: <id root="7DC0FFE7-1224-4598-8F20-3B7662D416A1"/>
line 4: <creationTime value="2004"/>
line 5: <acknowledgement typeCode="AE">
line 6: <messageRef>
line 7: <id root="04074443-DE90-4FE8-9D4A-DBF9A7E81FFA"/>
line 8: </messageRef>
line 9: </acknowledgement>
line 10: </Message>
end-of-file
 
scorpion53061 said:
Did you really intend to use this as an xml file? If not please post
complete xml file....but you probably should find a more on topic group.

Cheers Scorpion,

There are more elements, but they aren't pertinent to this particular issue.
The validation fell over on the scoped namespace: "urn:myurl-org:ns", such
that multiple : characters are not allowed in this namespace.

Any ideas how to parse >ANY< xml file with the above default namespace
present in it? VB, VB.NET are fine. MSXML just won't have any of it...

Thanks!

Mike
 
Mike [MCP VB] wrote:

I have an xml file thus:

<?xml version="1.0" encoding="UTF-8"?>
<Message xmlns="urn:myurl-org:ns2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:myurl-org:ns2
..\Schemas\schema.xsd" type="Message">
<id root="7DC0FFE7-1224-4598-8F20-3B7662D416A1"/>
<creationTime value="2004"/>
<acknowledgement typeCode="AE">
<messageRef>
<id root="04074443-DE90-4FE8-9D4A-DBF9A7E81FFA"/>
</messageRef>
</acknowledgement>
</Message>

I wish to be able to parse the file such that I can return the root
attribute of the id node within messageRef.

Unfortunately, I cannot do this using msxml. getElementByTagName doesn't
work because a default namespace is provided.

It should be possible to use selectSingleNode/selectNodes with MSXML 3,
4, and 5, you just need to make sure that a namespace prefix is
associated with that default namespace e.g.
xmlDocument.setProperty "SelectionLanguage", "XPath"
xmlDocument.setProperty "SelectionNamespaces",
"xmlns:myorg='urn:myurl-org:ns2'"
then you can use that prefix e.g.
xmlDocument.selectSingleNode("/myorg:Message")
 
Martin Honnen said:
It should be possible to use selectSingleNode/selectNodes with MSXML 3, 4,
and 5, you just need to make sure that a namespace prefix is associated
with that default namespace e.g.
xmlDocument.setProperty "SelectionLanguage", "XPath"
xmlDocument.setProperty "SelectionNamespaces",
"xmlns:myorg='urn:myurl-org:ns2'"
then you can use that prefix e.g.
xmlDocument.selectSingleNode("/myorg:Message")

Cheers Martin. The problem was my .setProperty "SelectionNamespaces" line,
as I was not surrounding my namespace with single quotes, and therefore
msxml thought I was attempting to declare a default scoped namespace, which
apparantly - is a big no-no.

It now works regardless of the "SelectionLanguage" property... what's this
used for? Complex XPath?

However, XPath won't work in DOMDocument50.... but will in DOMDocument...
wonder why...?


--

Liddle Feesh
*fap fap fap fap*
<>< <>< <>< <>< ><>
<>< <>< <>< <>< <>< <><
 
Back
Top