F# read an XML document

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hongqi162/article/details/3758052
  1. #light 
  2. open System 
  3. open System.IO 
  4. open System.Xml 
  5. let out(x) = printfn "%A"
  6. let xmldoc = new XmlDocument() 
  7. xmldoc.Load("c://test.xml"
  8. let xnl = xmldoc.SelectSingleNode("Root").ChildNodes 
  9. for n in 0 .. xnl.Count-1 do 
  10. let xe = xnl.Item(n) 
  11. out(xe.InnerText) 
  12. System.Console.ReadKey(true) > ignore
展开阅读全文

xmldocument为null

11-30

报错提示为:Exception in thread "main" java.lang.IllegalArgumentException: Adding text to an XML document must not be nullrn以下为源码,还建立了一个users.xml文件rnpublic class UserDao rn public static final String USER_XML = "users";rn private Document ud; rn rn public UserDao() rn ud = XMLUtil.getUserDocument();rn rn rn private void write()rn XMLUtil.write2XML(ud, USER_XML);rn rn public void add(User user)rn Element e = ud.getRootElement().addElement("user");rn e.addElement("username").addText(user.getUsername());rn e.addElement("password").addText(user.getPassword());rn e.addElement("nickname").addText(user.getNickname());rn write();rn rnrnrnpublic class XMLUtil rn private static Document userDocument;rn rn public static Document getUserDocument()rn //如果document存在直接返回rn if(userDocument!=null) return userDocument;rn //如果不存在就创建对象rn try rn SAXReader reader = new SAXReader();rn userDocument = reader.read(XMLUtil.class.getClassLoader().getResourceAsStream("xml/users.xml"));rn return userDocument;rn catch (DocumentException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn return null;rn rn rn public static void write2XML(Document d,String name)rn XMLWriter out = null;rn try rn String path = XMLUtil.class.getClassLoader().getResource("xml/"+name+".xml").getPath();rn path = path.replace("bin","src");rn out = new XMLWriter(new FileWriter(path),OutputFormat.createPrettyPrint());rn out.write(d);rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn finallyrn try rn if(out!=null) out.close();rn catch (IOException e) rn // TODO Auto-generated catch blockrn e.printStackTrace();rn rn rn rnrnpublic class TestUser rn private static UserDao ud = new UserDao();rn public static void main(String[] args) rn testAdd();rn rn rn public static void testAdd()rn User u = new User();rn u.setNickname("张三");rn u.setPassword("123");rn u.setNickname("zhangsan");rn ud.add(u);rn rnrnrnpublic class User rn private String username;rn private String password;rn private String nickname;rn public String getUsername() rn return username;rn rn public void setUsername(String username) rn this.username = username;rn rn public String getPassword() rn return password;rn rn public void setPassword(String password) rn this.password = password;rn rn public String getNickname() rn return nickname;rn rn public void setNickname(String nickname) rn this.nickname = nickname;rn rnrnrn这个是users.xml的代码rnrnrn 问答

Parsing XML document using xalan

06-25

I have developped some java-xml applicaitons under jdk1.4. Now I would like to replace the XML parser with Xalan, however there is an error as below. Notice that the parsing works fine, but when I try to save the document into a file, the error occur. And it work perfect under jaxp reference implementation from sun. Any suggestions?rnrn/*code*/rnFileWriter out = new FileWriter(outputFile);rn TransformerFactory tFactory = TransformerFactory.newInstance();rn Transformer transformer = tFactory.newTransformer();rn transformer.transform(new DOMSource(myDoc), new StreamResult(outputFile));rn/*code*/rnrn/*error*/rnjava.lang.NoSuchMethodErrorrn at org.apache.xpath.DOM2Helper.getNamespaceOfNode(DOM2Helper.java:348)rn at org.apache.xml.utils.TreeWalker.startNode(TreeWalker.java:368)rn at org.apache.xml.utils.TreeWalker.traverse(TreeWalker.java:192)rn at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:322)rn at crmloyalty.ConfigParser.saveXMLFile(ConfigParser.java:215)rn at crmloyalty.ConfigParser.setAllParameters(ConfigParser.java:201)rn at crmloyalty.ModelForecastDataServlet.doPost(ModelForecastDataServlet.java:107)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)rn at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)rn at org.apache.tomcat.core.Handler.service(Handler.java:287)rn at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)rn at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:812)rn at org.apache.tomcat.core.ContextManager.service(ContextManager.java:758)rn at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)rn at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)rn at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)rn at java.lang.Thread.run(Thread.java:484)rnrn/*error*/ 论坛

没有更多推荐了,返回首页