XMLDOM - parsing xml with namespaces

jensthomsenjensthomsen Member Posts: 173
I'm reading a XML with XMLDOM3.0. The XML looks like the image attached. What would be the right way to browse through the XML to get the information from say the 'JGTIdent'? My problem is, that the namespace 'cec' can be different from file to file (CEC, CBC etc) - can I somehow suppress the namespace when parcing the file?

Comments

  • mdPartnerNLmdPartnerNL Member Posts: 802
    Yes, im using XML 6.0 and from this forum build this function.
    OpenExistingXml(VAR vPtFilePath : Text[250];VAR vPxmlDoc : Automation "'Microsoft XML, v6.0'.DOMDocument60";PbRemoveNameSpaces : Boolean
    LfXml.OPEN(vPtFilePath);
    LfXml.CREATEINSTREAM(LisStream);
    IF ISCLEAR(vPxmlDoc) THEN BEGIN
      CREATE(vPxmlDoc);
    END;
    vPxmlDoc.load(LisStream);
    LfXml.CLOSE;
    CLEAR(LfXml);
    
    IF (STRPOS(LOWERCASE(vPtFilePath), '.nons.') > 0) THEN BEGIN
      PbRemoveNameSpaces := TRUE;
    END;
    
    IF (PbRemoveNameSpaces) THEN BEGIN
      //http://www.mibuso.com/forum/viewtopic.php?f=5&t=16627&hilit=transformNodeToObject
      LrCompanyInfo.Picture.CREATEOUTSTREAM(LosStylesheet);
      LrCompanyInfo.Picture.CREATEINSTREAM(LisStylesheet);
      LosStylesheet.WRITETEXT('<?xml version="1.0" encoding="UTF-8"?>');
      LosStylesheet.WRITETEXT('<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">');
      LosStylesheet.WRITETEXT('<xsl:output method="xml" encoding="UTF-8" />');
      LosStylesheet.WRITETEXT('<xsl:template match="/">');
      LosStylesheet.WRITETEXT('<xsl:copy>');
      LosStylesheet.WRITETEXT('<xsl:apply-templates />');
      LosStylesheet.WRITETEXT('</xsl:copy>');
      LosStylesheet.WRITETEXT('</xsl:template>');
      LosStylesheet.WRITETEXT('<xsl:template match="*">');
      LosStylesheet.WRITETEXT('<xsl:element name="{local-name()}">');
      LosStylesheet.WRITETEXT('<xsl:apply-templates select="@* | node()" />');
      LosStylesheet.WRITETEXT('</xsl:element>');
      LosStylesheet.WRITETEXT('</xsl:template>');
      LosStylesheet.WRITETEXT('<xsl:template match="@*">');
      LosStylesheet.WRITETEXT('<xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute>');
      LosStylesheet.WRITETEXT('</xsl:template>');
      LosStylesheet.WRITETEXT('<xsl:template match="text() | processing-instruction() | comment()">');
      LosStylesheet.WRITETEXT('<xsl:copy />');
      LosStylesheet.WRITETEXT('</xsl:template>');
      LosStylesheet.WRITETEXT('</xsl:stylesheet>');
      IF ISCLEAR(LxmlStylesheet) THEN BEGIN
        CREATE(LxmlStylesheet);
      END;
      LxmlStylesheet.load(LisStylesheet);
    
      IF ISCLEAR(LxmlToDoc) THEN BEGIN
        CREATE(LxmlToDoc);
      END;
    
      vPxmlDoc.transformNodeToObject(LxmlStylesheet,LxmlToDoc);
    
      LtFilePath := GcuRxCompatible.GetTempFileNamePathOnClient('xml');
    
      LxmlToDoc.save(LtFilePath);
      CLEAR(LxmlToDoc);
      CLEAR(LxmlStylesheet);
      CLEAR(vPxmlDoc);
    
      LfXml.OPEN(LtFilePath);
      LfXml.CREATEINSTREAM(LisStream);
      CREATE(vPxmlDoc);
      vPxmlDoc.load(LisStream);
      LfXml.CLOSE;
      CLEAR(LfXml);
    
    END;
    
  • ta5ta5 Member Posts: 1,164
    IMO best way to find values in a normal sized xml file is using xpath. Have a look here for a primer on xpath:
    http://www.w3schools.com/xpath/xpath_syntax.asp

    Hope this helps
    Thomas
Sign In or Register to comment.