Apache公共jxpath XML解析

编程入门 行业动态 更新时间:2024-10-23 21:36:43
Apache公共jxpath XML解析 - XML URL为null(Apache common jxpath XML parse - XML URL is null)

这是我用来了解JXPath xml解析的示例代码,

import java.net.URL; import java.util.Iterator; import org.apache.commons.jxpath.Container; import org.apache.commons.jxpath.JXPathContext; import org.apache.commons.jxpath.xml.DocumentContainer; public class DocumentContainerTest { /** * @param args */ public static void main(String[] args) { //Get the URL the of XML document URL url = DocumentContainerTest.class.getClassLoader().getResource("student_class.xml"); //Construct document container from the URL to XML Container container = new DocumentContainer(url); JXPathContext context = JXPathContext.newContext(container); Iterator<?> subjects = context.iterate("/studentClass/subjects_list/subject"); while (subjects.hasNext()) { System.out.println(subjects.next()); } Iterator<?> stdNames = context.iterate("/studentClass/student_list/student/firstName"); while (stdNames.hasNext()) { System.out.println(stdNames.next()); } System.out.println(context.getValue("/studentClass/student_list/student[@id='1']/firstName")); } }

这是我使用的XML文件

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <studentClass> <name>MPC</name> <subjects_list> <subject>Maths</subject> <subject>Physics</subject> <subject>Chemistry</subject> </subjects_list> <student_list> <student id="1"> <age>2</age> <dob>2011-09-25T16:41:56.250+05:30</dob> <firstName>Sriram</firstName> <hobby>Painting</hobby> <lastName>Kasireddi</lastName> </student> <student id="2"> <age>26</age> <dob>2011-09-25T16:41:56.250+05:30</dob> <firstName>Sudhakar</firstName> <hobby>Coding</hobby> <lastName>Kasireddi</lastName> </student> </student_list> </studentClass>

我收到以下错误,

Exception in thread "main" org.apache.commons.jxpath.JXPathException: XML URL is null at org.apache.commons.jxpath.xml.DocumentContainer.<init>(DocumentContainer.java:106) at org.apache.commons.jxpath.xml.DocumentContainer.<init>(DocumentContainer.java:92) at org.apache.commons.jxpath.XMLDocumentContainer.<init>(XMLDocumentContainer.java:58) at jxpath_ex1.DocumentContainerTest.main(DocumentContainerTest.java:26) Java Result: 1

我添加了lib文件,commons-jxpath-1.3.jar,commons-beanutils-1.3.jar,apache-commons-logging.jar。

here is an example code which i have used to learn about JXPath xml parsing,

import java.net.URL; import java.util.Iterator; import org.apache.commons.jxpath.Container; import org.apache.commons.jxpath.JXPathContext; import org.apache.commons.jxpath.xml.DocumentContainer; public class DocumentContainerTest { /** * @param args */ public static void main(String[] args) { //Get the URL the of XML document URL url = DocumentContainerTest.class.getClassLoader().getResource("student_class.xml"); //Construct document container from the URL to XML Container container = new DocumentContainer(url); JXPathContext context = JXPathContext.newContext(container); Iterator<?> subjects = context.iterate("/studentClass/subjects_list/subject"); while (subjects.hasNext()) { System.out.println(subjects.next()); } Iterator<?> stdNames = context.iterate("/studentClass/student_list/student/firstName"); while (stdNames.hasNext()) { System.out.println(stdNames.next()); } System.out.println(context.getValue("/studentClass/student_list/student[@id='1']/firstName")); } }

and here is my XML File used

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <studentClass> <name>MPC</name> <subjects_list> <subject>Maths</subject> <subject>Physics</subject> <subject>Chemistry</subject> </subjects_list> <student_list> <student id="1"> <age>2</age> <dob>2011-09-25T16:41:56.250+05:30</dob> <firstName>Sriram</firstName> <hobby>Painting</hobby> <lastName>Kasireddi</lastName> </student> <student id="2"> <age>26</age> <dob>2011-09-25T16:41:56.250+05:30</dob> <firstName>Sudhakar</firstName> <hobby>Coding</hobby> <lastName>Kasireddi</lastName> </student> </student_list> </studentClass>

i am getting the below error,

Exception in thread "main" org.apache.commons.jxpath.JXPathException: XML URL is null at org.apache.commons.jxpath.xml.DocumentContainer.<init>(DocumentContainer.java:106) at org.apache.commons.jxpath.xml.DocumentContainer.<init>(DocumentContainer.java:92) at org.apache.commons.jxpath.XMLDocumentContainer.<init>(XMLDocumentContainer.java:58) at jxpath_ex1.DocumentContainerTest.main(DocumentContainerTest.java:26) Java Result: 1

i have added the lib files, commons-jxpath-1.3.jar, commons-beanutils-1.3.jar, apache-commons-logging.jar.

最满意答案

在定义网址已经尝试过如下,它的工作原理!

URL url = DocumentContainerTest.class.getResource("student_class.xml");

in defining the url have tried out like below and it works !

URL url = DocumentContainerTest.class.getResource("student_class.xml");

更多推荐

本文发布于:2023-08-06 04:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Apache   jxpath   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!