How to display xml with two parent nodes - java

How to generate xml using dom parser in java , as shown below
<result>
<schma_index>
<id>8</id>
<name>raja</name>
<schma_index>
</result>
Above should be display like
<Massage>No privilege</Mesaage>
<result>
<schma_index>
<id>8</id>
<name>raja</name>
<schma_index>
</result>

You can't have two root elements in xml. Read about well-formed xml. You can however generate Message and result xml's separately and then concatenate them. However, the parser's can't parse the result xml.

In a "Valid" XML, there can be only one root element. As per this specification:"There is exactly one element, called the root, or document element, no part of which appears in the content of any other element."
So, you have two options:
create two seperate xml files - one containing Message and other results
enclose Message & Results with some other root element:
i.e,
<root>
<Massage>No privillage</Mesaage>
<result>
<schma_index>
<id>8</id>
<name>raja</name>
<schma_index>
</result>
</root>

Related

How can i get nodeList using xpath in Apache Camel

I have an xml like
<Response>
<Status>
<Code>0</Code>
</Status>
<Order>
<OrderId>1</OrderId>
</Order>
</Response>
How can i get all the nodes instead Response, but without the tag
When i try this, I get the entire xml
<xpath>/Response</xpath>
When i try this, the output is blank
<xpath>/Response/text()</xpath>
What can i do so I only get this, with the outer tag
<Status>
<Code>0</Code>
</Status>
<Order>
<OrderId>1</OrderId>
</Order>
It is not clear if the desired output is the XML without the document element or if you want all of the text from within the XML.
If you want the sequence of child elements, then you would want:
/Response/*
If you want the You could use the string() function to get the computed text value of the /Response element:
string(/Response)
If that happens to include some unwanted carriage returns and whitespace, due to the XML being pretty-printed, you could use normalize-space(), which will collapse repeated whitespace:
normalize-space(/Response)

Why is DOM doing this? (Wrong nodeName XML)

I have this XML (just a little part.. the complete xml is big)
<Root>
<Products>
<Product ID="307488">
<ClassificationReference ClassificationID="AR" Type="AgencyLink"/>
<ClassificationReference ClassificationID="AM" Type="AgencyLink">
<MetaData>
<Value AttributeID="tipoDeCompra" ID="C">Compra Centralizada</Value>
</MetaData>
</ClassificationReference>
</Product>
</Products>
</Root>
Well... I want to get the data from the line
<Value AttributeID="tipoDeCompra" ID="C">Compra Centralizada</Value>
I'm using DOM and when I use nodoValue.getTextContent() I got "Compra Centralizada" and that is ok...
But when I use nodoValue.getNodeName() I got "MetaData" but I was expecting "Value"
What is the explanations for this behaviour?
Thanks!
Your nodeValuevariable most likely points to the MetaData node, so the returned name is correct.
Note that for an element node Node.getTextContent() returns the concatenation of the text content of all child nodes. Therefore in your example the text content of the MetaData element is equal to the text content of the Value element, namely Compra Centralizada.
I guess your are getting the Node object using getElementsByTagName("MetaData"). In this case nodoValue.getTextContent() will return the text content correctly but to get the node name you need to get the child node.
Your current node must be MetaData and getTextContent() will give all the text within its opening and closing tags. This is because you are getting
Compra Centralizada
as the value. You should get the first child using getChildNodes() and then can get the Value tag.

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.

DOM xml parsing difficulties in 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

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