You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ofbiz.apache.org by Walter Vaughan <wv...@steelerubber.com> on 2007/03/28 22:04:15 UTC

Need to parse java string with XML data

Does anyone know of some good java SAX or DOM "Hello World" type tutorial sites? 
I have xml data in a java string (NOT A FILE) that I want to parse and use in 
ofBiz, and every example I've looked at looks like it's either severe overkill 
or way underkill (character by character).

My problem also is finding example code where the data is a string variable 
inside java and is in a <name=value> style xml rather than <name>value</name> xml.

--
Walter

Re: Need to parse java string with XML data

Posted by Walter Vaughan <wv...@steelerubber.com>.
Thanks David & Chris. That's the direction I needed.


David E. Jones wrote:

> 
> Here's the method you're looking for:
> 
> public static Document readXmlDocument(String content, boolean validate)
> 
> Even if you pass false for validate the XML content DOES need to be  at 
> least well formed so that the parser can do something with it.
> 
> What you passed in isn't actually XML, not just not well formed, but  
> just not XML. It has less than and greater than signs, but that's  about 
> where the similarity ends. So, you'd probably have to write a  parser by 
> hand and while you could use one of the many good parsing  tool sets out 
> there, for something this small a few java.lang.String  operations may 
> be sufficient.
> 
> -David
> 
> 
> On Mar 29, 2007, at 12:47 PM, Walter Vaughan wrote:
> 
>> Chris Howe wrote:
>>
>>>> <number=3106799433><name=><dnis=2149><state=active>
>>>
>>> I don't believe that to be well-formed XML. I suspect there is a  parent
>>> to those "elements" and if there is, it should look like this...
>>> As child elements:
>>> <parent>
>>>   <number>3106799433</number>
>>>   <name/>
>>>   <dnis>2149</dnis>
>>>   <state>active</state>
>>> </parent>
>>> or..
>>> as attributes:
>>> <parent number="3106799433" name="" dnis="2149" state="active"/>
>>
>>
>> No that's the way it gets kicked to me. I didn't even think to  
>> complain to the server app suppling that data that I'm not getting  
>> valid XML.
>>
>> If I can convince them to supply the data in a well formed XML  
>> layout, do you think I can then convert that to something that can  be 
>> used in an ofBiz service?
>>
>> Thanks
>>
>> -- 
>> Walter
> 
> 


Re: Need to parse java string with XML data

Posted by "David E. Jones" <jo...@hotwaxmedia.com>.
Here's the method you're looking for:

public static Document readXmlDocument(String content, boolean validate)

Even if you pass false for validate the XML content DOES need to be  
at least well formed so that the parser can do something with it.

What you passed in isn't actually XML, not just not well formed, but  
just not XML. It has less than and greater than signs, but that's  
about where the similarity ends. So, you'd probably have to write a  
parser by hand and while you could use one of the many good parsing  
tool sets out there, for something this small a few java.lang.String  
operations may be sufficient.

-David


On Mar 29, 2007, at 12:47 PM, Walter Vaughan wrote:

> Chris Howe wrote:
>
>>> <number=3106799433><name=><dnis=2149><state=active>
>> I don't believe that to be well-formed XML. I suspect there is a  
>> parent
>> to those "elements" and if there is, it should look like this...
>> As child elements:
>> <parent>
>>   <number>3106799433</number>
>>   <name/>
>>   <dnis>2149</dnis>
>>   <state>active</state>
>> </parent>
>> or..
>> as attributes:
>> <parent number="3106799433" name="" dnis="2149" state="active"/>
>
> No that's the way it gets kicked to me. I didn't even think to  
> complain to the server app suppling that data that I'm not getting  
> valid XML.
>
> If I can convince them to supply the data in a well formed XML  
> layout, do you think I can then convert that to something that can  
> be used in an ofBiz service?
>
> Thanks
>
> --
> Walter


Re: Need to parse java string with XML data

Posted by Walter Vaughan <wv...@steelerubber.com>.
Chris Howe wrote:

>><number=3106799433><name=><dnis=2149><state=active>
> 
> 
> I don't believe that to be well-formed XML. I suspect there is a parent
> to those "elements" and if there is, it should look like this...
> 
> As child elements:
> <parent>
>   <number>3106799433</number>
>   <name/>
>   <dnis>2149</dnis>
>   <state>active</state>
> </parent>
> 
> or..
> as attributes:
> <parent number="3106799433" name="" dnis="2149" state="active"/>

No that's the way it gets kicked to me. I didn't even think to complain to the 
server app suppling that data that I'm not getting valid XML.

If I can convince them to supply the data in a well formed XML layout, do you 
think I can then convert that to something that can be used in an ofBiz service?

Thanks

--
Walter

Re: Need to parse java string with XML data

Posted by Chris Howe <cj...@yahoo.com>.
> <number=3106799433><name=><dnis=2149><state=active>

I don't believe that to be well-formed XML. I suspect there is a parent
to those "elements" and if there is, it should look like this...

As child elements:
<parent>
  <number>3106799433</number>
  <name/>
  <dnis>2149</dnis>
  <state>active</state>
</parent>

or..
as attributes:
<parent number="3106799433" name="" dnis="2149" state="active"/>


--- Walter Vaughan <wv...@steelerubber.com> wrote:

> David E. Jones wrote:
> 
> > 
> > Check out the UtilXml class in OFBiz, it has some handy methods for
>  
> > this kind of stuff, including that very thing.
> 
> Okay I've read
> 
>
http://www.ofbiz.eu/framework/base/build/javadocs/org/ofbiz/base/util/UtilXml.html
> and I've looked at sample code and nothing jumps out at me. :(
> 
> I'll have a string with data like this in it...
> <number=3106799433><name=><dnis=2149><state=active>
> 
> It's not really clear to me get from that string via readXmlDocument,
> 
> firstChildElement, and get to an array or whatever where I can
> reference 
> "number" and get "3016799433" as a result.
> 
> --
> Walter
> 
> 
> 


Re: Need to parse java string with XML data

Posted by Walter Vaughan <wv...@steelerubber.com>.
David E. Jones wrote:

> 
> Check out the UtilXml class in OFBiz, it has some handy methods for  
> this kind of stuff, including that very thing.

Okay I've read

http://www.ofbiz.eu/framework/base/build/javadocs/org/ofbiz/base/util/UtilXml.html
and I've looked at sample code and nothing jumps out at me. :(

I'll have a string with data like this in it...
<number=3106799433><name=><dnis=2149><state=active>

It's not really clear to me get from that string via readXmlDocument, 
firstChildElement, and get to an array or whatever where I can reference 
"number" and get "3016799433" as a result.

--
Walter



Re: Need to parse java string with XML data

Posted by "David E. Jones" <jo...@hotwaxmedia.com>.
Check out the UtilXml class in OFBiz, it has some handy methods for  
this kind of stuff, including that very thing.

-David


On Mar 28, 2007, at 2:04 PM, Walter Vaughan wrote:

> Does anyone know of some good java SAX or DOM "Hello World" type  
> tutorial sites? I have xml data in a java string (NOT A FILE) that  
> I want to parse and use in ofBiz, and every example I've looked at  
> looks like it's either severe overkill or way underkill (character  
> by character).
>
> My problem also is finding example code where the data is a string  
> variable inside java and is in a <name=value> style xml rather than  
> <name>value</name> xml.
>
> --
> Walter