我从DBpedia SPARQL端点获得以下XML:
<?xml version="1.0"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
<variable name="onto"/>
</head>
<results>
<result>
<binding name="onto">
<uri>http://www.w3.org/2002/07/owl#Thing</uri>
</binding>
</result>
<result>
<binding name="onto">
<uri>http://www.w3.org/2002/07/owl#Thing</uri>
</binding>
</result>
<result>
<binding name="onto">
<uri>http://www.w3.org/2003/01/geo/wgs84_pos#SpatialThing</uri>
</binding>
</result>
</results>
</sparql>
Run Code Online (Sandbox Code Playgroud)
当我用Jena阅读它时,我尝试扫描它:
ResultSet r = ResultSetFactory.fromXML( xmlCode );
while ( r.hasNext() ) {
QuerySolution soln = r.next()
...
}
Run Code Online (Sandbox Code Playgroud)
我总是得到以下异常:
com.hp.hpl.jena.sparql.resultset.ResultSetException: End of document while processing solution
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.staxError(XMLInputStAX.java:503)
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.getOneSolution(XMLInputStAX.java:413)
at com.hp.hpl.jena.sparql.resultset.XMLInputStAX$ResultSetStAX.hasNext(XMLInputStAX.java:218)
Run Code Online (Sandbox Code Playgroud)
这是耶拿虫还是其他什么?
编辑:为了完整性,我报告一个关于此错误的线程:
当bio2rdf邮件列表提供一些帮助时,我们能够更多地跟踪错误.
Arq 2.8.3工作正常Arq 2.8.4失败并带有描述的错误Arq 2.8.5失败并带有描述的错误所以我想我会保留Art 2.8.3进行测试.如果我可以帮助更多地调试此错误,请告诉我.
奇怪的.错误来自STaX解析器 - 所有基本级别的XML解析都转包给Woodstox.几乎如果它的读取速度快于输入到达并且看到EOF而不是阻止新输入.我尝试读取整个流然后解析读取的字节,它工作正常.为什么2.8.3应该是不同的,目前还不清楚它可能只是时机.
解决方法:使用以下命令切换XML解析器:
ARQ.getContext().setTrue(ARQ.useSAX);
在调用QueryExecutionFactory.sparqlService …
我想在我们的代码库中重用一些现有的代码来接受XMLStreamReader我的应用程序将所需的数据作为w3c文档.
以下示例是最小测试用例:
public static void main(String[] args) throws Exception {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.newDocument();
Element rootElement = doc.createElement("Groups");
doc.appendChild(rootElement);
Element group = doc.createElement("Group");
group.setTextContent("Wibble");
rootElement.appendChild(group);
DOMSource source = new DOMSource(doc);
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(source);
reader.nextTag();
System.out.println("NextTag:" + reader.getName());
}
Run Code Online (Sandbox Code Playgroud)
预期的输出应该是这样的:NextTag:Groups但是抛出以下内容:
Exception in thread "main" javax.xml.stream.XMLStreamException: java.net.MalformedURLException
at com.sun.xml.stream.XMLReaderImpl.setInputSource(XMLReaderImpl.java:196)
at com.sun.xml.stream.XMLReaderImpl.<init>(XMLReaderImpl.java:179)
at com.sun.xml.stream.ZephyrParserFactory.createXMLStreamReader(ZephyrParserFactory.java:139)
at Main.main(Main.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.MalformedURLException …Run Code Online (Sandbox Code Playgroud) 我接管了一个使用stax解析器的应用程序的维护,将许多记录的XML文件分解为单个记录以供进一步处理.为此目的使用这种类型的解析器在我看来是过度的,但我没有写它.
该应用程序现在遇到如下数据:
<name><![CDATA[A & B]]></name>
Run Code Online (Sandbox Code Playgroud)
我们当前的解析器返回begin'name'标记的事件.下一个事件是值为'A&B'的字符事件.
从Sun的网页上我发现了这个:
报告CDATA事件Streaming XML Parser中实现的javax.xml.stream.XMLStreamReader不报告CDATA事件.如果您的应用程序需要接收此类事件,请配置XMLInputFactory以设置以下特定于实现的report-cdata-event属性:
XMLInputFactory factory = XMLInptuFactory.newInstance();
factory.setProperty("report-cdata-event", Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)
我们使用的解析器不支持'report-cdata-event'属性.
我想找到一个报告这样一个事件的解析器,这样我就不必检查需要由CDATA构造保护的字符的每一段文本.
更新:
发布后我浏览了一些相关的问题,并提到'isCoalescing'属性; 记录它被设置为FALSE.
在下面的代码示例中,我使用STaX解析器来解析一段XML.如果我通过它运行xml10,它按预期工作.xml11字符串(除了xml版本之外是相同的) - 它会抛出NullPointerException.我在使用JDK 1.6的Mac上运行它.
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Stack;
/**
*/
public class StaxSucks {
static String xml10 ="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"+
"<anElement/>";
static String xml11 ="<?xml version=\"1.1\" encoding=\"utf-8\" ?>\n"+
"<anElement/>";
static void parse(InputStream is) throws Exception{
final XMLInputFactory factory = XMLInputFactory.newInstance();
factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
final XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(is);
Stack<QName> XMLDEPTH = new Stack<QName>();
int eventType = xmlStreamReader.next();
while(eventType != XMLStreamConstants.END_DOCUMENT){
if(XMLStreamConstants.START_ELEMENT == eventType){
QName eventName = xmlStreamReader.getName();
XMLDEPTH.push(eventName); …Run Code Online (Sandbox Code Playgroud) 我需要在服务器上解析一个巨大的xml文件并将其发送给客户端.
我想按需进行解析 - 意思是,首先只解析并显示父节点,当客户端点击父节点时 - 向服务器发送一个请求,告诉我选择哪个父节点,然后再到解析并发送它的孩子(再次,不是整个子树,而只是父母).
我想过使用STAX解析器,但是当涉及到父子关系时,我不明白如何使用它.如何告诉解析器不要继续使用下一个作为子节点的START-ELEMENT,而是跳到其级别中的下一个父节点?而且 - 有没有办法回到ITERATOR实施?选择一位父母并看到其子女后,我可以回去看看以前的父母吗?
我真的很感激任何建议!
谢谢.
我想使用JAXB为以下XSD架构解析数据 http://www.uniprot.org/support/docs/uniprot.xsd.
典型的XML就是这样的:http://www.uniprot.org/uniprot/Q8NEJ9.xml
我的类是使用以下方法生成的:
xjc http://www.uniprot.org/support/docs/uniprot.xsd
Run Code Online (Sandbox Code Playgroud)
我无法获得一个JAXB unmarshaller来解析这些数据.
xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
XMLEventReader rx=xmlInputFactory.createXMLEventReader(in);
final QName uEntry=new QName("http://uniprot.org/uniprot","entry");
while(rx.hasNext())
{
XMLEvent evt=rx.peek();
if(!(evt.isStartElement() && evt.asStartElement().getName().equals(uEntry)))
{
rx.next();
continue;
}
JAXBElement<Entry> jaxbElement=uniprotUnmarshaller.unmarshal(rx, Entry.class);
Entry entry= jaxbElement.getValue();
(...)
}
Run Code Online (Sandbox Code Playgroud)
每个"条目"实例都保持为空.当一个条目被封送到stderr时,我得到类似的东西:
<ns2:entry xmlns:ns2="http://uniprot.org/uniprot" dataset="Swiss-Prot" created="2011-06-28+01:00" modified="2011-09-21+01:00" version="20"/>
Run Code Online (Sandbox Code Playgroud)
我认为这是因为xjc忽略了命名空间.它产生:
@XmlRootElement(name = "entry")
public class Entry {
Run Code Online (Sandbox Code Playgroud)
代替 (?)
@XmlRootElement(name = "entry",namespace="http://uniprot.org/uniprot")
public class Entry {
Run Code Online (Sandbox Code Playgroud)
我怎样才能解决这个问题 ?
我有一个非常庞大的XML结构.我想通过解组一个元素然后应用业务逻辑来更新此XML的各个部分.
我能够将子元素解组成POJO.我想在Java中对这个POJO进行更改,然后在同一位置将其更新回XML.
这在JAXB中可行吗?或者使用JAXB + StAX的组合.
示例结构:
<folder id="c5718b36-bab1-4c08-8f75-8e2f9aee42c5" name="Folder-1">
<description> folder Desc</description>
<createdBy>User2</createdBy>
<hidden>false</hidden>
<file id="4f2efb42-0604-4878-9e1e-ae90d66fb836" name="File-1">
<description>file desc</description>
<createdBy>User1</createdBy>
<hidden>false</hidden>
</file>
</folder>
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我能够将'file'元素解组为POJO.我想对此POJO进行更改,然后在XML文件中的正确位置更新它.
我怎么能做到这一点?
请帮我.谢谢.
我有以下代码:
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader xmlStreamReader = inputFactory.createXMLStreamReader(inStream);
this.encoding = xmlStreamReader.getEncoding();
Run Code Online (Sandbox Code Playgroud)
...
这段代码在JBoss和Websphere中都可以正常运行,但是在特定的JBoss中会引发以下异常:
java.lang.ClassCastException: com.ctc.wstx.stax.WstxInputFactory cannot be cast to javax.xml.stream.XMLInputFactory
at javax.xml.stream.XMLInputFactory.newInstance(XMLInputFactory.java:136)
at es.gema.core.shared.dim.data.XFacturaE.detectVersion(XFacturaE.java:115)
at es.gema.core.shared.dim.data.XFacturaE.<init>(XFacturaE.java:67)
at es.gema.core.shared.dim.bc.InvoiceLoader.readXMLInvoice(InvoiceLoader.java:544)
at es.gema.core.shared.dim.bc.InvoiceLoader.loadInvoiceFACE(InvoiceLoader.java:137)
at es.gema.core.expenses.fac.bc.InvoiceServicesBC.execute(InvoiceServicesBC.java:127)
at es.gema.core.expenses.fac.bc.InvoiceServicesBC.execute(InvoiceServicesBC.java:92)
Run Code Online (Sandbox Code Playgroud)
检查WstxInputFactory我看到它扩展了XMLInputFactory2而不是XMLInputFactory。
在这种情况下,推荐的方法是什么?在不使用工厂的情况下创建WstxInputFactory的实例,或配置Java容器以返回扩展XMLInputFactory的解析器。
我们有一个场景,我们需要在小块中拆分大小超过10GB的大型xml文件.每个块应包含100或200个元素.示例xml
<Employees>
<Employee id="1">
<age>29</age>
<name>Pankaj</name>
<gender>Male</gender>
<role>Java Developer</role>
</Employee>
<Employee id="3">
<age>35</age>
<name>Lisa</name>
<gender>Female</gender>
<role>CEO</role>
</Employee>
<Employee id="3">
<age>40</age>
<name>Tom</name>
<gender>Male</gender>
<role>Manager</role>
</Employee>
<Employee id="3">
<age>25</age>
<name>Meghna</name>
<gender>Female</gender>
<role>Manager</role>
</Employee>
<Employee id="3">
<age>29</age>
<name>Pankaj</name>
<gender>Male</gender>
<role>Java Developer</role>
</Employee>
<Employee id="3">
<age>35</age>
<name>Lisa</name>
<gender>Female</gender>
<role>CEO</role>
</Employee>
<Employee id="3">
<age>40</age>
<name>Tom</name>
<gender>Male</gender>
<role>Manager</role>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
我有Stax解析器代码,它将文件分成小块.但是每个文件只包含一个完整的Employee元素,我需要<Employee>在单个文件中包含100或200个或更多元素.这是我的java代码
public static void main(String[] s) throws Exception{
String prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+"\n";
String suffix = "\n</Employees>\n";
int count=0;
try {
int i=0; …Run Code Online (Sandbox Code Playgroud) 我想xmlns从以下xml字符串中删除属性。我已经编写了一个java程序,但是不确定它是否需要执行此处的操作。
如何删除xmlns属性并获取修改后的xml字符串?
输入XML字符串:
<?xml version="1.0" encoding="UTF-8"?>
<Payment xmlns="http://api.com/schema/store/1.0">
<Store>abc</Store>
</Payment>
Run Code Online (Sandbox Code Playgroud)
预期的XML输出字符串:
<?xml version="1.0" encoding="UTF-8"?>
<Payment>
<Store>abc</Store>
</Payment>
Run Code Online (Sandbox Code Playgroud)
Java类:
public class XPathUtils {
public static void main(String[] args) {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Payment xmlns=\"http://api.com/schema/store/1.0\"><Store>abc</Store></Payment>";
String afterNsRemoval = removeNameSpace(xml);
System.out.println("afterNsRemoval = " + afterNsRemoval);
}
public static String removeNameSpace(String xml) {
try {
System.out.println("before xml = " + xml);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new StringReader(xml));
Document …Run Code Online (Sandbox Code Playgroud)