Well, after trying it for more than four hours, it's making me feel
stupid...again...
I thought I could ask around for help again.
I have the usual code for iterating through the resources (copied below),
then I clean the String from the resource as commented in previous messages
in this same thread, and the 'initEquipmentTypes' method gets it, where I
want to manipulate the produced String and create the DOM document. The
String reaches the method, I can print it and I see everything is there, but
for some reason the parse fails! After parsing it, the document only has the
root element I hard coded, but nothing from the String I got from the DB :-(
I thought it could be something with the encoding, I browsed the list
archives and trying at least half dozen solutions to that problem with no
luck either. This is absolutely frustrating...hope someone could point me in
the right direction. Thanks.
Josema.
-----------------------------------------------------------------------
<code-snippet>
/**
* Inititializes a DOM Document to list the available Equipment types
*
*/
public Document initEquipmentTypes() {
DOMImplementation impl;
DocumentBuilder builder;
try {
// Find the implementation
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware( false );
factory.setValidating ( false );
builder = factory.newDocumentBuilder();
impl = builder.getDOMImplementation();
}
catch (Exception ex) {
throw new RuntimeException("Failed to initialize DOM factory. Root cause:
\n" + ex);
}
String strDocument="<EquipmentTypes>";
//find the Equipment Types available in the database
String localSubCol = "/Recording/Equipment/EquipmentType";
try {
XindiceManager xi = new XindiceManager();
//get the resources from the DB, this works ok
strDocument += xi.find(localSubCol, "//EquipmentType");
strDocument += "</EquipmentTypes>";
Document document = builder.parse(new
ByteArrayInputStream(strDocument.getBytes()));
return( document );
}
catch (Exception e) {
System.err.println("Cannot get results from the DB: " + e.getMessage());
}
}
/*##### code for retrieving resources from the database #####*/
/**
* Cleans the data from the resource, so it can conform to a simple Node
* method suggested by Stefan Lischke
*/
private String clean (String tmp) {
String ausgabe = "";
//first, remove the XML Declaration
tmp = removeDeclaration(tmp);
int pos = tmp.indexOf("xmlns:src=");
while(pos != -1) {
//bis pos ausgeben
ausgabe= ausgabe + tmp.substring(0,pos);
//System.out.println(ausgabe+":"+pos);
tmp=tmp.substring(pos);
//ende des schruts finden
int nameendpos = tmp.indexOf(">");
//System.out.println(">"+nameendpos);
//set newtmp-text
tmp=tmp.substring(nameendpos);
pos = tmp.indexOf("xmlns:src=");
}
ausgabe = ausgabe + tmp;
return(ausgabe);
}
/**
* Removes the XML Declaration from a resource
*
*/
private String removeDeclaration(String data) {
return (data.substring(data.indexOf("?>")+2));
}
/**
* Search for resources in the DB.
*/
public String find(String subCol, String xpath) throws Exception {
Collection col = null;
try {
String data = "";
Class c = Class.forName(driver);
Database database = (Database) c.newInstance();
DatabaseManager.registerDatabase(database);
String localCol = rootCollection + subCol;
col = DatabaseManager.getCollection(localCol);
XPathQueryService service =
(XPathQueryService) col.getService("XPathQueryService", "1.0");
ResourceSet resultSet = service.query(xpath);
ResourceIterator results = resultSet.getIterator();
while (results.hasMoreResources()) {
Resource res = results.nextResource();
//System.out.println((String)res.getContent());
data = data + clean((String)res.getContent());
//I have also tried the line below, with no luck either :-(
//data = new String(((String) res.getContent()).getBytes(), "UTF8");
data = clean(data);
}
return (data);
}
catch (XMLDBException e) {
System.err.println("XML:DB - Find Exception occured " + e.errorCode);
throw e;
}
finally {
if (col != null) {
col.close();
}
}
}
</code-snippet>
----- Original Message -----
From: "Carsten Ziegert" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Tuesday, November 12, 2002 12:15 PM
Subject: Re: how to get rid of the xml declaration on results?
> I'm doing exactly the same with JDOM (http:/www.jdom.org) instead
> of DOM, but it doesnt' seem to be the fastest solution ...
>
> Carsten
>
>
> Am Dienstag, 12.11.02, um 03:24 Uhr (Europe/Berlin) schrieb Josema