-2

I am writing a code to read attribute name and value from a XML String but my nodes are represented as for example hi:collection so the compiler is taking it as a URL and throwing the error No URL found for prefix:hi

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;


   import javax.xml.parsers.ParserConfigurationException;
   import javax.xml.xpath.XPathExpressionException;

     import com.ximpleware.*;

    import org.xml.sax.SAXException;
public class A20 {

public static void main(String[] args) throws IOException, XPathExpressionException, ParserConfigurationException, SAXException, XPathParseException, XPathEvalException, NavException{

    URL a=new URL("URL");
    HttpURLConnection b=(HttpURLConnection) a.openConnection();
    b.setRequestMethod("GET");
    b.setRequestProperty("Accept", "application/xml");
BufferedReader c=new BufferedReader(new InputStreamReader(b.getInputStream()));
StringBuilder sb=new StringBuilder();

String out,out1 = null;
while((out=c.readLine())!=null)
 {sb.append(out);
 out1=sb.toString();
  System.out.println( out1);   }
  c.close();
  b.disconnect();

   byte[] bytes = out1.getBytes("UTF-8");
   VTDGen vg = new VTDGen();
   vg.setDoc(bytes);
VTDNav vn = vg.getNav();
AutoPilot ap =  new AutoPilot(vn);

ap.selectXPath("//hi:collection/@name");// I am getting error here 
int i=0;
while( (i=ap.evalXPath())!=-1){
     System.out.println(" item name is ===>"+vn.toString(i+1)); 
}
}

}

codehacker
  • 381
  • 5
  • 15

1 Answers1

0

The reason you are getting that error is because you have not declared the binding between the prefix, hi in this case, and its corresponding URL, which is required and indispensable.

The fix is to call declareNamespaceURL() method of AutoPilot before selectXPath.

Another suggestion: you can use VTDGen's parseHTTPURL method to directly read from an URL address... thus saving you the effort of URL handling...

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30