aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to add Namespaces programmatically to the XML parser ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to add Namespaces programmatically to the XML parser ?" Watch "How to add Namespaces programmatically to the XML parser ?" New topic
Author

How to add Namespaces programmatically to the XML parser ?

Lotfi Sekhri
Greenhorn

Joined: Aug 28, 2009
Posts: 3
Hello,

I would like to parse XML files that doesn't contain the xmlns definitions
I want to add the xmlns definitions in the java code to the parser.
How could I do that ?

code:


Thanks for help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

I doubt that you can. Your proposal would allow a parser to treat as well-formed a document which is actually not well-formed, and the XML Recommendation doesn't allow that.

Why would you want to do this?
Lotfi Sekhri
Greenhorn

Joined: Aug 28, 2009
Posts: 3
I want to do this, because I want to parse lot of XML files that haven't the xmlns definitions
Effectively they are not well formed,
but i would like to find a solution to parse them like that without changing them,
by adding in the Java code the namespaces that they are using.

I found this : org.xml.sax.helpers.NamespaceSupport
but I don't know how this can be useful !
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
because I want to parse lot of XML files that haven't the xmlns definitions


Why do you want to add the namespace qualifiers during parsing but not to the actual XML document?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Lotfi Sekhri wrote:Effectively they are not well formed


There's no "effectively". Either a document is well-formed or it isn't. And XML parsers are only allowed to completely parse the well-formed kind.

It seems to me it shouldn't be too hard to write some non-XML code which reads the documents and slaps the correct namespace declarations into their headers, thereby making them well-formed. Why not just do that?
Lotfi Sekhri
Greenhorn

Joined: Aug 28, 2009
Posts: 3
I agree with you.

But my problem is that I'm using another library that creates XML files dynamically and they are not well formed.
And, I can't change this library code.

I'm just searching to know if there is a way to do it my self, by adding those XML namespaces to the context of the parser.
Do you think that it's not possible ?

thanks
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You could always read the whole document into memory as a List of Strings, modify it with String operations and then create an InputSource for the parser that reads the modified text.

Im sure there are plenty of other ways - the Java library is very flexible about parser inputs.

Bill
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Lotfi Sekhri wrote:But my problem is that I'm using another library that creates XML files dynamically and they are not well formed.
And, I can't change this library code.


Then I recommend you stop using it.

I'm just searching to know if there is a way to do it my self, by adding those XML namespaces to the context of the parser.
Do you think that it's not possible ?

Yes, I think that you are wasting your time on that idea. If you are being forced by circumstances beyond your control to consume that malformed XML, then fix it before you give it to the parser.

And get the people who are giving you that data to fix their processes so they produce well-formed XML. There's no excuse for producing garbage XML these days.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

I just remembered something... it's possible to create XML parsers which are not namespace-aware. There's a method on the parser factory which you call if I'm not mistaken. You could try that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add Namespaces programmatically to the XML parser ?