Java Method create datapoint node in the container of XML - java

how to fetch elements of container from XSD by XPath.

Related

How to validate the name property of an element in XML file using javax xml validation

I have the following XML file:
<node name="FIXED_NAME">
...
</node>
Note that all elements in my XML are node (same name) and I can not change them for some reasons.
I am using java xml validator and xsd file for validation.
I want to validate that the name property for the root node must be FIXED_NAME.
How my xsd file should be?
I cannot think of any solution to your problem, for these reasons:
XSD 1.0 identifies the type of a tag by matching the hierarchy of tags in the XML document against the element/type definitions in the XSD. In your case, there is no hierarchy of names (because all tag names are the same).
XSD 1.1 provides some extra flexibility via xs:alternative and xs:assert - but they do not allow references to the parent axis.
For these reasons, I cannot think of any way to use the features of XML Schema to describe your XML.

Java spring XML binding based on conditions using JAXB

I am binding XML to object using JAXB, is it possible to bind based on conditions such as
Bind href attribute in api:page element where attribute position is equal to next.
Or get all records as standard list and then filter out by using if condition
<api:pagination results-count="93" items-per-page="25">
<api:page position="this" href="https://url1.com" />
<api:page position="next" href="https://url1.com?pid=2" />
</api:pagination>
It is not necessary to make a transformation for such a simple setup.Take advantage of #XmlAnyElement https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/XmlAnyElement.html and just map your collection as a list of Elements.
#XmlElementWrapper(name = "pagination")
#XmlAnyElement
public List<Element> getPages() {
return pages;
}
Where Element is org.w3c.dom.Element
The #XmlElementWrapper is optional, if you want you can map your pagination element. I am not sure you need it though.
Then you extract the position with:
page.getAttribute("position")
and
page.getAttribute("href")
for the url

JAXB XML object to a single inline string (pass it to DB)

I am using maven and jaxb to process an inbound XML file and query a DB. Request is the inbound XML and response is the outbound XML. There is a (many possible formats here) within the XML file (child of the main). I need to create a single string with all of the contents and tags of ObjectData. The DB takes this tag as a single parameter.
What is the most efficient and useful way to have an entire XML node be placed into a string with tags included?
Thanks!

#XMLRootElement element or array name

I am currently trying to unmarshall a xml get response into one intricate java object. That java object has other classes as fields, so it goes very deep. Now, I am using the JaxbMarshaller2 for this job. So far everything has been going well, but now I am have a question:
Basically one of the tags in the XML stream is called "images" and contains an array of individual image items, each enclosed in the "item" tag.
<images>
<item>
<name></name>
</item>
<item>
<name></name>
</item>
</images>
Now, in my root java class, I have a property called images, which is a list of these image items (I made a custom class for these items.)
#XmlElement(name = "images")
private List<ProductImage> images;
Ok, now inside the ProductImage class, would the XML root element above the class name be #XmlRootElement(name="images") or #XmlRootElement(name="item")?
Thanks for your responses.
The #XmlRootElement annotation is only necessary for the root element of your xml file. Each parent always defines the names of the tags of its children. But since the root element has no parent, there is an additional annotation necessary to define its name (i.e. #XmlRootElement).
Option1:
Assuming that the <images> node is your root node of your xml file.
Create a class ProductImageCollection which has the #XmlRootElement(name="images") annotation.
Inside this class there should be a List<ProductImage> which has an annotation #XmlElement(name="image")
Option2:
Assuming that your <images> node is part of a bigger xml structure, which already has correct annotations.
Then you don't need any additional #XmlRootElement annotations. You can directly annotate your list with #XmlElementWrapper(name="images").

How to convert attributes to nodes of an xml in java?

I am parsing an XML file like
<STRUCTURE ID="EV_Se96ffb9a-df1f-44e7-a4f8-818688cf8d3b">
<SHORT-NAME>STRUCT</SHORT-NAME>
<LONG-NAME>Structure</LONG-NAME>
</STRUCTURE>
where I am getting the child nodes of STRUCTURE and adding it to a nodeList.
Can I have an option to add the attributes of STRUCTURE i.e, ID to a nodeList ?
How to convert the attributes to a node and add it to a nodelist ?
Please help me out.
I am using the DOM parsing strategy
The Node class has a method getAttributes() which returns a NamedNodeMap. Of course, only elements will return an appropriate named node map (as only elements can have attributes).
On such a NamedNodeMap you can then retrieve the attribute node via a call to getNamedItem(String) or via a call to item(int). Note, that the return type of these methods is a Node, which - in case of attributes - will in fact be an Attr.

Categories