Java – VTD XML big data XPATHexpression does not use namespaces
•
Java
When running the namespace, I have a small test file If I delete namespaces, do I do something wrong with huge code?
Change to remove infeasible code
try { //without namespace works VTDGen vtdGen = new VTDGen(); vtdGen.parseFile("test.xml",false); VTDNav vtdNav = vtdGen.getNav(); AutoPilot autoPilot = new AutoPilot(vtdNav); autoPilot.selectXPath("//Receiver/Identifier"); autoPilot.evalXPath(); System.out.println("Stand===>" + vtdNav.getXPathStringVal() + " ===>"); } catch(indexoutofboundsexception e) { e.printStackTrace(); } try { //with namespace doesn't work VTDGen vtdGen = new VTDGen(); vtdGen.parseFile("test.xml",true); VTDNav vtdNav = vtdGen.getNav(); AutoPilot autoPilot = new AutoPilot(vtdNav); autoPilot.declareXPathNameSpace("x","http://test/namespaces/ssfgf"); autoPilot.selectXPath("//x:Receiver/Identifier"); int index = autoPilot.evalXPath(); System.out.println("Stand NS ===>" + vtdNav.toString(index) + " ===>"); } catch(indexoutofboundsexception e) { e.printStackTrace(); } try { //without namespace doesn't work VTDGenHuge vg = new VTDGenHuge(); vg.parseFile("test.xml",false); VTDNavHuge vn = vg.getNav(); AutoPilotHuge ap = new AutoPilotHuge(vn); ap.selectXPath("//Receiver/Identifier"); ap.evalXPath(); System.out.println("Huge ===> " + vn.toString(vn.getText()) + " ===>"); } catch(indexoutofboundsexception e) { e.printStackTrace(); } try { //with namespace doesn't work VTDGenHuge vg = new VTDGenHuge(); vg.parseFile("test.xml",true); VTDNavHuge vn = vg.getNav(); AutoPilotHuge ap = new AutoPilotHuge(vn); ap.declareXPathNameSpace("x","http://test/namespaces/ssfgf"); ap.selectXPath("//Receiver/Identifier"); ap.evalXPath(); System.out.println("Huge NS ===> " + vn.toString(vn.getText()) + " ===>"); } catch(indexoutofboundsexception e) { e.printStackTrace(); }
I got huge code and NS standard Java lang.indexoutofboundsexception
This is a sample XML. Unfortunately, the real XML cannot be displayed
<x:TestDocument xmlns:x="http://test/namespaces/ssfgf" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://test/namespaces/ssfgf"> <x:TestHeader> <x:Type></x:Type> <x:Scopes> <x:Scope> <x:Identifier>Context</x:Identifier> <x:Type>CaseId</x:Type> <x:InstanceIdentifier>case1</x:InstanceIdentifier> <x:Business> <x:Name>businessnane1</x:Name> </x:Business> </x:Scope> <x:Scope> <x:Identifier>Context</x:Identifier> <x:InstanceIdentifier>test1</x:InstanceIdentifier> <x:Type>TestId</x:Type> </x:Scope> <x:Scope> <x:Identifier>Context</x:Identifier> <x:InstanceIdentifier>other1</x:InstanceIdentifier> <x:Type>OtherId</x:Type> </x:Scope> </x:Scopes> <x:Receiver> <x:Identifier>testreceiverid</x:Identifier> </x:Receiver> <x:DocumentIdentification> <x:Type>type1</x:Type> <x:Identifier>id1</x:Identifier> <x:TypeVersion>version1</x:TypeVersion> </x:DocumentIdentification> </x:TestHeader> <x:TestBody attribute1="attribute1" attribute2="attribute2"> <TestingData>testingdata1</TestingData> </x:TestBody> </x:TestDocument>
Solution
You will get exceptions due to some problems: some use your code, others use the extended VTD - XML
First, you didn't use the second snippet of VTD to enable namespace awareness
You also do not have permission to set namespace binding
ap.declareXPathNameSpace("x","http://test/namespaces/ssfgf");
Third, gettext () returns - 1, which causes an exception
Finally, / / test will not match any nodes... So gettext() will definitely return - 1
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码