faqts : Computers : Programming : Languages : Tse : XML : Parser

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 2 people (50%) answered Yes
Recently 1 of 2 people (50%) answered Yes

Entry

TSE: XML: Parser: Tag: How to pretty print the attributes of a single XML tag? [HTML]

Dec 19th, 2004 14:08
Knud van Eeden,


----------------------------------------------------------------------
--- Knud van Eeden --- 19 December 2004 - 06:38 pm -------------------

TSE: XML: Parser: Tag: How to pretty print the attributes of a single 
XML tag? [HTML]

---

Informal Backus Naur Form:

 -(<)-[name]-[attribute]*-(>)-

---

Description:

 First you get a '<'

 Then you get a name

 Then you get possibly some attributes

 Then you get a '>'

---
---

attribute =

 -[name]-'='-'"'-[text]-'"'-

---

An attribute looks like this:

 First you get a name

 Then you get an equal sign '='

 Then you get double quotes

 Then you get some text

 Then you get double quotes

---
---

Note: in this simple example:

1. "double" quotes around the attribute values are supposed.

2. no nested double quotes (thus double quotes in double quotes) are 
supposed.

---
---

Method:

 You first search the begin tag

 While you find the attributes
  Extract them
  Do something
 Endwhile

---
---

Steps: Overview:

 1. -Create a single tag with 0 or more attributes

--- cut here: begin --------------------------------------------------

<test a="1"



    b="2"


  c="3123412341"


  >

--- cut here: end ----------------------------------------------------

 2. -Mark this single tag block

 3. -Run this macro

     1. It checks if a block is marked

     2. It then skips possible spaces in the begin of the block

     3. It then checks if a '<' is found

     4. It then gets the name of the tag

     5. While attributes found
         Do something
        Endwhile

  4. -When the macro is finished, it will have stored the result
      in another file in the following format:

--- cut here: begin --------------------------------------------------

<test
  a="1"
  b="2"
  c="3123412341"
>

--- cut here: end ----------------------------------------------------

  5. -The macro is the following

--- cut here: begin --------------------------------------------------

FORWARD INTEGER PROC FNBlockCheckMarkXmlAttributeB( STRING s1 )

FORWARD PROC Main()

// --- MAIN --- //

PROC Main()

 Message( FNBlockCheckMarkXmlAttributeB( "c:\temp\ddd.dok" ) ) // 
gives e.g. TRUE if zero or more attributes in a single tag can be found

END

<F12> Main()

// --- LIBRARY --- //

// library: block: check: mark: xml: attribute 
(filenamemacro=checblxa.s) [kn, ri, su, 19-12-2004 19:17:12]

INTEGER PROC FNBlockCheckMarkXmlAttributeB( STRING filenameS )

 // e.g. PROC Main()

 // e.g.  Message( FNBlockCheckMarkXmlAttributeB
( "c:\temp\ddd.dok" ) ) // gives e.g. TRUE if zero or more attributes 
in a single tag can be found

 // e.g. END

 // e.g.

 // e.g. <F12> Main()

 STRING nameS[255] = ""

 STRING attributenameS[255] = ""

 INTEGER x1 = 0

 INTEGER y1 = 0

 INTEGER x2 = 0

 INTEGER y2 = 0

 //

 IF NOT ( IsBlockInCurrFile() )

  Warn( "XML: Error: Please mark a block first" )

  RETURN( FALSE )

 ENDIF

 //

 GotoBlockBegin() // goto the beginning of the marked block

 //

 WHILE ( CurrChar() == Asc( " " ) OR CurrChar() == _AT_EOL_ OR CurrChar
() == _BEYOND_EOL_ ) // skip spaces and go pass end of line

  NextChar()

 ENDWHILE

 //

 IF NOT ( CurrChar() == Asc( "<" ) ) // check if found the '<' of 
begin tag

  Warn( "XML: Error: No begin tag '<' found in XML block" )

  RETURN( FALSE )

 ENDIF

 //

 NextChar() // current character processed, so skip it and goto next 
character

 //

 IF NOT LFind( "[a-zA-Z][a-zA-Z0-9]@\c", "ixl" ) // get name of begin 
tag and skip it

  Warn( "XML: Error: No begin tag name found in XML block" )

  RETURN( FALSE )

 ENDIF

 //

 nameS = GetFoundText() // get the found tag name

 //

 PushPosition()

 EditFile( filenameS )

 EndFile()

 InsertText( "<" + nameS, _INSERT_ )

 PopPosition()

 //

 WHILE NOT( CurrChar() == Asc( ">" ) ) // check if found the '>' of 
begin tag

  //

  PushBlock()

  //

  WHILE ( CurrChar() == Asc( " " ) OR CurrChar() == _AT_EOL_ OR 
CurrChar() == _BEYOND_EOL_ ) // skip spaces and go pass end of line

   NextChar()

  ENDWHILE

  //

  IF NOT LFind( "[a-zA-Z][a-zA-Z0-9]@\c", "ixl" ) // get name of 
current attribute and skip it

   Warn( "XML: Error: No attribute name found in XML block" )

   RETURN( FALSE )

  ENDIF

  //

  attributenameS = GetFoundText() // get the found attribute name

  //

  PushPosition()

  EditFile( filenameS )

  EndFile()

  CReturn()

  PopPosition()

  //

  PushPosition()

  EditFile( filenameS )

  EndFile()

  InsertText( " " + " " + attributenameS, _INSERT_ )

  PopPosition()

  //

  WHILE ( CurrChar() == Asc( " " ) OR CurrChar() == _AT_EOL_ OR 
CurrChar() == _BEYOND_EOL_ ) // skip spaces and go pass end of line

   NextChar()

  ENDWHILE

  //

  IF NOT ( CurrChar() == Asc( "=" ) ) // check if found the '=' of 
that attribute

   Warn( "XML: Error: No attribute '=' found in XML block" )

   RETURN( FALSE )

  ENDIF

  //

  NextChar() // current character processed, so skip it and goto next 
character

  //

  PushPosition()

  EditFile( filenameS )

  EndFile()

  InsertText( "=", _INSERT_ )

  PopPosition()

  //

  WHILE ( CurrChar() == Asc( " " ) OR CurrChar() == _AT_EOL_ OR 
CurrChar() == _BEYOND_EOL_ ) // skip spaces and go pass end of line

   NextChar()

  ENDWHILE

  //

  IF NOT ( CurrChar() == Asc( '"' ) ) // check if found the begin 
double quote '"' of current attribute

   Warn( "XML: Error: No attribute begin double quote found in XML 
block" )

   RETURN( FALSE )

  ENDIF

  //

  NextChar() // current character processed, so skip it and goto next 
character

  //

  PushPosition()

  EditFile( filenameS )

  EndFile()

  InsertText( '"', _INSERT_ )

  PopPosition()

  //

  x1 = CurrCol() // get the x-coordinate of the beginning of the text

  y1 = CurrLine() // get the y-coordinate of the beginning of the text

  //

  IF NOT LFind( '"', "ixl" ) // check if found attribute end double 
quote

   Warn( "XML: Error: No attribute end double quote found in XML 
block" )

   RETURN( FALSE )

  ENDIF

  //

  PrevChar() // be sure to goto the position before the closing double 
quote '"' of the current attribute

  //

  x2 = CurrCol() // get the x-coordinate of the end of the text

  y2 = CurrLine() // get the y-coordinate of the end of the text

  //

  IF NOT ( ( ABS ( x2 - x1 ) == 1 ) AND ( ABS ( y2 - y1 ) == 0 ) ) // 
if text of more than 1 character mark it, else do not mark (it says 
here if only 1 character difference, that is (x2-x1=1), and on the 
same line (that is (y2-y1=0), you have here no text between the tags, 
e.g. '<test></test>')

   //

   GotoColumn( x1 ) // goto coordinates of beginning of the text

   GotoLine( y1 )

   //

   MarkStream() // mark from the begin of the text

   //

   GotoColumn( x2 ) // goto coordinates of end of the text

   GotoLine( y2 )

   //

   MarkStream() // mark (and fix) to the end of the text

   //

  ELSE

   UnMarkBlock() // if no text unmark block (to indicate no text found)

  ENDIF

  //

  PushPosition()

  Copy()

  EditFile( filenameS )

  EndFile()

  Paste()

  PopPosition()

  //

  NextChar() // move to next character, which is the end closing 
quote '"'

  NextChar() // current character processed, so skip it and goto next 
character

  //

  PushPosition()

  EditFile( filenameS )

  EndFile()

  InsertText( '"', _INSERT_ )

  PopPosition()

  //

  PopBlock()

  //

  WHILE ( CurrChar() == Asc( " " ) OR CurrChar() == _AT_EOL_ OR 
CurrChar() == _BEYOND_EOL_ ) // skip spaces and go pass end of line

   NextChar()

  ENDWHILE

  //

 ENDWHILE

 //

 NextChar() // move to next character, which is the '>' of the begin 
tag

 //

 PushPosition()

 EditFile( filenameS )

 EndFile()

 CReturn()

 PopPosition()

 //

 PushPosition()

 EditFile( filenameS )

 EndFile()

 InsertText( ">", _INSERT_ )

 PopPosition()

 //

 RETURN( TRUE )

 //

END

--- cut here: end ----------------------------------------------------

---
---

Internet: see also:

---

TSE: XML: Parser: Link: Overview: Can you give me an overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/32677/fid/1734

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