I have a XML content like below...
<Parent_Tag>
<Parent_Tag_1>
<Component>
<Type>ABC</Type>
<Amount>
<Value_1>0.1</Value_1>
<Value_2>AAA</Value_2>
</Amount>
</Component>
</Parent_Tag_1>
<Parent_Tag_2>
<Item>
<Item_1>
<Component>
<Type>CCC</Type>
<Amount>
<Value_1>0.1</Value_1>
<Value_2>BB</Value_2>
</Amount>
</Component>
<Component>
<Type>BBB</Type>
<Amount>
<Value_1>2.0</Value_1>
<Value_2>AA</Value_2>
</Amount>
</Component>
</Item_1>
</Item>
</Parent_Tag_2>
</Parent_Tag>
I need to get the values in tag Component under Parent_Tag_1. But my code gave all the component tags under Parent_Tag. How to fix this ??
Please give a suggestion...
Related
I'm rewriting C# application into java code.
There is REST API which return jsons.
I have to parse json to XML but C# library and Java doing it in difference ways.
How to keep type= attribute in java? I can't use JAXB annotations becouse there are too many objects in response and they might changing. XML.toString(jsonObject) doesn't work for me.
C# parsing is done in this way:
XDocument.load(JsonReaderWriterFactory.CreateJsonReader(Encoding.ASCII.GetBytes(jsonString), new XmlDictionaryReaderQuotas()));
C# result:
<root type="object">
<Items type="array">
<item type="object">
<Name type="string">test</Name>
<Total type="number">12.8000000</Total>
<CurrencyCode type="string">CHF</CurrencyCode>
<Country type="string">CH</Country>
</item>
</Items>
</root>
Java result:
<root>
<Items>
<item>
<Name>test</Name>
<Total>12.8000000</Total>
<CurrencyCode>CHF</CurrencyCode>
<Country>CH</Country>>
</item>
</Items>
</root>
I've used org.w3c.Document and org.w3c.dom.Element and set up attribute "type".
Anyway thanks for help :)
I have a very simple xml file that I would like to create a simple function to remove a tag from it. Here is my sample xml file:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channels>
<channel>
<items>
<item>
<title>Java Tutorials></title>
<link>http://www.tutorial-point.com/</link>
</item>
<item>
<title>Java Tutorials></title>
<link>http://www.javatpoint.com/</link>
</item>
</items>
</channel>
</channels>
</rss>
In my Java program, simply want to call a method to delete two tags from the file. I'm not very familiar with XML but did manage to create a reader and writer but now I'm having trouble creating a method to delete an item from my file.
// retrieve the element
Element element = (Element) doc.getElementsByTagName("channels").item(0);
Element element2 = (Element) doc.getElementsByTagName("channel").item(0);
// remove the specific node
element.getParentNode().removeChild(element);
element2.getParentNode().removeChild(element2);
When I used the above code in Java, it removed all the tags but I expected the result like below:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<items>
<item>
<title>Java Tutorials></title>
<link>http://www.tutorial-point.com/</link>
</item>
<item>
<title>Java Tutorials></title>
<link>http://www.javatpoint.com/</link>
</item>
</items>
</rss>
Can you please suggest?
Try this.
Element rss = (Element) doc.getElementsByTagName("rss").item(0);
Element channels = (Element) doc.getElementsByTagName("channels").item(0);
Element items = (Element) doc.getElementsByTagName("items").item(0);
rss.appendChild(items);
rss.removeChild(channels);
I am getting an error message when trying to add a Spring component to a Mule Flow. This should be a common user-case, but I wasn't able to find the right documentation or examples. Thanks in advance.
The follow was the original configuration and works fine:
<flow name="ApplicationEndpoint">
<inbound-endpoint address="server:port/JSONAPI/"/>
<jersey:resources>
<component>
<spring-object bean="myJerseyService"/>
</component>
</jersey:resources>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<flow-ref name="ErrorHandling" doc:name="Flow Reference"/>
</catch-exception-strategy>
</flow>
I simply want to add a new component to do some post-processing. When I try this, it doesn't work:
<flow name="ApplicationEndpoint">
<inbound-endpoint address="server:port/JSONAPI/"/>
<jersey:resources>
<component>
<spring-object bean="myJerseyService"/>
</component>
</jersey:resources>
<component>
<spring-object bean="postProcessor"/>
<component>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<flow-ref name="ErrorHandling" doc:name="Flow Reference"/>
</catch-exception-strategy>
</flow>
Where "postProcessor" maps elsewhere in the config as a spring bean.
The error message I get is:
org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'component'. One of '{"http://www.mulesoft.org/schema/mule/core":abstract-lifecycle-adapter-factory, "http://www.mulesoft.org/schema/mule/core":binding}' is expected.
The above error clearly shows that the tag <component> is not closed..
for example, it should be in following format :-
<component>
<spring-object bean="postProcessor"/>
</component>
where you need to end the tag like the following :- </component>
One more thing ... I tried to run your code, but due to server:port/JSONAPI/ configured in your inbound-endpoint address it gives a error saying the xml is malformed
So I modified your code as following and it ran successfully :-
<flow name="ApplicationEndpoint">
<inbound-endpoint address="http://localhost:8189/JSONAPI"/>
<jersey:resources>
<component>
<spring-object bean="myJerseyService"/>
</component>
</jersey:resources>
<component>
<spring-object bean="postProcessor"/>
</component>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<flow-ref name="ErrorHandling" doc:name="Flow Reference"/>
</catch-exception-strategy>
</flow>
So, you can now use it and modify as per your requirement
I've set the following xml as payload in order to iterate over every product using splitter component.
<root>
<product>
<id>1</id>
<name>apple</name>
</product>
<product>
<id>2</id>
<name>orange</name>
</product>
</root>
<splitter expression="#[xpath('//product')]" />
The splitter component returns an object of type org.dom4j.tree.DefaultElement on which I call the method asXML() to get single product's xml.
First iteration
<product>
<id>1</id>
<name>apple</name>
</product>
Second iteration
<product>
<id>2</id>
<name>orange</name>
</product>
I need to replace splitter with foreach component, but I'm having some troubles.
<foreach collection="#[xpath('//product')]">
...
</foreach>
The foreach component returns an object of type org.apache.xerces.dom.ElementNSImpl which hasn't the method asXML().
Any idea how I can get products'xml as String as explained in the first example?
Thanks in advice!
Use Mule's dom-to-xml-transformer.
Reference: http://www.mulesoft.org/documentation/display/current/DomToXml+Transformer
I want to represent the following xml into a java object, and the ordering matters for the sub-nodes.
<document version="1.1">
<id>abc23423lja123423</id>
<version>
<revision>23</revision>
<date>2011/11/28</date>
</version>
<body>
<title>some title</title>
<desc>some desc</desc>
<full>
<line number="1" count="31" text="some text goes here" />
<line number="2" count="31" text="some text goes here" />
<line number="3" count="31" text="some text goes here" />
</full>
</body>
<author>
<name>John Doe</name>
<address>
<city>mississipi</city>
<country>usa</country>
</address>
<meta-data>
<item key="age">33</item>
<item key="books">19</item>
<item key="related>
<item isbn="2342343242343">some title2</item>
<item isbn="2312888888">other title3</item>
</item>
</meta-data>
</author>
</document>
So my class is Document:
public class Document {
String version;
String id;
}
So the sub-items have to be in the same order, meaning the xml nodes in the meta-data node (item) have to be ordered as they appear in the xml file.
How would you design this class? Using many inner classes or would you do it in a more generic way somehow?
You could have JAXB sort it out for you. Have it generate classes for you and see how you like what it comes up with; tweak as necessary.
I wouldn't have inner classes. I'd create classes like DocumentMetaData; I don't know what your <full> and <item> are about, but those would be List of classes as well.