• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add Namespaces programmatically to the XML parser ?

 
Lotfi Sekhri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20746
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20746
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20746
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20746
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic