I am trying to automate this process.

I have link to this xml file. it has many parameters, but I am only interested in 3 of it's parameters. I want to use those 3 parameter and create new xml file automactially with different tag names.

see screenshot.

Original XML file.XML File

I am only interested in all the //item/image",//item/source/file_details/file, and //item/title.

How can I automatically create another XML file and get all these parameters there with different meta names?

I have tried importing importxml in Google spreadsheet but don't know how to create XML from it.

final result should be like this. `http://blog.defaultroute.com/2011/01/04/building-a-roku-channel-part-2/



You could write a Java Application to do the trick.

E.g. to read the data you need:

File fXmlFile = new File(fileName);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        NodeList playlists = doc.getElementsByTagName("playlist");      // Get all Nodes named 'playlist'
        if(playlists != null && playlists.getLength() > 0){
            for (int temp = 0; temp < playlists.getLength(); temp++) {     // Iterate through all 'playlists'
                if(playlists.item(temp).getNodeType() == Node.ELEMENT_NODE){     //Check if they are Elements (just to be sure)
                    Element playlist = (Element) playlists.item(temp);       // Cast to Element

                    NodeList items = playlist.getElementsByTagName("item");     // Get Nodes named 'item' in 'playlist'

                    for (int temp1 = 0; temp1 < items.getLength(); temp1++) {       // Iterate through all items
                        if(items.item(temp1).getNodeType() == Node.ELEMENT_NODE){
                            Element item = (Element) items.item(temp1);

                            NodeList imgs = item.getElementsByTagName("image");
                            Node img = imgs.item(0);              // We suppose there is only one image-node in the item-node
                            if(img.getNodeType() == Node.ELEMENT_NODE){
                                Element image = (Element) img;
                                String imgUrl = image.getTextContent();      // Get the textContent of image --> Store it in an Array(List) etc...                                      

Of course you got to do the same for the other Nodes too.

And write it back in an XML File using the DocumentBuilder.

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("playlist");

Element item = doc.createElement("item");

Element image = doc.createElement("image");
image.appendChild(doc.createTextNode("HERE YOUR ImgURL "));

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(fileName)); 
transformer.transform(source, result);

Hope this helps a bit.

  • Thank you so much..I haven't started working on it, but I am sure this is the way to approach. – user206168 Sep 4 '14 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.