Solved

XML Nodes and show tree

Posted on 2013-06-14
2
467 Views
Last Modified: 2013-06-14
I would like a function that I pass in XML and get the "schema" or "tree" back

For example
From this xml I wouls set @XmlDoc
ss
And get this returned
ss2
0
Comment
Question by:lrbrister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39249025
Hi.

Using your example XML:
DECLARE @XmlDoc AS XML = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<org.m5.api.v1.Response xmlns:m5="http://www.m5net.com/org/m5/data/v2/cti" xmlns:csta="http://www.ecma-international.org/standards/ecma-323/csta/ed5">
    <ErrorCount>0</ErrorCount>
    <Id>7</Id>
    <Result xsi:type="org.m5.data.v2.cti.HostedConnectObject" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <csta:MonitorStartResponse>
            <csta:monitorCrossRefID>726</csta:monitorCrossRefID>
        </csta:MonitorStartResponse>
    </Result>
</org.m5.api.v1.Response>
';

Open in new window


You can try something like this:
- search for all child nodes '//*'
- get local-name of the current node '.'
- get local-name of the current node's parent '..'
SELECT x.n.value('fn:local-name(.)', 'varchar(255)') AS [Node]
     , x.n.value('fn:local-name(..)', 'varchar(255)') AS [ParentNode]
FROM @XmlDoc.nodes('//*') x(n)
;

Open in new window


MSDN: http://msdn.microsoft.com/en-us/library/ms188282.aspx

I hope that helps!

Best regards and happy coding,

Kevin
0
 

Author Closing Comment

by:lrbrister
ID: 39249055
Thanks.
That worked as well as EXEC sp_xml_preparedocument @handle OUTPUT, @XmlDoc
Watch for follow-up
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
push and Pull replication 31 48
CURL sending XML without spaces PHP 5 38
SQL Syntax 6 32
SQL - Error "The Multi-Part Identifier could not be found" 2 21
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question