Need to parse java string with XML data

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Need to parse java string with XML data

Walter Vaughan
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
Reply | Threaded
Open this post in threaded view
|

Re: Need to parse java string with XML data

David E Jones

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


smime.p7s (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Need to parse java string with XML data

Walter Vaughan
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


Reply | Threaded
Open this post in threaded view
|

Re: Need to parse java string with XML data

cjhowe
> <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 <[hidden email]> 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
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Need to parse java string with XML data

Walter Vaughan
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
Reply | Threaded
Open this post in threaded view
|

Re: Need to parse java string with XML data

David E Jones

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


smime.p7s (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Need to parse java string with XML data

Walter Vaughan
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
>
>