[Haskell-beginners] xml light problem or pebkac

Manfred Lotz manfred.lotz at arcor.de
Mon Jun 6 20:19:49 CEST 2011


Hi all,
I'm a bit lost. I'm having a problem with xml light but as a Haskell
newbie it could be of course that the problem is sitting between my
keyboard and chair.


I tried hard to create a minimal example.

Here is a minimal xmltest.hs:

<-----------------------------snip--------------------------------> 
module Main where

import System.Environment.UTF8
import qualified System.IO.UTF8 as U
import System.IO
import Text.XML.Light


data CTest = CTest {
    ctName :: String
  , ctLocation :: String
  } deriving (Show,Read,Eq,Ord)

getXmlContent :: Handle -> IO CTest 
getXmlContent inh = do
    xml <- U.hGetContents inh
    let content = parseXMLDoc xml
    case content of
      Just c -> do let name = case findChild (unqual "name") c of
                         Nothing -> "<unknown>"
                         Just n' -> strContent n' 
                   let path = case findChild (unqual "location") c of
                         Nothing -> "<unknown>"
                         Just path' -> case findAttr (unqual "path")
path' of Nothing -> "<unknown>"
                                      Just p -> p
                   return CTest { ctName=name, ctLocation=path}
      _  -> fail "not expected" 

  
readXmlFile :: FilePath -> IO CTest
readXmlFile f = do
  inh <- U.openBinaryFile f ReadMode
  xml <- getXmlContent inh 
  hClose inh
  return xml

doSomething :: Show a => a -> IO ()  
doSomething xml =
  print xml
  
main :: IO ()
main = do
  args <- getArgs
  xml <- readXmlFile $ head args 
  doSomething xml
<-----------------------------snap--------------------------------> 


Here is the test.xml I use:

<-----------------------------snip--------------------------------> 
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE entry SYSTEM 'test.dtd'>
<entry>
  <name>some name</name>
  <description>
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
	bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla 
  </description>
  <location path='/info/somepath'/>
</entry>
<-----------------------------snap--------------------------------> 

Here the output of the program:
CTest {ctName = "some name", ctLocation = "<unknown>"}


which is wrong.


If I delete just one of the 'bla bla' lines then the output of the
program is:
CTest {ctName = "some name", ctLocation = "/info/somepath"}

which is correct.


Question: What is my error?



-- 
Manfred





More information about the Beginners mailing list