DOM xml parsing difficulties in java - java

<XML>
<log>
<date>20022014</date>
<time>2323</time>
<schools>
<school name="ahss"/>
<student>shiva</student>
<class>B</class>
</schools>
</log>
<log>...</log>
</XML>
need to parse this xml format using DOM i have tried many substitutes but i couldn't get it
is there any one to give shoulder

Related

Java - Parse XML dynamically and insert elements and values into a stack or deque

I have a requirement where I should be able to take any XML document, parse it dynamically and store into a stack/deque for further processing.
Can someone recommend what is a good way to parse XMLs dynamically in JAVA.
Consider this XML
<Response>
<Stock>
<RecordID>130</RecordID>
<SegmentLength>0023</SegmentLength>
<Account>
<Number>233342</Number>
<Type>P</Type>
</Account>
</Stock>
<Stock>
<RecordID>030</RecordID>
<SegmentLength>1023</SegmentLength>
<Account>
<Number>255673</Number>
<Type>P</Type>
</Account>
</Stock>
</Response>
How can I write a method that parsers this XML dynamically and pushes elements into a stack/deque.
I cannot use DOM as DOM requires me to provide the element tag while parsing. The program should be able to accept any XML and parse it dynamically

How to use xpath in camel when the outermost element has an xmlns attribute?

I am having some trouble using xpath to extract the "Payload" values below using apache-camel. I use the below xpath in my route for both of the example xml, the first example xml returns SomeElement and SomeOtherElement as expected, but the second xml seems unable to parse the xml at all.
xpath("//Payload/*")
This example xml parses just fine.
<Message>
<Payload>
<SomeElement />
<SomeOtherElement />
</Payload>
</Message>
This example xml does not parse.
<Message xmlns="http://www.fake.com/Message/1">
<Payload>
<SomeElement />
<SomeOtherElement />
</Payload>
</Message>
I found a similar question about xml and xpath, but it deals with C# and is not a camel solution.
Any idea how to solve this using apache-camel?
Your 2nd example xml, specifies a default namespace: xmlns="http://www.fake.com/Message/1" and so your xpath expression will not match, as it specifies no namespace.
See http://camel.apache.org/xpath.html#XPath-Namespaces on how to specify a namespace.
You would need something like
Namespaces ns = new Namespaces("fk", "http://www.fake.com/Message/1");
xpath("//fk:Payload/*", ns)
I'm not familiar with Apache-Camel, this was just a result of some quick googling.
An alternative maybe to just change your xPath to something like
xpath("//*[local-name()='Payload']/*)
Good luck.

Java DOM XML PARSER, how to get text values from document with that structure?

How to use JAVA DOM XML PARSER with an XML structure like this, can anybody give me an example, I have to do it in 3 hours.
the document looks sth like this:
<mainNode>
<header>
<a></a>
<b></b>
</header>
<part1>
<inside>
<1>text1</1>
<2>text2</2>
</inside>
</part1>
<part2>
<inside>
<1>text1</1>
<2>tex2</2>
</inside>
</part2>
</mainNode>
I need to get the 1 and two text values from a big file.

remove <![CDATA[ tag from xml webserivce responses

I am using below format to response for the webservices.
<Name>abc</Name>
<Detail>
<RESPONSE>
<Age>20</Age>
<Address>blahblah</Address>
<Mobile>12345</Mobile>
</RESPONSE>
</Detail>
Due to the requirements, I need to return xml format data insides the <Detail></Detail> tag.
In my java class, I parse using Xstream and format into xml and put insides the Detail tag.
But when I test using SOAPUI , I am getting extra <![CDATA[<RESPONSE>.. <</RESPONSE>]]> insdies Detail tag.
How can I avoid having those CDATA tag for the xml response?
<![CDATAP[......]]> is used to tell that the XML meaning of it should not be taken and to treat it as normal text that is called character data. so Parser won't seek for any XML meaning in it.
As Dave Newton and kshitij told it will automatically removed while converting it into object.
If you are not supposed to parse it as it is no issue to bother about it.

How to get data with tag name & their values inside parent tag in xml

I am working on Java. I am parsing an xml file, I am getting tag values, it is working. I have xml file as follows:
<DOC>
<STUDENT>
<ID>1</ID>
<NAME>DAN</NAME>
<ADDRESS>U.K</ADDRESS>
</STUDENT>
<STUDENT>
<ID>2</ID>
<NAME>JACK</NAME>
<ADDRESS>U.S</ADDRESS>
</STUDENT>
</DOC>
I have question that I want to fetch data inside <DOC>....</DOC> with their tag name & value as well. Means I want data as follows:
"<STUDENT>
<ID>1</ID>
<NAME>DAN</NAME>
<ADDRESS>U.K</ADDRESS>
</STUDENT>
<STUDENT>
<ID>2</ID>
<NAME>JACK</NAME>
<ADDRESS>U.S</ADDRESS>
</STUDENT>"
Please guide me how to do it.
The most common approaches in Java are to use one of either SAX or Dom parsing libraries.
If you look them up you should find loads of documentation/tutorials about them.
Dom is the easiest to use normally as it stores the entire XML in memory and you cna then access any tag, however, this is less performant and can be problematic if you are using very large XML. SAX requires more work, but reads the XML and processes each tag as it gets to it.
Both are able to do what you need though.
Take a look at SAX Parser.
This link might be helpful too: http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

Categories