У меня есть проект, в котором я пытаюсь выполнить запросы к файлам XML, используя in.jlibs.jlibs-xmldog.2.2.1.
Используемая мной XPath:
(//server[@id=123])[1]
, а запрашиваемый файл выглядит следующим образом
<servers>
<server id="123" name="servername3">
<cpu_count>3</cpu_count>
<ram_gb>3</ram_gb>
<mac>aabbccaabbcc</mac>
<domain>test.internal</domain>
<ip>192.168.1.20</ip>
</server>
<server id="124" name="servername4">
<cpu_count>4</cpu_count>
<ram_gb>4</ram_gb>
<mac>aabbccaabbcc</mac>
<domain>test.internal</domain>
<ip>192.168.1.20</ip>
</server>
<server id="125" name="servername5">
<cpu_count>5</cpu_count>
<ram_gb>5</ram_gb>
<mac>aabbccaabbcc</mac>
<domain>test.internal</domain>
<ip>192.168.1.20</ip>
</server>
</servers>
Как видите, уже есть только один результат, который возвращается внутренним оператором xpath, но у меня есть несколько причин, по которым мне нужно выполнить запрос следующим образом. В любом случае, такое же поведение происходит, если внутреннее утверждение возвращает больше результатов. Я протестировал xpath онлайн, и он, кажется, действителен.
Код:
public static void main(String[] args) throws Exception{
final String filePath = "C:\\Users\\NCsorba\\Desktop\\xml\\sample_11433_entries.xml";
final String xPathExpression = "(//server[@id=123])[1]";
DefaultNamespaceContext namespaceContext = new DefaultNamespaceContext();
namespaceContext.declarePrefix("xsd", Namespaces.URI_XSD);
XMLDog xmlDog = new XMLDog(namespaceContext);
Expression compiledXPath = xmlDog.addXPath(xPathExpression);
XPathResults xPathResults = xmlDog.sniff(new InputSource(filePath)); // exception thrown
// System.out.println(xPathResults.getResult(compiledXPath));
}
Вот трассировка стека:
Exception in thread "main" java.lang.NegativeArraySizeException
at jlibs.core.util.LongTreeMap$Values.toArray(LongTreeMap.java:534)
at java.util.ArrayList.<init>(ArrayList.java:178)
at jlibs.xml.sax.dog.expr.nodset.PathEvaluation.finished(PathExpression.java:218)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.fireFinished(LocationEvaluation.java:215)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.finished(LocationEvaluation.java:208)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.resultPrepared(LocationEvaluation.java:190)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.expired(LocationEvaluation.java:176)
at jlibs.xml.sax.dog.path.EventID.expireList(EventID.java:244)
at jlibs.xml.sax.dog.path.EventID.expire(EventID.java:230)
at jlibs.xml.sax.dog.path.EventID.pop(EventID.java:322)
at jlibs.xml.sax.dog.sniff.Event.firePop(Event.java:266)
at jlibs.xml.sax.dog.sniff.Event.pop(Event.java:712)
at jlibs.xml.sax.dog.sniff.Event.onEndDocument(Event.java:587)
at jlibs.xml.sax.dog.sniff.SAXHandler.endDocument(SAXHandler.java:75)
at org.apache.xerces.parsers.AbstractSAXParser.endDocument(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.endEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.endEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:189)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:174)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:205)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:212)
at com.hp.oo.content.actions.xml.Main.main(Main.java:24)
--------------- linked to ------------------
javax.xml.xpath.XPathException: java.lang.NegativeArraySizeException
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:192)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:174)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:205)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:212)
at com.hp.oo.content.actions.xml.Main.main(Main.java:24)
Caused by: java.lang.NegativeArraySizeException
at jlibs.core.util.LongTreeMap$Values.toArray(LongTreeMap.java:534)
at java.util.ArrayList.<init>(ArrayList.java:178)
at jlibs.xml.sax.dog.expr.nodset.PathEvaluation.finished(PathExpression.java:218)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.fireFinished(LocationEvaluation.java:215)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.finished(LocationEvaluation.java:208)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.resultPrepared(LocationEvaluation.java:190)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.expired(LocationEvaluation.java:176)
at jlibs.xml.sax.dog.path.EventID.expireList(EventID.java:244)
at jlibs.xml.sax.dog.path.EventID.expire(EventID.java:230)
at jlibs.xml.sax.dog.path.EventID.pop(EventID.java:322)
at jlibs.xml.sax.dog.sniff.Event.firePop(Event.java:266)
at jlibs.xml.sax.dog.sniff.Event.pop(Event.java:712)
at jlibs.xml.sax.dog.sniff.Event.onEndDocument(Event.java:587)
at jlibs.xml.sax.dog.sniff.SAXHandler.endDocument(SAXHandler.java:75)
at org.apache.xerces.parsers.AbstractSAXParser.endDocument(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.endEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.endEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:189)
... 4 more
--------------- linked to ------------------
javax.xml.xpath.XPathException: javax.xml.xpath.XPathException: java.lang.NegativeArraySizeException
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:176)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:205)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:212)
at com.hp.oo.content.actions.xml.Main.main(Main.java:24)
Caused by: javax.xml.xpath.XPathException: java.lang.NegativeArraySizeException
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:192)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:174)
... 3 more
Caused by: java.lang.NegativeArraySizeException
at jlibs.core.util.LongTreeMap$Values.toArray(LongTreeMap.java:534)
at java.util.ArrayList.<init>(ArrayList.java:178)
at jlibs.xml.sax.dog.expr.nodset.PathEvaluation.finished(PathExpression.java:218)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.fireFinished(LocationEvaluation.java:215)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.finished(LocationEvaluation.java:208)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.resultPrepared(LocationEvaluation.java:190)
at jlibs.xml.sax.dog.expr.nodset.LocationEvaluation.expired(LocationEvaluation.java:176)
at jlibs.xml.sax.dog.path.EventID.expireList(EventID.java:244)
at jlibs.xml.sax.dog.path.EventID.expire(EventID.java:230)
at jlibs.xml.sax.dog.path.EventID.pop(EventID.java:322)
at jlibs.xml.sax.dog.sniff.Event.firePop(Event.java:266)
at jlibs.xml.sax.dog.sniff.Event.pop(Event.java:712)
at jlibs.xml.sax.dog.sniff.Event.onEndDocument(Event.java:587)
at jlibs.xml.sax.dog.sniff.SAXHandler.endDocument(SAXHandler.java:75)
at org.apache.xerces.parsers.AbstractSAXParser.endDocument(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl.endEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityManager.endEntity(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source)
at org.apache.xerces.impl.XMLEntityScanner.skipSpaces(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$TrailingMiscDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at jlibs.xml.sax.dog.XMLDog.sniff(XMLDog.java:189)
... 4 more
Я буду признателен любая помощь! Спасибо!