Copying content of one xml into other using xslt - java

I am trying to copy a content from one xml to other xml using Xslt.
I need to copy content of file1
<?xml version="1.0"?>
<products author="Jesper">
<product>
<name>Delta</name>
<price>800</price>
<stock>
<price>13a</price>
</stock>
<place>Denmark</place>
</product>
</products>
to file 2. File2 has similar tags but order is jumbled,
<?xml version="1.0"?>
<products author="Jesper">
<product>
<stock>
<price>13d</price>
</stock>
<price>700</price>
<place>Copenhagen</place>
<name>Beta</name>
</product>
</products>
expected output
<products author="Jesper">
<product>
<stock>
<price>13a</price>
</stock>
<price>800</price>
<place>Denmark</place>
<name>Delta</name>
</product>
</products>
so basically I need to Iterate through file1 using for-each and then find the matching tag in file2 and copy the tag value. Not sure about an efficient way to do so ... Double iterating is inefficient. Any suggestion will be helpful.

This is a very broad question, but I'll try to give you some pointers that should get you started. You will probably want to use the doc() function to load the files since XSLT only allows you to iterate over a single "main" file. doc() loads a new file into a variable that you can apply templates to and so on. If you are concerned about the iteration performance, you should learn about xsl:key and the key() function, which build indexes that will help with that.

Related

Append data after root element without loading the whole XML file into memory

I have to output an XML file which can contain use amount of data, I am using DOM parser to write XML file. It is also possible to append data to an existing XML file.
My requirement is add data to the root element.
Is it possible to append data without reading the entire XML document (Not to load XML into memory)?
Example Data:
Current XML file:
<employees>
<employee>
<name>jon</name>
<age> 22</age>
<address> address1 </address>
</employee>
</employees>
Required file:
<employees>
<employee>
<name>jon</name>
<age> 22</age>
<address> address1 </address>
</employee>
<employee>
<name>jon1</name>
<age> 24</age>
<address> address2 </address>
</employee>
</employees>
It would be hard if you don't want to load entire XML into memory.
You can achieve this by manipulating raw String (substring, etc.) - but I don't recommend this.
Or you can try using SAX reader http://www.saxproject.org/apidoc/org/xml/sax/XMLReader.html which enables you to manipulate XMLs "on the go". (I'm sorry, although you can use SAX parsers to process XML without reading its whole content, you cannot edit with it)
EDIT:
On second though you can copy existing XML using SAX parser, and by adding event listener to e.g. root you can add a child. It might be good solution if your concern is memory (big xml file).
You could use DOM4j for doing that.

Best way to bind a XML File in Java (NetBeans)

i want to bind this simple XML File in my java project:
<?xml version="1.0" encoding="UTF-8"?>
<itg>
<reader>
<chapter id="1">
<subchapter id="1"></subchapter>
<subchapter id="2"></subchapter>
</chapter>
<chapter id="2">
<subchapter id="1"></subchapter>
<subchapter id="2"></subchapter>
</chapter>
<chapter id="3"></chapter>
</reader>
<questions>
</questions>
</itg>
I use NetBeans, and actually i bind the XML File by parsing the xml file into a ArrayList, an bind the list.
It works, but it is possible to bind the xml File in a better way?
Thanks!
For this small XML (and not only) I would recommend that you take a look at JAXB. The two basic operations are marshalling (converting Java objects to XML data) and unmarshalling (converting XML data to Java objects) but verification and so on is also provided.

Java DOM How to check if node exists in XML

Here is my xml file
<?xml version="1.0" encoding="UTF-8"?>
<productlist>
<product>
<title>1</title>
<price>30</price>
</product>
<product>
<title>2</title>
<price>9</price>
</product>
<product>
<title>3</title>
</product>
</productlist>
What I need to do is use Java DOM API to print out all the contents from XML file which contain "price" tag. Such things like this
title:1
price:30
title:2
price:9
In the org.w3c.dom documentation I only find hasAttribute(String name) method to check if this element have attribute but I can not find method like "hasTag(String name)" in documentation. I find this website http://www.java-forums.org/xml/62136-check-whether-xml-tag-exists-while-parsing-xml-using-dom-java.html but unfortunately I can not open the site. Hope you can help.
Just call getElementsByTagName and see if the returned list has any nodes (using NodeList.getLength()).

XML highcharts file creation using java

the question is how to create this xml file exactly for me to be able to use it in the get function of highcharts? The CSV file is created rapidly but now i want to create the xml file from the back end which is java.
<chart>
<categories>
<item>..</item>
</categories>
<series>
<name>xxx</name>
<data>
<point>1</point>
<point>2</point>
<point>3</point>
<point>4</point>
</data>
</series>
</chart>
Upon request:
<chart>
<categories>
<item>Apples</item> these two items must be populated from the database
<item>Pears</item>
</categories>
<series>
<name>John</name> this one also from database
<data>
<point>8</point> this is the data part, retrieved from database
<point>4</point>
<point>6</point>
<point>5</point>
</data>
</series>
</chart>
I want to create this type of xml structure using a java method or class where i retrieve all the information i need and then pass in into the xml file.
Anyways, am using xml to populate data for highcharts now.. Thanks for the answer

Finding all valid xpath from xml

I am trying to write a program in java where in i can find all the xpath for the given xml.I found out the link on the internet xpath generator but it does not work when one element can repeat multipletimes for example if we have xml like the following :-
<?xml version="1.0" encoding="UTF-8"?>
<Report>
<Name>
<FirstName>A</FirstName>
<LastName>B</LastName>
<MiddleName>C</MiddleName>
</Name>
<Name>
<FirstName>D</FirstName>
<LastName>E</LastName>
<MiddleName>S</MiddleName>
</Name>
</Report>
It will produce xpaths :-
/Report/Name/firstname for both firstname nodes.
but the expected should be /Report/Name1/firstname and /Report/Name[2]/firstname
Any ideas?
I think you may have to do this yourself.
Using a SAX parser will make it straightforward. Just maintain a stack of the elements you encounter and a count so you can increment the indexes (/Report/Name[1], /Report/Name[2]) easily.

Categories

Resources