Sign in to follow this  
Followers 0
JRowe

XMLDomWrapper: parsing informatoin from an xml file

5 posts in this topic

I can't quite figure out what pieces fit where.

I have some xml, and I want to traverse the document from start to finish. I can load it using the XMLDOMWrapper, but I don't have a clue how to navigate the UDF.

Here's my XML:

<?xml version="1.0" encoding="UTF-8"?>
<RDF>
    <CLASS rdfAbout="Badminton_accessory">
    <rdfsLabel xml:lang="en">badminton accessory</rdfsLabel>
    <prettyString xml:lang="en">accessory for playing badminton</prettyString>
    <prettyString xml:lang="en">badminton accessories</prettyString>
    <cycLabel xml:lang="en">(AccessoryFn-Activity (PlayingFn Badminton))</cycLabel>
    <rdfsComment xml:lang="en"><![CDATA[The collection of all badminton accessories. A type of <a href="http://sw.opencyc.org/2008/06/10/concept/en/Artifact" class="cyc_term">Artifact</a>.]]></rdfsComment>
    <cycExternalID>Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rvVjiGZwpEbGdrcN5Y29ycA</cycExternalID>
    <rdfType rdfResource="ArtifactTypeByGenericCategory"/>
    <rdfType rdfResource="SpatiallyDisjointObjectType"/>
    <rdfsSubClassOf rdfResource="Artifact"/>
    <owlSameAs rdfResource="http://umbel.org/umbel/sc/Badminton_accessory"/>
    <owlSameAs rdfResource="http://sw.cyc.com/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rvVjiGZwpEbGdrcN5Y29ycA"/>
    <owlSameAs rdfResource="http://sw.opencyc.org/2008/06/10/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rvVjiGZwpEbGdrcN5Y29ycA"/>
    <owlSameAs rdfResource="http://sw.opencyc.org/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rvVjiGZwpEbGdrcN5Y29ycA"/>
    </CLASS>
    <CLASS rdfAbout="Baseball_accessory">
    <rdfsLabel xml:lang="en">baseball accessory</rdfsLabel>
    <prettyString xml:lang="en">accessory for playing baseball</prettyString>
    <prettyString xml:lang="en">baseball accessories</prettyString>
    <cycLabel xml:lang="en">(AccessoryFn-Activity (PlayingFn Baseball-TheGame))</cycLabel>
    <rdfsComment xml:lang="en"><![CDATA[The collection of all baseball accessories. A type of <a href="http://sw.opencyc.org/2008/06/10/concept/en/Artifact" class="cyc_term">Artifact</a>.]]></rdfsComment>
    <cycExternalID>Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rv1T7_pwpEbGdrcN5Y29ycA</cycExternalID>
    <rdfType rdfResource="ArtifactTypeByGenericCategory"/>
    <rdfType rdfResource="SpatiallyDisjointObjectType"/>
    <rdfsSubClassOf rdfResource="Artifact"/>
    <owlSameAs rdfResource="http://umbel.org/umbel/sc/Baseball_accessory"/>
    <owlSameAs rdfResource="http://sw.cyc.com/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rv1T7_pwpEbGdrcN5Y29ycA"/>
    <owlSameAs rdfResource="http://sw.opencyc.org/2008/06/10/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rv1T7_pwpEbGdrcN5Y29ycA"/>
    <owlSameAs rdfResource="http://sw.opencyc.org/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rv1T7_pwpEbGdrcN5Y29ycA"/>
    </CLASS>
    <CLASS rdfAbout="Basketball_accessory">
    <rdfsLabel xml:lang="blue">basketball accessory</rdfsLabel>
    <prettyString xml:lang="en">accessory for playing basketball</prettyString>
    <prettyString xml:lang="en">basketball accessories</prettyString>
    <cycLabel xml:lang="en">(AccessoryFn-Activity (PlayingFn Basketball-TheGame))</cycLabel>
    <cycExternalID>Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rvVjG0ZwpEbGdrcN5Y29ycA</cycExternalID>
    <rdfType rdfResource="ArtifactTypeByGenericCategory"/>
    <rdfType rdfResource="ExistingObjectType"/>
    <rdfType rdfResource="SpatiallyDisjointObjectType"/>
    <rdfsSubClassOf rdfResource="Artifact"/>
    <owlSameAs rdfResource="http://sw.cyc.com/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rvVjG0ZwpEbGdrcN5Y29ycA"/>
    <owlSameAs rdfResource="http://sw.opencyc.org/2008/06/10/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rvVjG0ZwpEbGdrcN5Y29ycA"/>
    <owlSameAs rdfResource="http://sw.opencyc.org/concept/Mx8Ngh4rvYthVpwpEbGdrcN5Y29ycB8Ngh4rvVjqHpwpEbGdrcN5Y29ycB4rvVjG0ZwpEbGdrcN5Y29ycA"/>
    </CLASS>
</RDF>

There are two types of data as far as I can tell. There's the data encapsulated by tags, like <data>this </data> , and data inside a tag, like <data type="this"/>.

How do I get either of those?

Share this post


Link to post
Share on other sites



Share this post


Link to post
Share on other sites

#include <_XMLDomWrapper.au3>
#include <Array.au3>
$sXmlFile = _XMLFileOpen(@ScriptDir & "\test.xml")

ConsoleWrite($sXmlFile&@CRLF)

$iClasses = _XMLGetNodeCount("/ROOT/CLASS")

ConsoleWrite($iClasses&@CRLF)

$cnt = $iClasses

Global $aAttrName[1], $aAttrValue[1], $i


$sRet = _XMLSelectNodes("//CLASS")

For $X = 1 to $sRet[0]
    ConsoleWrite("prettyString: " & _GetFirstValue("//CLASS[" & $X & "]/prettyString") & @CRLF & "cycLabel: " & _GetFirstValue("//CLASS[" & $X & "]/cycLabel") & @CRLF)
Next

;Get the first real value returned from the _XMLGetValue() return array.
Func _GetFirstValue($node)
    $ret_val = _XMLGetValue($node)
    If IsArray($ret_val) Then
    Return ($ret_val[1])
    Else
    Return SetError(1,3,0)
    EndIf
EndFunc

$path = "//CLASS"

;Retrieve all subnodes of "Keys"
$nodesArray = _XMLGetChildNodes($path)

;Retrieve all subnodes (same result as above)
;$nodesArray = _XMLSelectNodes($path & "/*")

;Retrieve all subnodes named "Key"
;$nodesArray = _XMLSelectNodes($path & "/Key")

_ArrayDisplay($nodesArray)

$nodes = _XMLGetNodeCount("//CLASS/rdfType")
If $nodes =0 then
    MsgBox(0,"Error","No nodes found")
    Exit
EndIf


    $msg = ""
    Global $aName1[1],$aValue1[1], $test


    ;ConsoleWrite("Number of results: " & $test[0][0] & @CRLF & $nodes & @CRLF)
    ;ConsoleWrite("Number of results: " & $test[0][0] & @CRLF & $nodes & @CRLF)

For $x = 0 to $nodes-1
    $test = _XMLGetAllAttribIndex('//CLASS/rdfType', $aName1,$aValue1, "", $x)
    ConsoleWrite($aName1[0] & ": " & $aValue1[0] & $test & @CRLF)
Next

I can get the attributes now, but I don't know how to associate them to their respective objects(or CLASSes in this case.)

Is there a way I can associate an array of attributes with a particular node/object/CLASS ?

Share this post


Link to post
Share on other sites

Neat. I just learned about XPath and how to query XML documents. Pretty simple once I figured out that you could do that... I had been under the impression that you had to set up all the query style stuff manually.

So to iterate through the document, all I need to do is find the path of the nodes I want to iterate through, namely, the CLASS nodes.

#include <_XMLDomWrapper.au3>
$sXmlFile = _XMLFileOpen(@ScriptDir & "\test.xml")
$nodesArray = _XMLSelectNodes("//CLASS/@*")

For $i = 0 to $nodesArray[0] -1
$test = _XMLGetAttrib("//CLASS[" & $i+1 & "]", "rdfAbout")
ConsoleWrite($test & @CRLF)
Next

With that, I can cycle through each CLASS, starting with its own attributes, and get the child nodes, attributes, and values in turn. Thanks for looking, at least, hopefully this will turn up on searches for help, at least.

meta:

XML , XPath , how to query xml , how to get xml attributes

Share this post


Link to post
Share on other sites

<data:sql-datasource xmlns:data="http://jfreereport.sourceforge.net/namespaces/datasources/sql">
  <data:config label-mapping="true"/>
  <data:connection>
    <data:driver>com.ibm.db2.jcc.DB2Driver</data:driver>
    <data:url>jdbc:db2://10.237.50.227:60012/EPM89DEV</data:url>
    <data:properties>
      <data:property name="user">db2inst1</data:property>
      <data:property name="password">insight</data:property>
      <data:property name="::pentaho-reporting::port">60012</data:property>
      <data:property name="::pentaho-reporting::name">EPM89DEV</data:property>
      <data:property name="::pentaho-reporting::database-name">EPM89DEV</data:property>
      <data:property name="::pentaho-reporting::hostname">10.237.50.227</data:property>
      <data:property name="::pentaho-reporting::database-type">DB2</data:property>
    </data:properties>
  </data:connection>
  <data:query name="Week_List">SELECT DISTINCT       
TRIM(CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25))) CONCAT ' ' CONCAT TRIM(CAST(  (SELECT MIN(DAY(CAST(C1.DATE AS DATE))) FROM ELM_STG.AIC_SAMPLE_DATA C1 WHERE   CAST(C1.WeekNumber AS CHAR(25)) =  CAST(T1.WeekNumber AS CHAR(25))) AS CHAR(25))) CONCAT ' to ' CONCAT TRIM(CAST(  (SELECT MAX(DAY(CAST(C1.DATE AS DATE))) FROM ELM_STG.AIC_SAMPLE_DATA C1 WHERE   CAST(C1.WeekNumber AS CHAR(25)) =  CAST(T1.WeekNumber AS CHAR(25))) AS CHAR(25))) AS &quot;WeekNo&quot;
FROM 
    ELM_STG.AIC_SAMPLE_DATA T1
    WHERE CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25))  = (${Month})
    AND
    TRIM(CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25))) CONCAT ' ' CONCAT TRIM(CAST(  (SELECT MIN(DAY(CAST(C1.DATE AS DATE))) FROM ELM_STG.AIC_SAMPLE_DATA C1 WHERE CAST(MONTHNAME(CAST(C1.DATE AS DATE)) AS CHAR(25))  = CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25)) ) AS CHAR(25))) CONCAT ' to ' CONCAT TRIM(CAST((SELECT MAX(DAY(CAST(C1.DATE AS DATE))) FROM ELM_STG.AIC_SAMPLE_DATA C1 WHERE CAST(MONTHNAME(CAST(C1.DATE AS DATE)) AS CHAR(25))  = CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25)) ) AS CHAR(25))) = (${Fortnight})
UNION
SELECT  DISTINCT CAST( '- ALL -' AS CHAR(25)) AS &quot;WeekNo&quot;
FROM 
    ELM_STG.AIC_SAMPLE_DATA T1
    ORDER BY &quot;WeekNo&quot; ASC
</data:query>

Hi,

PFA code snip for reference.

I want to retreive the SQL query i.e.

SELECT DISTINCT       
TRIM(CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25))) CONCAT ' ' CONCAT TRIM(CAST(  (SELECT MIN(DAY(CAST(C1.DATE AS DATE))) FROM ELM_STG.AIC_SAMPLE_DATA C1 WHERE   CAST(C1.WeekNumber AS CHAR(25)) =  CAST(T1.WeekNumber AS CHAR(25))) AS CHAR(25))) CONCAT ' to ' CONCAT TRIM(CAST(  (SELECT MAX(DAY(CAST(C1.DATE AS DATE))) FROM ELM_STG.AIC_SAMPLE_DATA C1 WHERE   CAST(C1.WeekNumber AS CHAR(25)) =  CAST(T1.WeekNumber AS CHAR(25))) AS CHAR(25))) AS &quot;WeekNo&quot;
FROM 
    ELM_STG.AIC_SAMPLE_DATA T1
    WHERE CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25))  = (${Month})
    AND
    TRIM(CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25))) CONCAT ' ' CONCAT TRIM(CAST(  (SELECT MIN(DAY(CAST(C1.DATE AS DATE))) FROM ELM_STG.AIC_SAMPLE_DATA C1 WHERE CAST(MONTHNAME(CAST(C1.DATE AS DATE)) AS CHAR(25))  = CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25)) ) AS CHAR(25))) CONCAT ' to ' CONCAT TRIM(CAST((SELECT MAX(DAY(CAST(C1.DATE AS DATE))) FROM ELM_STG.AIC_SAMPLE_DATA C1 WHERE CAST(MONTHNAME(CAST(C1.DATE AS DATE)) AS CHAR(25))  = CAST(MONTHNAME(CAST(T1.DATE AS DATE)) AS CHAR(25)) ) AS CHAR(25))) = (${Fortnight})
UNION
SELECT  DISTINCT CAST( '- ALL -' AS CHAR(25)) AS &quot;WeekNo&quot;
FROM 
    ELM_STG.AIC_SAMPLE_DATA T1
    ORDER BY &quot;WeekNo&quot; ASC

from the attached XML file. Any idea of how to proceed? I'm stuck up with this now and moreover, I found your post relevant to my issue but I could resolve by your stand.


Learning is Lifelong!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0