StAX NullPointerException 在 xmlns=""
StAX NullPointerException at xmlns=""
Objective
获取有关此问题原因的建议,solution/workaround。
问题
当 运行 与 Whosebug Split 1GB Xml file using Java to split the XML file having xmlns="" as in W3C specification 几乎相同的程序时出现 NullPointerException。
XML
已通过 https://validator.w3.org/check 验证并确认格式正确。
<?xml version="1.0" encoding="UTF-8"?>
<EntityList>
<Entity xmlns="">
</Entity>
<Entity xmlns="">
</Entity>
</EntityList>
错误
JAXP: find factoryId =javax.xml.stream.XMLInputFactory
JAXP: loaded from fallback value: com.sun.xml.internal.stream.XMLInputFactoryImpl
JAXP: created new instance of class com.sun.xml.internal.stream.XMLInputFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: loaded from fallback value: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
ERROR: ''
Exception in thread "main" javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at stax.cursor.Demo.main(Demo.java:24)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.NamespaceMappings.pushNamespace(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToStream.startPrefixMapping(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.emitFirstTag(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.flush(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.startElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleStartElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
... 3 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.NamespaceMappings.pushNamespace(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToStream.startPrefixMapping(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.emitFirstTag(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.flush(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.startElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleStartElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at stax.cursor.Demo.main(Demo.java:24)
环境
在 DELL latitude E7440 Windows 10 Pro 64 位 Java 7 和 8
上测试
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
代码
package stax.cursor;
import java.io.File;
import java.io.FileReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
xsr.nextTag(); // Advance to statements element
long count = 1;
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT && xsr.getLocalName().toString().equals("Entity")) {
File file = new File("out/" + java.lang.Long.valueOf(count++) + ".xml");
t.transform(new StAXSource(xsr), new StreamResult(file));
}
}
}
仅供参考
<?xml version="1.0" encoding="UTF-8"?>
<EntityList>
<Entity xmlns="mailto:test.com">
</Entity>
<Entity>
</Entity>
</EntityList>
此 XML 按预期拆分为两个 XML 文件。
1.xml
<?xml version="1.0" encoding="UTF-8"?><Entity xmlns="mailto:test.com">
</Entity>
2.xml
<?xml version="1.0" encoding="UTF-8"?><Entity>
</Entity>
这是 JDK 中的错误。您应该在 http://bugs.java.com/
提交错误报告
作为解决方法,您可以尝试使用替代的 Stax 实现。
Objective
获取有关此问题原因的建议,solution/workaround。
问题
当 运行 与 Whosebug Split 1GB Xml file using Java to split the XML file having xmlns="" as in W3C specification 几乎相同的程序时出现 NullPointerException。
XML
已通过 https://validator.w3.org/check 验证并确认格式正确。
<?xml version="1.0" encoding="UTF-8"?>
<EntityList>
<Entity xmlns="">
</Entity>
<Entity xmlns="">
</Entity>
</EntityList>
错误
JAXP: find factoryId =javax.xml.stream.XMLInputFactory
JAXP: loaded from fallback value: com.sun.xml.internal.stream.XMLInputFactoryImpl
JAXP: created new instance of class com.sun.xml.internal.stream.XMLInputFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: loaded from fallback value: com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl using ClassLoader: null
ERROR: ''
Exception in thread "main" javax.xml.transform.TransformerException: java.lang.NullPointerException
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at stax.cursor.Demo.main(Demo.java:24)
Caused by: java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.NamespaceMappings.pushNamespace(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToStream.startPrefixMapping(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.emitFirstTag(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.flush(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.startElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleStartElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
... 3 more
---------
java.lang.NullPointerException
at com.sun.org.apache.xml.internal.serializer.NamespaceMappings.pushNamespace(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToStream.startPrefixMapping(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.emitFirstTag(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.flush(Unknown Source)
at com.sun.org.apache.xml.internal.serializer.ToUnknownStream.startElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.handleStartElement(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.bridge(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.StAXStream2SAX.parse(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(Unknown Source)
at stax.cursor.Demo.main(Demo.java:24)
环境
在 DELL latitude E7440 Windows 10 Pro 64 位 Java 7 和 8
上测试Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)
代码
package stax.cursor;
import java.io.File;
import java.io.FileReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stax.StAXSource;
import javax.xml.transform.stream.StreamResult;
public class Demo {
public static void main(String[] args) throws Exception {
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
xsr.nextTag(); // Advance to statements element
long count = 1;
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
while(xsr.nextTag() == XMLStreamConstants.START_ELEMENT && xsr.getLocalName().toString().equals("Entity")) {
File file = new File("out/" + java.lang.Long.valueOf(count++) + ".xml");
t.transform(new StAXSource(xsr), new StreamResult(file));
}
}
}
仅供参考
<?xml version="1.0" encoding="UTF-8"?>
<EntityList>
<Entity xmlns="mailto:test.com">
</Entity>
<Entity>
</Entity>
</EntityList>
此 XML 按预期拆分为两个 XML 文件。
1.xml
<?xml version="1.0" encoding="UTF-8"?><Entity xmlns="mailto:test.com">
</Entity>
2.xml
<?xml version="1.0" encoding="UTF-8"?><Entity>
</Entity>
这是 JDK 中的错误。您应该在 http://bugs.java.com/
提交错误报告作为解决方法,您可以尝试使用替代的 Stax 实现。