How to use nested element as root node with SimpleXML in Java? - java

assume the following XML structure:
<A>
<B>
<C>
<!-- other stuff -->
</C>
</B>
</A>
I am not interested in mapping tags A and B, I just want to map C and what is within it. I tried it like this:
#Root
#Path("/A/B")
public class C {
// Mapping for fields within
}
Also tried #Path("/A/B/C") but nothing seems to work. I don't want to create mappings for the outer tags.
It crashes with the following error message:
"Exception in thread "main" org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy #org.simpleframework.xml.Element(name=, type=void, data=false, required=true) on field 'InvoiceNumber' public java.lang.String silc.xml.ups.Invoice.InvoiceNumber for class silc.xml.ups.Invoice at line 2"
Here, "InvoiceNumber" is another element within tag C. If I comment out A and B everything works as intended with no error.
My expected result is, that tags A and B are ignored and C is treatet as root node for parsing the XML document.
Any suggestions on how to do this?

Related

Compare two XML attributes with same name in two different XML files using Java

I have an XML with attributes and elements/tags.
I want to know whether using an attribute or a tag is good according to performance.
Could you please give an example to compare if the content has a child tag and also if the content has a attribute.
My question is, is it possible to compare 2 attributes with same name in 2 different XML files and also here we will have huge data.
So, I want to be sure how the performance is, if i consider it as a attribute or tag.
<A Name="HRMS">
<B BName="IN">
<C Code="0001">
<IN irec="200" />
<OUT orec="230" Number="" Outname=""/>
</C>
<C Code="0004">
<IN irec="209" />
<OUT orec="209" Number="" Outname=""/>
</C>
<C Code="0008">
<IN irec="250" />
<OUT orec="250" Number="" Outname=""/>
</C>
</B>
</A>
Here, i have to compare irec with orec for a particular B name and C code
It's possible. You need a java lib like jsoup to help parse xml by path expression like jquery css selection expression.
Jsoup is a HTML parser, but html is a kind of xml application, so you can use it to parse xml content.
jsoup example:
String xml = "<root><person name=\"Bob\"><age>20</age></person></root>";
Document root = Jsoup.parse(xml);
System.out.println(root.body().html());//origin XML content
Elements persons = root.getElementsByTag("person");
Element person = persons.first();
System.out.println("The attribute 'name' of Person:" + person.attr("name"));
System.out.println(persons.select("person[name=Bob]").first().text());
You can implement compare difference function using jsoup simplily.

what causes the org.xml.sax.saxparseexception: already seen doctype

I am using Eclipse to find a specific anchor elements in XML file. The plugin I am using in Eclipse is XPath. When I typed in the element I want to find, it shows:
org.xml.sax.saxparseexception: already seen doctype
<div class="list">
<input name = "abc" type = "hidden" value = "true">
...
and if I want to search all the attribute name. I type in //#name in the expression field. it shows the error warning like that. Did I typed in the wrong expression or any ways find the element? Thanks

InvalidReferenceException while using struts2 doubleselect tag

I need to show two dropdown for Class-Section.
Description:
first select is appearing but second select is not showing the value in the dropdown.
My double select looks like:
<s:doubleselect label="Standard"
name="standard"
listValue="standard"
list="#session.standardList"
doubleList="#session.standardList.section"
doubleName="section"
doubleListValue="section"></s:doubleselect>
Standard class looks like:
public class Standard {
String standard;
ArrayList<String> section;
// getters and setters below
}
Exception:
freemarker.core.InvalidReferenceException: Expression parameters.formName is undefined on line 150, column 43 in template/simple/doubleselect.ftl.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125)
at freemarker.core.Expression.getStringValue(Expression.java:118)
I found the root cause of the issue, As the exception says formName was not found, i encapsulated double-select tag inside the form tag and all start working fine.
Now form tag looks like:
<s:form name="studentsubmit" action="add/submitstudent">
<s:doubleselect label="Standard" name="standard" listValue="standard"
listKey="standard" list="#session.standardList" doubleList="section"
doubleName="section" ></s:doubleselect>
</s:form>

Replacing xml node from another xml

i use Java and I have 2 xml files like
<xml>
<a value="5">
<b value="7">
<c>
<d value="9">
</c>
<xml>
and
<xml>
<c>
<d value="8">
</c>
<xml>
So what i want is for every node in second xml if there exists with same node path in first xml, replace the first xml's node with the second xml's node.For these xmls, i expect
<xml>
<a value="5">
<b value="7">
<c>
<d value="8">
</c>
<xml>
Many Thanks for Your Help
You can use a Sax parser and iterate through second XML and get all the available nodes. Or use DOM for that. Same way get all nodes in first XML. Then write a logic to find the matching nodes. Then use DOM to edit first XML. Try it your self so that you can learn. See how to edit XML here
there man ways to read xml and write xml like DOM parser , jaxB so i would prefer to use JAXB marshall and unmarshaller so that you can have an object of your xml file and to set the value and get the value becomes easier

unmarshalling <br/> in XML data

I have some xml data I'm trying to unmarshall into java objects and one of the elements contains <br/> elements:
<details>
<para>
Line Number One
<br/>
Line Number Two
</para>
</details>
In my Details java object I have:
class Details {
#XmlElement(name="para")
private List<String> paragraphs;
}
The problem is that the only element in the paragraphs list is 'Line Number Two'. Does anyone know how I can deal with this?
You can represent mixed content with #XmlMixed as follows (note that it's applied to content of a class itself rather than to its element, thus you need an additional class):
class Details {
#XmlElement(name="para")
private Para para;
...
}
class Para {
#XmlMixed
#XmlAnyElement
private List<Object> paragraphs;
...
}
paragraphs property will contain Strings for text lines and Elements for XML elements.
In that case the XML is not formed correctly. Put the entire data inside the tags within CDATA to avoid this issue. Refer - http://www.w3schools.com/xml/xml_cdata.asp
You could use #XmlAnyElement along with a DomHandler to preserve fragments of the XML document as a String. Below is a link to a complete example demonstrating how to do this:
http://blog.bdoughan.com/2011/04/xmlanyelement-and-non-dom-properties.html

Categories