有如下一个XML文件:<?xml version="1.0" encoding="gb2312"?>
<operations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
<result isError="false" type="double">3.0</result>
</operations>
问题:在使用JDOM解析进行读取操作时,老是会出现一个异常:
org.jdom.JDOMException: The name "xsi:noNamespaceSchemaLocation" is not legal for JDOM/XML attributes: Attribute names cannot contain colons.: The name "xsi:noNamespaceSchemaLocation" is not legal for JDOM/XML attributes: Attribute names cannot contain colons.
它的意思我也知道,是说根节点operations中的属性不能有冒号,但xmlns:xsi是取别名,xsi:noNamespaceSchemaLocation是指向约束该xml文件的属性.这2项因为某种原因本人不能舍弃.请问有没有什么方法既能保留这2个属性也能正常解析此XML文件呢?
<operations xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="test.xsd">
<result isError="false" type="double">3.0</result>
</operations>
问题:在使用JDOM解析进行读取操作时,老是会出现一个异常:
org.jdom.JDOMException: The name "xsi:noNamespaceSchemaLocation" is not legal for JDOM/XML attributes: Attribute names cannot contain colons.: The name "xsi:noNamespaceSchemaLocation" is not legal for JDOM/XML attributes: Attribute names cannot contain colons.
它的意思我也知道,是说根节点operations中的属性不能有冒号,但xmlns:xsi是取别名,xsi:noNamespaceSchemaLocation是指向约束该xml文件的属性.这2项因为某种原因本人不能舍弃.请问有没有什么方法既能保留这2个属性也能正常解析此XML文件呢?
解决方案 »
- StringBuilder的问题
- 求助呀,一个关于java中时间相减的问题,求大神协助。
- 大企业比较看重程序员的什么技能
- 用ObjectInputStream类的readObject()方法时,怎么判断已经读完了,谢谢
- 急急急!!! 集合里面的元素怎么删除不了????
- 请教:Java中的系统属性与.properties文件的自动加载
- 如何使用句柄关闭窗体?
- java访问网络的问题
- 用java开发一个简单的数据库查许系统(实现用web浏览)
- 请教各位一个有关OutputStreamWriter类的问题,谢谢了!
- Java面试题每日五题(2010/03/01)及(2010/02/26)答案
- 最近公司不把我们当人看整天加班!我该不该走
是不是刚定义的namespace xsi不能在当前标签中使用?
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.List;import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;public class ParseXmlString {
public static void main(String[] args) {
String xmlstring = "<?xml version='1.0' encoding='utf-8'?>"
+ "<operations xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
+ "xsi:noNamespaceSchemaLocation='test.xsd'>"
+ "<result isError='false' type='double'>3.0</result>"
+ "</operations>"; parse(xmlstring);
} // 通过Jdom来实现解析
public static void parse(String xmlstring) {
SAXBuilder builder = new SAXBuilder();
try {
// 创建xml文档对象
//Document doc = builder.build(new StringReader(xmlstring));
Document doc = builder.build(new File("e:/11.xml"));
// 根节点
Element root = doc.getRootElement();
Element e = root.getChild("result");
System.out.println(e.getName()+":"+e.getText());
} catch (JDOMException e) {
e.printStackTrace();
}
}
}
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD