how to generate xml using single jaxb instance - java

I want to generate following xml structure using one jaxb marshaller instance.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ABC>//root element
<BCD>
<DEF>
<EFG>xyz</EFG>
<FGH>
<GHI>PASSWORD_1</GHI>
<HIJ>false</HIJ>
</FGH>
</DEF>
</BCD>
<CDE/>
</ABC>
<ABC>//root element
<BCD>
<DEF>
<EFG>xyz</EFG>
<FGH>
<GHI>PASSWORD_2</GHI>
<HIJ>false</HIJ>
</FGH>
</DEF>
</BCD>
<CDE/>
</ABC>
<ABC>// root element
<BCD>
<DEF>
<EFG>xyz</EFG>
<FGH>
<GHI>PASSWORD_3</GHI>
<HIJ>false</HIJ>
</FGH>
</DEF>
</BCD>
<CDE/>
</ABC>
I above structure node ABC is root node which I want to repeat in same file for multiple records in list. Is there a way to do so in JaxB?

You cannot since this is not valid XML. XML documents must have exactly one root element.

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)

Change tag name in XML

I have exception like this:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"tax"). Expected elements are <{}TaxGroup>
I have resposne which is String and look like this:
<?xml version="1.0" encoding="utf-8"?>
<tax xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xml_message_type>tax</xml_message_type>
<version>
<xml_version>1.0</xml_version>
</version>
</tax>
How to iterate over this XML and repleace tag name tax with TaxGroup ?
It must mean that you try to unmarshal your XML which has a root element <tax> to Java Jaxb class which expects root element <TaxGroup> instead.
Definitelly XML does not match what unmarshaler was requested.
check what Java class do you expect to get out of this XML, and change it accordingly. Also check what element name defined in its JAXB annotation.

How to create XML with declared namespace?

I am creating a xml request using java.
I am new in creating xmls using java.
Here is code:
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("UserRequest");
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns0", "https://com.user.req");
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.appendChild(rootElement);
// user element
Element user = doc.createElement("User");
rootElement.appendChild(user);
// userAttributes element
Element userAttr = doc.createElement("UserAttributes");
rootElement.appendChild(userAttr);
// name elements
Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode("hello"));
userAttr.appendChild(name);
// value elements
Element value = doc.createElement("Value");
name.appendChild(doc.createTextNode("dude"));
userAttr.appendChild(value);
Expected output:
<?xml version="1.0" encoding="UTF-8"?>
<UserRequest
xmlns:ns0="https://com.user.req"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="ns0:UserRequest">
<User/>
<UserAttributes>
<Name>hello</Name>
<Value>dude</Value>
</UserAttributes>
</UserRequest>
Generated output:
<?xml version="1.0" encoding="UTF-8"?>
<UserRequest
xmlns:ns0="https://com.user.req"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<User/>
<UserAttributes>
<Name>hello</Name>
<Value>dude</Value>
</UserAttributes>
</UserRequest>
How to get correct namespace (as shown at expected section).
There's nothing wrong with the namespaces in your generated output. However this is an accident ... you're using setAttributeNS() to do something it's not intended for.
Read up on XML namespace declarations and namespace prefixes. That will be a lot easier than trying to explain point-by-point why you're not getting what you expected. For example, xmlns is not a namespace prefix, and xsi:type is not a namespace.
Instead of trying to create the desired namespace declarations as if they were normal attributes, delete these two lines
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:ns0", "https://com.user.req");
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
and instead use
rootElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance",
"xsi:type", "ns0:UserRequest");
This should give you most of your expected output, except for the ns0 namespace prefix declaration. It won't generate that because you're not using ns0 on any element or attribute. Did you mean to have
<ns0:UserRequest ...
in your expected output?

How to display xml with two parent nodes

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>

Android java XML junk after document element

I'm using SAX to read/parse XML documents and I have it working fine except for this particular site where eclipse tells me "junk after document element" and I get no data returned
http://www.zachblume.com/apis/rhyme.php?format=xml&word=example
The site is not mine..just trying to get some data from it.
Yes, that's not an XML document. It's trying to include more than one root element:
<?xml version="1.0"?>
<word>ampal</word>
<word>ample</word>
<word>hampel</word>
<word>hample</word>
<word>lampl</word>
<word>pampel</word>
<word>sample</word>
The parser regards everything after <word>ampal</word> as by that time it's read a complete document... hence the complain about "junk after document element".
An XML document can only have one root, but several children within the root. For example:
<?xml version="1.0"?>
<words>
<word>ampal</word>
<word>ample</word>
<word>hampel</word>
<word>hample</word>
<word>lampl</word>
<word>pampel</word>
<word>sample</word>
</words>
The page does not contain XML. It contains an XML snippet at best:
<?xml version="1.0"?>
<word>ampal</word>
<word>ample</word>
<word>hampel</word>
<word>hample</word>
<word>lampl</word>
<word>pampel</word>
<word>sample</word>
This is incorrect since there is no document element. SAX interprets the first <word> as the document element, and correctly reports "junk after document element" since for all it knows, the document element ends on line 1.
To get around the error, do not treat this document as XML. Download it as text, remove the XML declaration (<?xml version="1.0"?>) and then wrap it in a fake document element before you try to process it.

Categories