简体   繁体   中英

clojure.zip/branch? got a NullPointerException on a xml node

I have the following xml file

<?xml version="1.0" encoding="utf-8"?>
<library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="library.xsd">
  <SingleBook>
  </SingleBook>
</library>

And my clojure (1.5.1) program crashes and get a NullPointerException while calling (zip/branch?)

(let [f (.toFile (sh/getPath "lib.xml"))
    dom (clojure.zip/xml-zip (clojure.xml/parse f))
    top (clojure.zip/children dom)
    ]
    (prn (first top))
    (prn (clojure.zip/branch? (first top)))
)

I am new to clojure.zip and clojure.xml. Can anyone tell me why it crashes? Thanks.

When you get the children, you get the a seq of the content of the node, not the zipper itself. You will navigate from the top node and can keep using your zipper by clojure.zip/down like this:

(clojure.zip/branch? (clojure.zip/down dom))

Check clojuredocs.org about xml-zip for more info!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM