You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-users@xml.apache.org by Josema Alonso <al...@aafunky.com> on 2002/11/12 02:09:06 UTC

how to get rid of the xml declaration on results?

Hi, all.

When I run a query against one of my collections I get something like this:
<?xml version="1.0"?>
<EquipmentType id="mic" xmlns:src="http://xml.apache.org/xindice/Query"
src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic">
 <Description lang="en">Microphone</Description>
 <Description lang="es">Micrófono</Description>
</EquipmentType>

<?xml version="1.0"?>
<EquipmentType id="player" xmlns:src="http://xml.apache.org/xindice/Query"
src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="player">
 <Description lang="en">Player</Description>
 <Description lang="es">Reproductor</Description>
</EquipmentType>

<?xml version="1.0"?>
<EquipmentType id="preamp" xmlns:src="http://xml.apache.org/xindice/Query"
src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="preamp">
 <Description lang="en">Preamplifier</Description>
 <Description lang="es">Preamplificador</Description>
</EquipmentType>

More or less is what I expected, but I'd prefer to have a root element named
'EquipmentTypes' and I would need to get rid of the xml declaration in every
element. Is it possible?

Thanks.


Re: how to get rid of the xml declaration on results?

Posted by Murray Altheim <m....@open.ac.uk>.
Josema Alonso wrote:

> Hi, all.
> 
> When I run a query against one of my collections I get something like this:
> <?xml version="1.0"?>
> <EquipmentType id="mic" xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic">
>  <Description lang="en">Microphone</Description>
>  <Description lang="es">Microno</Description>
> </EquipmentType>
> 
> <?xml version="1.0"?>
> <EquipmentType id="player" xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="player">
>  <Description lang="en">Player</Description>
>  <Description lang="es">Reproductor</Description>
> </EquipmentType>
> 
> <?xml version="1.0"?>
> <EquipmentType id="preamp" xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="preamp">
>  <Description lang="en">Preamplifier</Description>
>  <Description lang="es">Preamplificador</Description>
> </EquipmentType>
> 
> More or less is what I expected, but I'd prefer to have a root element named
> 'EquipmentTypes' and I would need to get rid of the xml declaration in every
> element. Is it possible?


If you're treating this as a DOM Document, you can iterate over

over the Nodes in it, grabbing only the ProcessingInstructions:

    Document doc = getDocument();
    NodeIterator ni = DocumentTraversal.createNodeIterator(doc,
        NodeFilter.SHOW_PROCESSING_INSTRUCTION,true);
    Node n = doc;
    ProcessingInstruction pi = null;
    while ( n != null ) {
        pi = (ProcessingInstruction)ni.nextNode();
        if ( pi.getTarget().equals("xml") ) doc.removeNode(pi);
    }

[I just wrote this off the top of my head, so it might not work directly]

Since these are simply Nodes, you can remove them from the Document
upon encountering them in the Iterator.

As for having a single root, I don't quite understand the context
under which you could receive multiple roots as separate Elements,
so I'll leave the answer to that to others. If they are truly from
separate Documents, you can always create a new Document and use
importNode(Node,true).


Murray

......................................................................
Murray Altheim                  <http://kmi.open.ac.uk/people/murray/>
Knowledge Media Institute
The Open University, Milton Keynes, Bucks, MK7 6AA, UK

            If you're the first person in a new territory,
            you're likely to get shot at.
                                                     -- ma


Re: how to get rid of the xml declaration on results?

Posted by Evan Borysko <ev...@yahoo.com>.
I found this an easy to handle this. This code uses the DOM4J api to  
read the result tree and insert it into a new DOM result tree.

Document doc, newdoc;
          DocumentFactory df = DocumentFactory.getInstance();
          newdoc = df.createDocument(df.createElement("Results"));

          XPathQueryService service =
             (XPathQueryService) col.getService("XPathQueryService",  
"1.0");
          ResourceSet resultSet = service.query(xpath);
          ResourceIterator results = resultSet.getIterator();
          while (results.hasMoreResources()) {
             XMLResource res = (XMLResource)results.nextResource();
             DOMReader xmlReader = new DOMReader();
             doc = xmlReader.read(((org.w3c.dom.Document)  
res.getContentAsDOM()));
			Element elm = doc.getRootElement();
			newdoc.getRootElement().add(elm);
             System.out.println(doc.getRootElement().getName());

          }

It's clean and quick, but I sure there must be a better way to handle  
these types of document fragments.

Cheers,
Evan

--------------------------------------
Evan Borysko
evanborysko@yahoo.com

"The programmer, like the poet, works only slightly removed from pure  
thought-stuff. He builds his castles in the air, from air, creating by  
exertion of the imagination."
	--Frederick Brooks, Jr.


On Monday, November 11, 2002, at 09:24 PM, Josema Alonso wrote:

> Hey, just a quick followup.
>
> Since this is giving so much problems, what if:
> 1.- I get the content as text and not as DOM
> 2.- I apply the suggested functions so I can clean it up
> 3.- I load it into a DOM document with something like:
> private Document load(String s) {
>   DocumentBuilder builder = factory.newDocumentBuilder();
>   return builder.parse(new StringBufferInputStream(s));
> }
>
> I guess this is not a too clean approach, but it seems this time  
> Xindice is
> taking me there...any better ideas?
>
> I need some sleep now...
>
>
> ----- Original Message -----
> From: "Josema Alonso" <al...@aafunky.com>
> To: <xi...@xml.apache.org>
> Sent: Tuesday, November 12, 2002 2:54 AM
> Subject: Re: how to get rid of the xml declaration on results?
>
>
>> Hello,
>>
>>> I asked this some days ago too, but there was no real solution  
>>> besides
>>> hacking code of Xindice.
>> I see...ughhh...
>>
>>> The <EquipmentTypes /> Tag, u have to set by yourself, cause u are
>> getting
>>> a resultset.
>> Ok, I already did it.
>>
>>> the declaration can be killed by
>>> ----------------------------------------------
>>> data= data+xml.substring(xml.indexOf("?>")+2);
>>> -----------------------------------------------
>>> for the
>>>
>>> "xmlns:src="http://xml.apache.org/xindice/Query"
>>> src:col="/db/xmtrader/Recording/Equipment/EquipmentType"  
>>> src:key="mic"
>>>
>>> stuff i found a really bad solution, it "overreads" just that stuff  
>>> and
>>> gives the string back to u without it.
>>> ....
>> Thanks, I'll apply it, too.
>>
>>> hope that helps
>> Sure it does. Thanks a lot.
>>
>> Btw, when I get the resources list I would like to add it to a  
>> document.
>> This will be a document fragment, the method should return a DOM Node  
>> and
>> this one will be after that added to the root element I already  
>> created.
> I'm
>> trying the usual iterating through the ResourceSet and creating the
> fragment
>> in the method, but it is giving me an eception, something like 'DOM005
> Wrong
>> document'. I do not know what's happening, any ideas? (code attached
> below).
>>
>> Thanks a lot :-)
>>
>> ----------------
>> /**
>>  * Search for documents in the DB. If not found, return null.
>>  * The rootCollection is initialized in the constructor
>>  * I'm trying to return the fragment in xmldoc but it gives the  
>> exception
>> :-(
>>  */
>> public Node find(String subCol, String xpath) throws Exception {
>>  Collection col = null;
>>  Node xmldoc = null;
>>  try {
>>   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((((XMLResource)res).getContentAsDOM().getFirstChild( 
> )));
>>    if(xmldoc!=null) {
>>     Node childNode =  
>> ((XMLResource)res).getContentAsDOM().getFirstChild();
>>     xmldoc.appendChild(childNode);
>>    }
>>    else {
>>     xmldoc = ((XMLResource)res).getContentAsDOM().getFirstChild();
>>    }
>>   }
>>   return (xmldoc);
>>  }
>>  catch (XMLDBException e) {
>>   System.err.println("XML:DB - Find Exception occured " +  
>> e.errorCode);
>>   throw e;
>>  }
>>  finally {
>>   if (col != null) {
>>    col.close();
>>   }
>>  }
>> }
>>
>>
>> I'd like to invoke this method from this one:
>> /**
>>  * Inititializes a DOM Node to list the available Equipment types
>>  *
>>  */
>> public void initEquipmentTypes() {
>>  DOMImplementation impl;
>>  try {
>>   // Find the implementation
>>   DocumentBuilderFactory factory =  
>> DocumentBuilderFactory.newInstance();
>>   factory.setNamespaceAware(true);
>>   factory.setValidating ( false );
>>   DocumentBuilder builder = factory.newDocumentBuilder();
>>   impl = builder.getDOMImplementation();
>>  }
>>  catch (Exception ex) {
>>   throw new RuntimeException("Failed to initialize DOM factory. Root
> cause:
>> \n" + ex);
>>  }
>>
>>  //Create the node for the root, 'Class'
>>  Document doc = impl.createDocument( null, "EquipmentTypes", null);
>>  equipmentTypes = doc.getDocumentElement();
>>
>>  //find the Equipment Types available in the database
>>  String localSubCol =  "/Recording/Equipment/EquipmentType";
>>  try {
>>   //Here is where I create an instance and call the method above
>>   //result is not null but it gives an exception
>>   XindiceManager xi = new XindiceManager();
>>   Node result = xi.find(localSubCol, "//EquipmentType");
>>
>>   if (result!=null) {
>>    equipmentTypes.appendChild(result);
>>   }
>>  }
>>  catch (Exception e) {
>>   System.err.println("Cannot get results from the DB: " +  
>> e.getMessage());
>>  }
>> }
>>
>>
>
>


Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
Quick followup...

Ooops...I reset my computer and it's now working as a charm :-)
Something went wrong in my Websphere...

Andrea, thank you very very much for your time and help.


----- Original Message -----
From: "Josema Alonso" <al...@aafunky.com>
To: <xi...@xml.apache.org>
Sent: Thursday, November 14, 2002 4:47 PM
Subject: Re: how to get rid of the xml declaration on results?


> Dear Andrea,
>
> Thanks for your help. I already solved it but I'm not satisfied with the
> solution. I had to get the results as String and clean them first.
>
> Btw, I've tested your function, and I already had one very very similar,
and
> I still have a DOM exception in this line with both:
> Element resElement =
> ((Document)((XMLResource)r).getContentAsDOM()).getDocumentElement();
>
> The exception said DOM003: Wrong Document.
>
> So, I'll use my solution from now on, 'til I could correct that error or
> find a better one.
>
> Thanks again :-)
>
>
>
> ----- Original Message -----
> From: "Andrea Broglia" <an...@dstc.edu.au>
> To: <xi...@xml.apache.org>
> Sent: Thursday, November 14, 2002 5:18 AM
> Subject: RE: how to get rid of the xml declaration on results?
>
>
> > Hi Josema,
> >   I don't know if you're still struggling with the query results to
strip
> > away the xml declaration and the added attributes, but this may help
you:
> >
> >   public Document combineXPathResults(String resultRootElement,
> >                                       ResourceSet result)
> >     throws XMLDBException
> >   {
> >     // Create a new Document and a root element that will have as
children
> >     // all the results
> >     Document resultDoc = new DocumentImpl();
> >     Element root = resultDoc.createElement(resultRootElement);
> >     resultDoc.appendChild(root);
> >
> >     // Iterate the xpath results and add each of them to the main
Document
> >     ResourceIterator iterator = result.getIterator();
> >     while(iterator.hasMoreResources()) {
> >       Resource r = iterator.nextResource();
> >
> >       Element resElement = ((Document)
> >         ((XMLResource)r).getContentAsDOM()).getDocumentElement();
> >
> >       // Remove unwanted attributes
> >       resElement.removeAttribute("src:col");
> >       resElement.removeAttribute("src:key");
> >       resElement.removeAttribute("xmlns:src");
> >
> >       // Add this result to the root element
> >       Element importedElement =
(Element)resultDoc.importNode(resElement,
> > true);
> >       root.appendChild(importedElement);
> >     }
> >
> >     return resultDoc;
> >   }
> >
> > Once you have the "global" result Document object you can serialize it
> > (using the XMSerialize class), or apply an xslt to it (using Xalan), or
> > anything you need to.
> >
> > Cheers,
> >   Andrea
> >
> >
> > > -----Original Message-----
> > > From: Josema Alonso [mailto:alonso@aafunky.com]
> > > Sent: Thursday, 14 November 2002 3:02 AM
> > > To: xindice-users@xml.apache.org
> > > Subject: Re: how to get rid of the xml declaration on results?
> > >
> > >
> > > Hey,
> > >
> > > Remember the weird characters? The function below, which is really
> stupid,
> > > makes the parse work, just by rewriting the characters from the XML
> string
> > > with the same ones. So, I'm now sure it is an encoding issue, and I do
> not
> > > know how to fix other way. Any ideas on how to retrieve the
> > > resources in the
> > > right encoding?
> > >
> > > Thanks.
> > >
> > > -------------
> > > /**
> > >  * Removes those nasty characters from the resource
> > >  *
> > >  */
> > > private String removeNasty(String data) throws
> > > UnsupportedEncodingException
> > > {
> > >  char oldChar = new String("\n").charAt(0);
> > >  char newChar = new String("\n").charAt(0);
> > >  data = data.replace(oldChar, newChar);
> > >
> > >  newChar = new String("\t").charAt(0);
> > >  oldChar = new String("\t").charAt(0);
> > >  data = data.replace(oldChar, newChar);
> > >
> > >  return data;
> > > }
> >
>


Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
Dear Andrea,

Thanks for your help. I already solved it but I'm not satisfied with the
solution. I had to get the results as String and clean them first.

Btw, I've tested your function, and I already had one very very similar, and
I still have a DOM exception in this line with both:
Element resElement =
((Document)((XMLResource)r).getContentAsDOM()).getDocumentElement();

The exception said DOM003: Wrong Document.

So, I'll use my solution from now on, 'til I could correct that error or
find a better one.

Thanks again :-)



----- Original Message -----
From: "Andrea Broglia" <an...@dstc.edu.au>
To: <xi...@xml.apache.org>
Sent: Thursday, November 14, 2002 5:18 AM
Subject: RE: how to get rid of the xml declaration on results?


> Hi Josema,
>   I don't know if you're still struggling with the query results to strip
> away the xml declaration and the added attributes, but this may help you:
>
>   public Document combineXPathResults(String resultRootElement,
>                                       ResourceSet result)
>     throws XMLDBException
>   {
>     // Create a new Document and a root element that will have as children
>     // all the results
>     Document resultDoc = new DocumentImpl();
>     Element root = resultDoc.createElement(resultRootElement);
>     resultDoc.appendChild(root);
>
>     // Iterate the xpath results and add each of them to the main Document
>     ResourceIterator iterator = result.getIterator();
>     while(iterator.hasMoreResources()) {
>       Resource r = iterator.nextResource();
>
>       Element resElement = ((Document)
>         ((XMLResource)r).getContentAsDOM()).getDocumentElement();
>
>       // Remove unwanted attributes
>       resElement.removeAttribute("src:col");
>       resElement.removeAttribute("src:key");
>       resElement.removeAttribute("xmlns:src");
>
>       // Add this result to the root element
>       Element importedElement = (Element)resultDoc.importNode(resElement,
> true);
>       root.appendChild(importedElement);
>     }
>
>     return resultDoc;
>   }
>
> Once you have the "global" result Document object you can serialize it
> (using the XMSerialize class), or apply an xslt to it (using Xalan), or
> anything you need to.
>
> Cheers,
>   Andrea
>
>
> > -----Original Message-----
> > From: Josema Alonso [mailto:alonso@aafunky.com]
> > Sent: Thursday, 14 November 2002 3:02 AM
> > To: xindice-users@xml.apache.org
> > Subject: Re: how to get rid of the xml declaration on results?
> >
> >
> > Hey,
> >
> > Remember the weird characters? The function below, which is really
stupid,
> > makes the parse work, just by rewriting the characters from the XML
string
> > with the same ones. So, I'm now sure it is an encoding issue, and I do
not
> > know how to fix other way. Any ideas on how to retrieve the
> > resources in the
> > right encoding?
> >
> > Thanks.
> >
> > -------------
> > /**
> >  * Removes those nasty characters from the resource
> >  *
> >  */
> > private String removeNasty(String data) throws
> > UnsupportedEncodingException
> > {
> >  char oldChar = new String("\n").charAt(0);
> >  char newChar = new String("\n").charAt(0);
> >  data = data.replace(oldChar, newChar);
> >
> >  newChar = new String("\t").charAt(0);
> >  oldChar = new String("\t").charAt(0);
> >  data = data.replace(oldChar, newChar);
> >
> >  return data;
> > }
>


RE: how to get rid of the xml declaration on results?

Posted by Andrea Broglia <an...@dstc.edu.au>.
Hi Josema,
  I don't know if you're still struggling with the query results to strip
away the xml declaration and the added attributes, but this may help you:

  public Document combineXPathResults(String resultRootElement,
                                      ResourceSet result)
    throws XMLDBException
  {
    // Create a new Document and a root element that will have as children
    // all the results
    Document resultDoc = new DocumentImpl();
    Element root = resultDoc.createElement(resultRootElement);
    resultDoc.appendChild(root);

    // Iterate the xpath results and add each of them to the main Document
    ResourceIterator iterator = result.getIterator();
    while(iterator.hasMoreResources()) {
      Resource r = iterator.nextResource();

      Element resElement = ((Document)
        ((XMLResource)r).getContentAsDOM()).getDocumentElement();

      // Remove unwanted attributes
      resElement.removeAttribute("src:col");
      resElement.removeAttribute("src:key");
      resElement.removeAttribute("xmlns:src");

      // Add this result to the root element
      Element importedElement = (Element)resultDoc.importNode(resElement,
true);
      root.appendChild(importedElement);
    }

    return resultDoc;
  }

Once you have the "global" result Document object you can serialize it
(using the XMSerialize class), or apply an xslt to it (using Xalan), or
anything you need to.

Cheers,
  Andrea


> -----Original Message-----
> From: Josema Alonso [mailto:alonso@aafunky.com]
> Sent: Thursday, 14 November 2002 3:02 AM
> To: xindice-users@xml.apache.org
> Subject: Re: how to get rid of the xml declaration on results?
>
>
> Hey,
>
> Remember the weird characters? The function below, which is really stupid,
> makes the parse work, just by rewriting the characters from the XML string
> with the same ones. So, I'm now sure it is an encoding issue, and I do not
> know how to fix other way. Any ideas on how to retrieve the
> resources in the
> right encoding?
>
> Thanks.
>
> -------------
> /**
>  * Removes those nasty characters from the resource
>  *
>  */
> private String removeNasty(String data) throws
> UnsupportedEncodingException
> {
>  char oldChar = new String("\n").charAt(0);
>  char newChar = new String("\n").charAt(0);
>  data = data.replace(oldChar, newChar);
>
>  newChar = new String("\t").charAt(0);
>  oldChar = new String("\t").charAt(0);
>  data = data.replace(oldChar, newChar);
>
>  return data;
> }


Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
Hey,

Remember the weird characters? The function below, which is really stupid,
makes the parse work, just by rewriting the characters from the XML string
with the same ones. So, I'm now sure it is an encoding issue, and I do not
know how to fix other way. Any ideas on how to retrieve the resources in the
right encoding?

Thanks.

-------------
/**
 * Removes those nasty characters from the resource
 *
 */
private String removeNasty(String data) throws UnsupportedEncodingException
{
 char oldChar = new String("\n").charAt(0);
 char newChar = new String("\n").charAt(0);
 data = data.replace(oldChar, newChar);

 newChar = new String("\t").charAt(0);
 oldChar = new String("\t").charAt(0);
 data = data.replace(oldChar, newChar);

 return data;
}


RE: how to get rid of the xml declaration on results?

Posted by Andrea Broglia <an...@dstc.edu.au>.
Hi Josema,

> I am. I'm downloading it today. Btw, if you could make this, I
> guess Xindice
> can export documents in that format? I mean, do you know what I must to do
> in order to have Xindice returning the correct encoding?

If you want to export a Resource to a file with a specified encoding,
probably the easiest way is the following:

import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;

...

public void exportResource(Resource res, OutputStream outStream)
  throws IOException
{
  OutputFormat outputFormat = new OutputFormat();
  outputFormat.setIndenting(true);
  outputFormat.setIndent(4);
  outputFormat.setEncoding("ISO-8859-1"); // or UTF-8, UTF-16, ...
  XMLSerializer ser = new XMLSerializer(outStream, outputFormat);
  ser.serialize((Document) ((XMLResource)res).getContentAsDOM());
}


I hope this helps,
  Andrea


RE: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
Hello,

>The reason is that Xindice stores the documents in UTF-8.
>So when you import a document in whatever encoding (e.g. ISO-8859-1) it
will
>be converted in UTF-8.
>Thus when you export (or display) such document from Xindice (using either
>the Xindice command line tool or a graphical interface) the default output
>will be in UTF-8 and the document declaration will simply be
><?xml version="1.0"?>
>since the default XML encoding is "UTF-8".
Ok, I understand this. And now, what can I do to retrieve the document in
the enconding of my choice?

>If you're interested, yesterday I have added a new configuration option to
>XMLdbGUI to set a different encoding (e.g. ISO-8859-1) when exporting
>documents. This is available from the CVS only, by now.
I am. I'm downloading it today. Btw, if you could make this, I guess Xindice
can export documents in that format? I mean, do you know what I must to do
in order to have Xindice returning the correct encoding?

Best,
	Josema.


RE: how to get rid of the xml declaration on results?

Posted by Andrea Broglia <an...@dstc.edu.au>.
Hi Josema,

> Ok, I added one of the addressbook documents to a collection
> using XMLdbGUI.
> I set the encoding of the document to 'ISO-8859-1' before adding
> it. It was
> successfully except for the encoding. The encoding now in the DB
> is missing.
> I also tried setting the encoding to 'UTF-8' and it's missing too.


The reason is that Xindice stores the documents in UTF-8.
So when you import a document in whatever encoding (e.g. ISO-8859-1) it will
be converted in UTF-8.
Thus when you export (or display) such document from Xindice (using either
the Xindice command line tool or a graphical interface) the default output
will be in UTF-8 and the document declaration will simply be
<?xml version="1.0"?>
since the default XML encoding is "UTF-8".

If you're interested, yesterday I have added a new configuration option to
XMLdbGUI to set a different encoding (e.g. ISO-8859-1) when exporting
documents. This is available from the CVS only, by now.

Regards,
  Andrea


Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
> Okay, you don't need worry about the encoding once
> it's in the db.
Ok.

> Also, check this XMLdbGUI tool to be sure it can
> handle your encoding. Not all tools provide proper
> support of  encodings. ( I personally use Attrezzo
> plugin for the Eclipse IDE to browse and edit
> documents in Xindice)
I'll do and I'll try to insert some documents programatically tomorrow, too.

> When the document is retrive try calling :
> Resource res = results.nextResource();
> System.out.println((String) res.getContent());
>
> You should see a print out of each item in the result
> with the <?xml?> line. Check the encoding of on this
> line. If it is set to something other than UTF or
> ISO-8859, you may have to do some work to keep you
> encoding it sync.
Nope. The line is there but without the encoding parameter...sighhh...

> Also the class I referred to was called
> XindiceQueryResultWrapper. If pass the ResourceSet to
> the constructor and then call setResultTag then
> buildResultSet methods you can use one of the getter
> method to get a complete DOM Document of your query
> results or a string contain the xml.
I see. I'll try it again.

> if this all fails, you could alwas use windows
> character mapper to determine what the hex vale of
> those characters are a write a regular expression to
> remove them from your document.
Currently I tried making just a simple String.replace(old, new), and it
worked. But this is not the solution I wanted. I'm afraid this is working
today and maybe not in the future...

> I will give the issue more thought tonight, and see if
> any other solutions come to mind.
Thank you very much. I'll post tomorrow about my new problems or solutions
if any.

Best.


Re: how to get rid of the xml declaration on results?

Posted by Evan Borysko <ev...@yahoo.com>.
Okay, you don't need worry about the encoding once
it's in the db. 

Also, check this XMLdbGUI tool to be sure it can
handle your encoding. Not all tools provide proper
support of  encodings. ( I personally use Attrezzo
plugin for the Eclipse IDE to browse and edit
documents in Xindice)

When the document is retrive try calling :

Resource res = results.nextResource();
System.out.println((String) res.getContent());

You should see a print out of each item in the result
with the <?xml?> line. Check the encoding of on this
line. If it is set to something other than UTF or
ISO-8859, you may have to do some work to keep you
encoding it sync.

Also the class I referred to was called
XindiceQueryResultWrapper. If pass the ResourceSet to
the constructor and then call setResultTag then
buildResultSet methods you can use one of the getter
method to get a complete DOM Document of your query
results or a string contain the xml.

if this all fails, you could alwas use windows
character mapper to determine what the hex vale of
those characters are a write a regular expression to
remove them from your document.

I will give the issue more thought tonight, and see if
any other solutions come to mind.

Cheers,
Evan
--- Josema Alonso <al...@aafunky.com> wrote:
> > Not to nudge too much into the inner working fo
> you
> > project, but it seems too me like you have a
> character
> > encoding issue. I've had these types of issue
> using
> > other languages. Usually they steam from an into
> put
> > source that is not encoding the file properly for
> OS
> > platform.
> I think it should be something like that. I'm
> developing on Windows XP
> Professional. Maybe my problems is cause I'm Spanish
> and running the locale
> version of the OS?
> 
> > My first question would are you running this
> project
> > on multiple platforms? (Linux, Solaris, NT, etc.)
> > If so if make your code has proper checks on the
> files
> > encoding. These squares look to me be like
> carriage
> > returns.
> I'm 99.9% sure these are carriage returns and tabs.
> 
> > I would also check to see if all your developers
> IDE
> > and editors are set to save is the same encoding
> > format.(ISO-8859-1, UTF-8, etc.) Try insert a new
> xml
> > document from your system into the DB and then
> > retrieve it. It help to see if it's just your
> machine
> > out of sync with the DB's encoding.
> I'm trying right now...
> Ok, I added one of the addressbook documents to a
> collection using XMLdbGUI.
> I set the encoding of the document to 'ISO-8859-1'
> before adding it. It was
> successfully except for the encoding. The encoding
> now in the DB is missing.
> I also tried setting the encoding to 'UTF-8' and
> it's missing too.
> 
> I guess this is a problem...how to fix it? May I add
> the documents using
> Java code and may I specify then the encoding? Is it
> possible to change the
> default encoding in Xindice by Java code or by conf
> file?
> 
> > Also, earlier I posted ( email title:  A Solution
> to a
> > few XML result set problems) sample class that
> > retrives nodes from getContentAsDOM method and
> returns
> > it as a DOM Document, DOM4J Document, or as a UTF
> > string. You might want to take a look and see if
> it
> > will help your project.
>  I tried, but I have the very same problem you
> mentioned in your followup
> post, when you get the org.w3c.dom.DOMException:
> DOM003: Namespace error.
> 
> > I'm sorry I could not be more help.
> No sorry. Thank you very much! :-)
> 


Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
> Not to nudge too much into the inner working fo you
> project, but it seems too me like you have a character
> encoding issue. I've had these types of issue using
> other languages. Usually they steam from an into put
> source that is not encoding the file properly for OS
> platform.
I think it should be something like that. I'm developing on Windows XP
Professional. Maybe my problems is cause I'm Spanish and running the locale
version of the OS?

> My first question would are you running this project
> on multiple platforms? (Linux, Solaris, NT, etc.)
> If so if make your code has proper checks on the files
> encoding. These squares look to me be like carriage
> returns.
I'm 99.9% sure these are carriage returns and tabs.

> I would also check to see if all your developers IDE
> and editors are set to save is the same encoding
> format.(ISO-8859-1, UTF-8, etc.) Try insert a new xml
> document from your system into the DB and then
> retrieve it. It help to see if it's just your machine
> out of sync with the DB's encoding.
I'm trying right now...
Ok, I added one of the addressbook documents to a collection using XMLdbGUI.
I set the encoding of the document to 'ISO-8859-1' before adding it. It was
successfully except for the encoding. The encoding now in the DB is missing.
I also tried setting the encoding to 'UTF-8' and it's missing too.

I guess this is a problem...how to fix it? May I add the documents using
Java code and may I specify then the encoding? Is it possible to change the
default encoding in Xindice by Java code or by conf file?

> Also, earlier I posted ( email title:  A Solution to a
> few XML result set problems) sample class that
> retrives nodes from getContentAsDOM method and returns
> it as a DOM Document, DOM4J Document, or as a UTF
> string. You might want to take a look and see if it
> will help your project.
 I tried, but I have the very same problem you mentioned in your followup
post, when you get the org.w3c.dom.DOMException: DOM003: Namespace error.

> I'm sorry I could not be more help.
No sorry. Thank you very much! :-)


Re: how to get rid of the xml declaration on results?

Posted by Evan Borysko <ev...@yahoo.com>.
Not to nudge too much into the inner working fo you
project, but it seems too me like you have a character
encoding issue. I've had these types of issue using
other languages. Usually they steam from an into put
source that is not encoding the file properly for OS
platform. 

My first question would are you running this project
on multiple platforms? (Linux, Solaris, NT, etc.) 
If so if make your code has proper checks on the files
encoding. These squares look to me be like carriage
returns.  

I would also check to see if all your developers IDE
and editors are set to save is the same encoding
format.(ISO-8859-1, UTF-8, etc.) Try insert a new xml
document from your system into the DB and then
retrieve it. It help to see if it's just your machine
out of sync with the DB's encoding.

Also, earlier I posted ( email title:  A Solution to a
few XML result set problems) sample class that
retrives nodes from getContentAsDOM method and returns
it as a DOM Document, DOM4J Document, or as a UTF
string. You might want to take a look and see if it
will help your project.  

I'm sorry I could not be more help.

Cheers,
Evan
--- Josema Alonso <al...@aafunky.com> wrote:
> I've been also folowing the thread 'Problem with
> adding DOM node...', but
> none of the suggested solutions work for me. I've
> tried JDom and it makes
> things a bit better, but still do not work. You know
> why? I'm absolutely
> sure it is because of thos weird characters I
> mentioned earlier. I've
> attached a small image (hope you don't mind) where
> you can see a screen
> fragment of the variables window in WADS, reflecting
> the 'data' variable. It
> is filled with the value got from the DB right after
> a typical
> 'res.getContent()'...I guess it has something to do
> with encoding?!?!?
> 
> When using DOM, parsing stops exactly when one of
> those weird characters is
> found, but it doesn't trow an exception.
> With JDOM it goes 'til the end of the String, but
> creates a new node for
> everyone of those characters.
> 
> Oh, God, I'm desperate, I decided to use a native
> XML DB for this project
> cause I thought things would be easier this way, and
> things are getting
> worse indeed....
> 
> Thanks for listening and helping :-)
> 
> 
> ----- Original Message -----
> From: "Josema Alonso" <al...@aafunky.com>
> To: <xi...@xml.apache.org>
> Sent: Tuesday, November 12, 2002 9:03 PM
> Subject: Re: how to get rid of the xml declaration
> on results?
> 
> 
> > > u can send an example of ur cleaned XML
> document?
> > Of course, below is the produced xml string right
> before making the call
> to
> > the parse method, I don't see anything relevant.
> > But...I'm using Websphere Developer to debug the
> application and I can see
> > in the variables inspector that there are some
> weird characters in between
> > the xml tags, i.e., something like this:
> > ...</EquipmentType>##<EquipmentType>....
> >
> > The # characters are not actually #, but something
> different, like a tiny
> > square...I'll try to analyze the String with Java
> and see what I can get.
> >
> > Thanks.
> >
> > ----------
> > <EquipmentTypes>
> >  <EquipmentType id="player" >
> >   <Description lang="en">Player</Description>
> >   <Description lang="es">Reproductor</Description>
> >  </EquipmentType>
> >  <EquipmentType id="preamp" >
> >   <Description
> lang="en">Preamplifier</Description>
> >   <Description
> lang="es">Preamplificador</Description>
> >  </EquipmentType>
> >  <EquipmentType id="recorder" >
> >   <Description lang="en">Recorder</Description>
> >   <Description lang="es">Grabadora</Description>
> >  </EquipmentType>
> > </EquipmentTypes>
> >
> >
> 

> ATTACHMENT part 2 image/png
name=weird_text_value.png



Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
I've been also folowing the thread 'Problem with adding DOM node...', but
none of the suggested solutions work for me. I've tried JDom and it makes
things a bit better, but still do not work. You know why? I'm absolutely
sure it is because of thos weird characters I mentioned earlier. I've
attached a small image (hope you don't mind) where you can see a screen
fragment of the variables window in WADS, reflecting the 'data' variable. It
is filled with the value got from the DB right after a typical
'res.getContent()'...I guess it has something to do with encoding?!?!?

When using DOM, parsing stops exactly when one of those weird characters is
found, but it doesn't trow an exception.
With JDOM it goes 'til the end of the String, but creates a new node for
everyone of those characters.

Oh, God, I'm desperate, I decided to use a native XML DB for this project
cause I thought things would be easier this way, and things are getting
worse indeed....

Thanks for listening and helping :-)


----- Original Message -----
From: "Josema Alonso" <al...@aafunky.com>
To: <xi...@xml.apache.org>
Sent: Tuesday, November 12, 2002 9:03 PM
Subject: Re: how to get rid of the xml declaration on results?


> > u can send an example of ur cleaned XML document?
> Of course, below is the produced xml string right before making the call
to
> the parse method, I don't see anything relevant.
> But...I'm using Websphere Developer to debug the application and I can see
> in the variables inspector that there are some weird characters in between
> the xml tags, i.e., something like this:
> ...</EquipmentType>##<EquipmentType>....
>
> The # characters are not actually #, but something different, like a tiny
> square...I'll try to analyze the String with Java and see what I can get.
>
> Thanks.
>
> ----------
> <EquipmentTypes>
>  <EquipmentType id="player" >
>   <Description lang="en">Player</Description>
>   <Description lang="es">Reproductor</Description>
>  </EquipmentType>
>  <EquipmentType id="preamp" >
>   <Description lang="en">Preamplifier</Description>
>   <Description lang="es">Preamplificador</Description>
>  </EquipmentType>
>  <EquipmentType id="recorder" >
>   <Description lang="en">Recorder</Description>
>   <Description lang="es">Grabadora</Description>
>  </EquipmentType>
> </EquipmentTypes>
>
>

Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
> u can send an example of ur cleaned XML document?
Of course, below is the produced xml string right before making the call to
the parse method, I don't see anything relevant.
But...I'm using Websphere Developer to debug the application and I can see
in the variables inspector that there are some weird characters in between
the xml tags, i.e., something like this:
...</EquipmentType>##<EquipmentType>....

The # characters are not actually #, but something different, like a tiny
square...I'll try to analyze the String with Java and see what I can get.

Thanks.

----------
<EquipmentTypes>
 <EquipmentType id="player" >
  <Description lang="en">Player</Description>
  <Description lang="es">Reproductor</Description>
 </EquipmentType>
 <EquipmentType id="preamp" >
  <Description lang="en">Preamplifier</Description>
  <Description lang="es">Preamplificador</Description>
 </EquipmentType>
 <EquipmentType id="recorder" >
  <Description lang="en">Recorder</Description>
  <Description lang="es">Grabadora</Description>
 </EquipmentType>
</EquipmentTypes>



AW: how to get rid of the xml declaration on results?

Posted by Stefan Lischke <li...@novacom.net>.
Hi,

> 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

u can send an example of ur cleaned XML document?

mfg stefan

Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
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" <Ca...@ik.fh-hannover.de>
To: <xi...@xml.apache.org>
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



Re: how to get rid of the xml declaration on results?

Posted by Carsten Ziegert <Ca...@ik.fh-hannover.de>.
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  
Alonso:

> Hey, just a quick followup.
>
> Since this is giving so much problems, what if:
> 1.- I get the content as text and not as DOM
> 2.- I apply the suggested functions so I can clean it up
> 3.- I load it into a DOM document with something like:
> private Document load(String s) {
>   DocumentBuilder builder = factory.newDocumentBuilder();
>   return builder.parse(new StringBufferInputStream(s));
> }
>
> I guess this is not a too clean approach, but it seems this time  
> Xindice is
> taking me there...any better ideas?
>
> I need some sleep now...
>
>
> ----- Original Message -----
> From: "Josema Alonso" <al...@aafunky.com>
> To: <xi...@xml.apache.org>
> Sent: Tuesday, November 12, 2002 2:54 AM
> Subject: Re: how to get rid of the xml declaration on results?
>
>
>> Hello,
>>
>>> I asked this some days ago too, but there was no real solution  
>>> besides
>>> hacking code of Xindice.
>> I see...ughhh...
>>
>>> The <EquipmentTypes /> Tag, u have to set by yourself, cause u are
>> getting
>>> a resultset.
>> Ok, I already did it.
>>
>>> the declaration can be killed by
>>> ----------------------------------------------
>>> data= data+xml.substring(xml.indexOf("?>")+2);
>>> -----------------------------------------------
>>> for the
>>>
>>> "xmlns:src="http://xml.apache.org/xindice/Query"
>>> src:col="/db/xmtrader/Recording/Equipment/EquipmentType"  
>>> src:key="mic"
>>>
>>> stuff i found a really bad solution, it "overreads" just that stuff  
>>> and
>>> gives the string back to u without it.
>>> ....
>> Thanks, I'll apply it, too.
>>
>>> hope that helps
>> Sure it does. Thanks a lot.
>>
>> Btw, when I get the resources list I would like to add it to a  
>> document.
>> This will be a document fragment, the method should return a DOM Node  
>> and
>> this one will be after that added to the root element I already  
>> created.
> I'm
>> trying the usual iterating through the ResourceSet and creating the
> fragment
>> in the method, but it is giving me an eception, something like 'DOM005
> Wrong
>> document'. I do not know what's happening, any ideas? (code attached
> below).
>>
>> Thanks a lot :-)
>>
>> ----------------
>> /**
>>  * Search for documents in the DB. If not found, return null.
>>  * The rootCollection is initialized in the constructor
>>  * I'm trying to return the fragment in xmldoc but it gives the  
>> exception
>> :-(
>>  */
>> public Node find(String subCol, String xpath) throws Exception {
>>  Collection col = null;
>>  Node xmldoc = null;
>>  try {
>>   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((((XMLResource)res).getContentAsDOM().getFirstChild( 
> )));
>>    if(xmldoc!=null) {
>>     Node childNode =  
>> ((XMLResource)res).getContentAsDOM().getFirstChild();
>>     xmldoc.appendChild(childNode);
>>    }
>>    else {
>>     xmldoc = ((XMLResource)res).getContentAsDOM().getFirstChild();
>>    }
>>   }
>>   return (xmldoc);
>>  }
>>  catch (XMLDBException e) {
>>   System.err.println("XML:DB - Find Exception occured " +  
>> e.errorCode);
>>   throw e;
>>  }
>>  finally {
>>   if (col != null) {
>>    col.close();
>>   }
>>  }
>> }
>>
>>
>> I'd like to invoke this method from this one:
>> /**
>>  * Inititializes a DOM Node to list the available Equipment types
>>  *
>>  */
>> public void initEquipmentTypes() {
>>  DOMImplementation impl;
>>  try {
>>   // Find the implementation
>>   DocumentBuilderFactory factory =  
>> DocumentBuilderFactory.newInstance();
>>   factory.setNamespaceAware(true);
>>   factory.setValidating ( false );
>>   DocumentBuilder builder = factory.newDocumentBuilder();
>>   impl = builder.getDOMImplementation();
>>  }
>>  catch (Exception ex) {
>>   throw new RuntimeException("Failed to initialize DOM factory. Root
> cause:
>> \n" + ex);
>>  }
>>
>>  //Create the node for the root, 'Class'
>>  Document doc = impl.createDocument( null, "EquipmentTypes", null);
>>  equipmentTypes = doc.getDocumentElement();
>>
>>  //find the Equipment Types available in the database
>>  String localSubCol =  "/Recording/Equipment/EquipmentType";
>>  try {
>>   //Here is where I create an instance and call the method above
>>   //result is not null but it gives an exception
>>   XindiceManager xi = new XindiceManager();
>>   Node result = xi.find(localSubCol, "//EquipmentType");
>>
>>   if (result!=null) {
>>    equipmentTypes.appendChild(result);
>>   }
>>  }
>>  catch (Exception e) {
>>   System.err.println("Cannot get results from the DB: " +  
>> e.getMessage());
>>  }
>> }
>>
>>
>
>
>
-- 
Hannover Medical School 			University of Applied Sciences
Dept. of Hematology and Oncology		Faculty of Information Sciences
Carl-Neuberg-Straße 1				Ricklinger Stadtweg 120
30625 Hannover					30459 Hannover
				http://summit-bmt.fh-hannover.de



Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
Hey, just a quick followup.

Since this is giving so much problems, what if:
1.- I get the content as text and not as DOM
2.- I apply the suggested functions so I can clean it up
3.- I load it into a DOM document with something like:
private Document load(String s) {
  DocumentBuilder builder = factory.newDocumentBuilder();
  return builder.parse(new StringBufferInputStream(s));
}

I guess this is not a too clean approach, but it seems this time Xindice is
taking me there...any better ideas?

I need some sleep now...


----- Original Message -----
From: "Josema Alonso" <al...@aafunky.com>
To: <xi...@xml.apache.org>
Sent: Tuesday, November 12, 2002 2:54 AM
Subject: Re: how to get rid of the xml declaration on results?


> Hello,
>
> > I asked this some days ago too, but there was no real solution besides
> > hacking code of Xindice.
> I see...ughhh...
>
> > The <EquipmentTypes /> Tag, u have to set by yourself, cause u are
> getting
> > a resultset.
> Ok, I already did it.
>
> > the declaration can be killed by
> > ----------------------------------------------
> > data= data+xml.substring(xml.indexOf("?>")+2);
> > -----------------------------------------------
> > for the
> >
> > "xmlns:src="http://xml.apache.org/xindice/Query"
> > src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic"
> >
> > stuff i found a really bad solution, it "overreads" just that stuff and
> > gives the string back to u without it.
> > ....
> Thanks, I'll apply it, too.
>
> > hope that helps
> Sure it does. Thanks a lot.
>
> Btw, when I get the resources list I would like to add it to a document.
> This will be a document fragment, the method should return a DOM Node and
> this one will be after that added to the root element I already created.
I'm
> trying the usual iterating through the ResourceSet and creating the
fragment
> in the method, but it is giving me an eception, something like 'DOM005
Wrong
> document'. I do not know what's happening, any ideas? (code attached
below).
>
> Thanks a lot :-)
>
> ----------------
> /**
>  * Search for documents in the DB. If not found, return null.
>  * The rootCollection is initialized in the constructor
>  * I'm trying to return the fragment in xmldoc but it gives the exception
> :-(
>  */
> public Node find(String subCol, String xpath) throws Exception {
>  Collection col = null;
>  Node xmldoc = null;
>  try {
>   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((((XMLResource)res).getContentAsDOM().getFirstChild()));
>    if(xmldoc!=null) {
>     Node childNode = ((XMLResource)res).getContentAsDOM().getFirstChild();
>     xmldoc.appendChild(childNode);
>    }
>    else {
>     xmldoc = ((XMLResource)res).getContentAsDOM().getFirstChild();
>    }
>   }
>   return (xmldoc);
>  }
>  catch (XMLDBException e) {
>   System.err.println("XML:DB - Find Exception occured " + e.errorCode);
>   throw e;
>  }
>  finally {
>   if (col != null) {
>    col.close();
>   }
>  }
> }
>
>
> I'd like to invoke this method from this one:
> /**
>  * Inititializes a DOM Node to list the available Equipment types
>  *
>  */
> public void initEquipmentTypes() {
>  DOMImplementation impl;
>  try {
>   // Find the implementation
>   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
>   factory.setNamespaceAware(true);
>   factory.setValidating ( false );
>   DocumentBuilder builder = factory.newDocumentBuilder();
>   impl = builder.getDOMImplementation();
>  }
>  catch (Exception ex) {
>   throw new RuntimeException("Failed to initialize DOM factory. Root
cause:
> \n" + ex);
>  }
>
>  //Create the node for the root, 'Class'
>  Document doc = impl.createDocument( null, "EquipmentTypes", null);
>  equipmentTypes = doc.getDocumentElement();
>
>  //find the Equipment Types available in the database
>  String localSubCol =  "/Recording/Equipment/EquipmentType";
>  try {
>   //Here is where I create an instance and call the method above
>   //result is not null but it gives an exception
>   XindiceManager xi = new XindiceManager();
>   Node result = xi.find(localSubCol, "//EquipmentType");
>
>   if (result!=null) {
>    equipmentTypes.appendChild(result);
>   }
>  }
>  catch (Exception e) {
>   System.err.println("Cannot get results from the DB: " + e.getMessage());
>  }
> }
>
>


Re: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
Hello,

> I asked this some days ago too, but there was no real solution besides
> hacking code of Xindice.
I see...ughhh...

> The <EquipmentTypes /> Tag, u have to set by yourself, cause u are
getting
> a resultset.
Ok, I already did it.

> the declaration can be killed by
> ----------------------------------------------
> data= data+xml.substring(xml.indexOf("?>")+2);
> -----------------------------------------------
> for the
>
> "xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic"
>
> stuff i found a really bad solution, it "overreads" just that stuff and
> gives the string back to u without it.
> ....
Thanks, I'll apply it, too.

> hope that helps
Sure it does. Thanks a lot.

Btw, when I get the resources list I would like to add it to a document.
This will be a document fragment, the method should return a DOM Node and
this one will be after that added to the root element I already created. I'm
trying the usual iterating through the ResourceSet and creating the fragment
in the method, but it is giving me an eception, something like 'DOM005 Wrong
document'. I do not know what's happening, any ideas? (code attached below).

Thanks a lot :-)

----------------
/**
 * Search for documents in the DB. If not found, return null.
 * The rootCollection is initialized in the constructor
 * I'm trying to return the fragment in xmldoc but it gives the exception
:-(
 */
public Node find(String subCol, String xpath) throws Exception {
 Collection col = null;
 Node xmldoc = null;
 try {
  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((((XMLResource)res).getContentAsDOM().getFirstChild()));
   if(xmldoc!=null) {
    Node childNode = ((XMLResource)res).getContentAsDOM().getFirstChild();
    xmldoc.appendChild(childNode);
   }
   else {
    xmldoc = ((XMLResource)res).getContentAsDOM().getFirstChild();
   }
  }
  return (xmldoc);
 }
 catch (XMLDBException e) {
  System.err.println("XML:DB - Find Exception occured " + e.errorCode);
  throw e;
 }
 finally {
  if (col != null) {
   col.close();
  }
 }
}


I'd like to invoke this method from this one:
/**
 * Inititializes a DOM Node to list the available Equipment types
 *
 */
public void initEquipmentTypes() {
 DOMImplementation impl;
 try {
  // Find the implementation
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  factory.setNamespaceAware(true);
  factory.setValidating ( false );
  DocumentBuilder builder = factory.newDocumentBuilder();
  impl = builder.getDOMImplementation();
 }
 catch (Exception ex) {
  throw new RuntimeException("Failed to initialize DOM factory. Root cause:
\n" + ex);
 }

 //Create the node for the root, 'Class'
 Document doc = impl.createDocument( null, "EquipmentTypes", null);
 equipmentTypes = doc.getDocumentElement();

 //find the Equipment Types available in the database
 String localSubCol =  "/Recording/Equipment/EquipmentType";
 try {
  //Here is where I create an instance and call the method above
  //result is not null but it gives an exception
  XindiceManager xi = new XindiceManager();
  Node result = xi.find(localSubCol, "//EquipmentType");

  if (result!=null) {
   equipmentTypes.appendChild(result);
  }
 }
 catch (Exception e) {
  System.err.println("Cannot get results from the DB: " + e.getMessage());
 }
}



AW: how to get rid of the xml declaration on results?

Posted by Stefan Lischke <li...@novacom.net>.
Hi Josema,

I asked this some days ago too, but there was no real solution besides
hacking code of Xindice.

The <EquipmentTypes /> Tag, u have to set by yourself, cause u are  getting
a resultset.

the declaration can be killed by
----------------------------------------------
data= data+xml.substring(xml.indexOf("?>")+2);
-----------------------------------------------
for the

"xmlns:src="http://xml.apache.org/xindice/Query"
src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic"

stuff i found a really bad solution, it "overreads" just that stuff and
gives the string back to u without it.

   public String clearXpath(String tmp) throws Exception{
        String ausgabe = "";
        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);
    }


hope that helps



Re: how to get rid of the xml declaration on results?

Posted by "Mark J. Stang" <ma...@earthlink.net>.
Xindice uses namespaces internally.   The responses have namespaces
in them:

> > <?xml version="1.0"?>
> > <EquipmentType id="mic" xmlns:src="http://xml.apache.org/xindice/Query"
> > src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic">
> >  <Description lang="en">Microphone</Description>
> >  <Description lang="es">Micrófono</Description>
> > </EquipmentType>

If you look at the above result, you will see "xmlns:src".   That is a
namespace.
What I do is to create an instance of JDOM, set the namespace and tell it to
parse the above XML.  Then I can ask for either elements or attributes
by walking the document.

hth,

Mark

Josema Alonso wrote:

> I do not understand it. Could you elaborate a bit more, please?
> Btw, I'm not using namespaces...I have enough problems already...
>
> Thank you.
>
> -----Mensaje original-----
> De: Mark J. Stang [mailto:markstang@earthlink.net]
> Enviado el: martes, 12 de noviembre de 2002 6:13
> Para: xindice-users@xml.apache.org
> Asunto: Re: how to get rid of the xml declaration on results?
>
> When I do it internally, I use JDom, set the namespace and
> then parse it.
>
> HTH,
>
> Mark
>
> Matthew Van Horn wrote:
>
> > My solution has been a small batch file to remove those declarations
> > and wrap the whole thing in a <root></root> element
> > Not elegant, but it works. If you find something better, let me know.
> >
> > On Tuesday, November 12, 2002, at 10:09 AM, Josema Alonso wrote:
> >
> > > Hi, all.
> > >
> > > When I run a query against one of my collections I get something like
> > > this:
> > > <?xml version="1.0"?>
> > > <EquipmentType id="mic" xmlns:src="http://xml.apache.org/xindice/Query"
> > > src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic">
> > >  <Description lang="en">Microphone</Description>
> > >  <Description lang="es">Micrófono</Description>
> > > </EquipmentType>
> > >
> > > <?xml version="1.0"?>
> > > <EquipmentType id="player"
> > > xmlns:src="http://xml.apache.org/xindice/Query"
> > > src:col="/db/xmtrader/Recording/Equipment/EquipmentType"
> > > src:key="player">
> > >  <Description lang="en">Player</Description>
> > >  <Description lang="es">Reproductor</Description>
> > > </EquipmentType>
> > >
> > > <?xml version="1.0"?>
> > > <EquipmentType id="preamp"
> > > xmlns:src="http://xml.apache.org/xindice/Query"
> > > src:col="/db/xmtrader/Recording/Equipment/EquipmentType"
> > > src:key="preamp">
> > >  <Description lang="en">Preamplifier</Description>
> > >  <Description lang="es">Preamplificador</Description>
> > > </EquipmentType>
> > >
> > > More or less is what I expected, but I'd prefer to have a root element
> > > named
> > > 'EquipmentTypes' and I would need to get rid of the xml declaration in
> > > every
> > > element. Is it possible?
> > >
> > > Thanks.
> > >
>
> --
> Mark J Stang
> System Architect
> Cybershop Systems

--
Mark J Stang
System Architect
Cybershop Systems


RE: how to get rid of the xml declaration on results?

Posted by Josema Alonso <al...@aafunky.com>.
I do not understand it. Could you elaborate a bit more, please?
Btw, I'm not using namespaces...I have enough problems already...

Thank you.


-----Mensaje original-----
De: Mark J. Stang [mailto:markstang@earthlink.net]
Enviado el: martes, 12 de noviembre de 2002 6:13
Para: xindice-users@xml.apache.org
Asunto: Re: how to get rid of the xml declaration on results?


When I do it internally, I use JDom, set the namespace and
then parse it.

HTH,

Mark

Matthew Van Horn wrote:

> My solution has been a small batch file to remove those declarations
> and wrap the whole thing in a <root></root> element
> Not elegant, but it works. If you find something better, let me know.
>
> On Tuesday, November 12, 2002, at 10:09 AM, Josema Alonso wrote:
>
> > Hi, all.
> >
> > When I run a query against one of my collections I get something like
> > this:
> > <?xml version="1.0"?>
> > <EquipmentType id="mic" xmlns:src="http://xml.apache.org/xindice/Query"
> > src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic">
> >  <Description lang="en">Microphone</Description>
> >  <Description lang="es">Micrófono</Description>
> > </EquipmentType>
> >
> > <?xml version="1.0"?>
> > <EquipmentType id="player"
> > xmlns:src="http://xml.apache.org/xindice/Query"
> > src:col="/db/xmtrader/Recording/Equipment/EquipmentType"
> > src:key="player">
> >  <Description lang="en">Player</Description>
> >  <Description lang="es">Reproductor</Description>
> > </EquipmentType>
> >
> > <?xml version="1.0"?>
> > <EquipmentType id="preamp"
> > xmlns:src="http://xml.apache.org/xindice/Query"
> > src:col="/db/xmtrader/Recording/Equipment/EquipmentType"
> > src:key="preamp">
> >  <Description lang="en">Preamplifier</Description>
> >  <Description lang="es">Preamplificador</Description>
> > </EquipmentType>
> >
> > More or less is what I expected, but I'd prefer to have a root element
> > named
> > 'EquipmentTypes' and I would need to get rid of the xml declaration in
> > every
> > element. Is it possible?
> >
> > Thanks.
> >

--
Mark J Stang
System Architect
Cybershop Systems



Re: how to get rid of the xml declaration on results?

Posted by "Mark J. Stang" <ma...@earthlink.net>.
When I do it internally, I use JDom, set the namespace and
then parse it.

HTH,

Mark

Matthew Van Horn wrote:

> My solution has been a small batch file to remove those declarations
> and wrap the whole thing in a <root></root> element
> Not elegant, but it works. If you find something better, let me know.
>
> On Tuesday, November 12, 2002, at 10:09 AM, Josema Alonso wrote:
>
> > Hi, all.
> >
> > When I run a query against one of my collections I get something like
> > this:
> > <?xml version="1.0"?>
> > <EquipmentType id="mic" xmlns:src="http://xml.apache.org/xindice/Query"
> > src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic">
> >  <Description lang="en">Microphone</Description>
> >  <Description lang="es">Micrófono</Description>
> > </EquipmentType>
> >
> > <?xml version="1.0"?>
> > <EquipmentType id="player"
> > xmlns:src="http://xml.apache.org/xindice/Query"
> > src:col="/db/xmtrader/Recording/Equipment/EquipmentType"
> > src:key="player">
> >  <Description lang="en">Player</Description>
> >  <Description lang="es">Reproductor</Description>
> > </EquipmentType>
> >
> > <?xml version="1.0"?>
> > <EquipmentType id="preamp"
> > xmlns:src="http://xml.apache.org/xindice/Query"
> > src:col="/db/xmtrader/Recording/Equipment/EquipmentType"
> > src:key="preamp">
> >  <Description lang="en">Preamplifier</Description>
> >  <Description lang="es">Preamplificador</Description>
> > </EquipmentType>
> >
> > More or less is what I expected, but I'd prefer to have a root element
> > named
> > 'EquipmentTypes' and I would need to get rid of the xml declaration in
> > every
> > element. Is it possible?
> >
> > Thanks.
> >

--
Mark J Stang
System Architect
Cybershop Systems


Re: how to get rid of the xml declaration on results?

Posted by Matthew Van Horn <ho...@accesstech.com>.
My solution has been a small batch file to remove those declarations 
and wrap the whole thing in a <root></root> element
Not elegant, but it works. If you find something better, let me know.

On Tuesday, November 12, 2002, at 10:09 AM, Josema Alonso wrote:

> Hi, all.
>
> When I run a query against one of my collections I get something like 
> this:
> <?xml version="1.0"?>
> <EquipmentType id="mic" xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/xmtrader/Recording/Equipment/EquipmentType" src:key="mic">
>  <Description lang="en">Microphone</Description>
>  <Description lang="es">Micrófono</Description>
> </EquipmentType>
>
> <?xml version="1.0"?>
> <EquipmentType id="player" 
> xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/xmtrader/Recording/Equipment/EquipmentType" 
> src:key="player">
>  <Description lang="en">Player</Description>
>  <Description lang="es">Reproductor</Description>
> </EquipmentType>
>
> <?xml version="1.0"?>
> <EquipmentType id="preamp" 
> xmlns:src="http://xml.apache.org/xindice/Query"
> src:col="/db/xmtrader/Recording/Equipment/EquipmentType" 
> src:key="preamp">
>  <Description lang="en">Preamplifier</Description>
>  <Description lang="es">Preamplificador</Description>
> </EquipmentType>
>
> More or less is what I expected, but I'd prefer to have a root element 
> named
> 'EquipmentTypes' and I would need to get rid of the xml declaration in 
> every
> element. Is it possible?
>
> Thanks.
>