XML value of node attribute

I am trying to read an xml file, for example:

<entry>
    <title>FEED TITLE</title>
    <id>5467sdad98787ad3149878sasda</id>
    <tempi type="application/xml">
      <conento xmlns="http://mydomainname.com/xsd/radiofeed.xsd" madeIn="USA" />
    </tempi>
</entry>

Here is the code that I still have:

Here is my attempt to try to encode this, which it was not possible to say, so I started generosity. Here is http://pastebin.com/huKP4KED .

Bounty Update:

I really really tried to do this for several days, so I did not expect it to be so difficult, I will accept useful links / books / tutorials, but I prefer the code because I need it yesterday.

This is what I need:

Regarding xml above:

  • I need to get the value title, id
  • the value of the tempi attribute, as well as the value of the In attribute for contento

What is the best way to do this?

EDIT:

@ Pascal Tivent

, , public String getValue (String xml, Element elementname), , (, ),

, () , , ,

+3
3

XPath. , . , feed.xml :

<?xml version="1.0" encoding="UTF-8" ?>
<entries>
<entry>
    <title>FEED TITLE 1</title>
    <id>id1</id>
    <tempi type="type1">
      <conento xmlns="dontcare?" madeIn="MadeIn1" />
    </tempi>
</entry>
<entry>
    <title>FEED TITLE 2</title>
    <id>id2</id>
    <tempi type="type2">
      <conento xmlns="dontcare?" madeIn="MadeIn2" />
    </tempi>
</entry>
<entry>
    <id>id3</id>
</entry>
</entries>

, ( feed.xml ).

import javax.xml.xpath.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;

public class XPathTest {
    static class Entry {
        final String title, id, origin, type;
        Entry(String title, String id, String origin, String type) {
            this.title = title;
            this.id = id;
            this.origin = origin;
            this.type = type;
        }
        @Override public String toString() {
            return String.format("%s:%s(%s)[%s]", id, title, origin, type);
        }
    }

    final static XPath xpath = XPathFactory.newInstance().newXPath();
    static String evalString(Node context, String path) throws XPathExpressionException {
        return (String) xpath.evaluate(path, context, XPathConstants.STRING);
    }

    public static void main(String[] args) throws Exception {
        File file = new File("feed.xml");
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
        NodeList entriesNodeList = (NodeList) xpath.evaluate("//entry", document, XPathConstants.NODESET);

        List<Entry> entries = new ArrayList<Entry>();
        for (int i = 0; i < entriesNodeList.getLength(); i++) {
            Node entryNode = entriesNodeList.item(i);
            entries.add(new Entry(
                evalString(entryNode, "title"),
                evalString(entryNode, "id"),
                evalString(entryNode, "tempi/conento/@madeIn"),
                evalString(entryNode, "tempi/@type")
            ));
        }
        for (Entry entry : entries) {
            System.out.println(entry);
        }
    }
}

:

id1:FEED TITLE 1(MadeIn1)[type1]
id2:FEED TITLE 2(MadeIn2)[type2]
id3:()[]

, XPath , , , "" .

API

+4

Element.getAttribute Element.setAttribute

((Node) content.item(0)).getFirstChild().getAttributes(). , content , contento, getFirstChild NULL, contento . : ((Node) contento.item(0)).getAttributes() .

, , getFirstChild getChildNodes()[0], , , .

+2

, <contento> , :

(contento.item(0)).getFirstChild().getAttributes()

Node Element getAttribute (String), - :

((Element)contento.item(0)).getAttribute("madeIn")

( , ):

InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(inputStream);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("entry");
System.out.println("Information of all entries");

for (int s = 0; s < nodeLst.getLength(); s++) {

    Node fstNode = nodeLst.item(s);

    if (fstNode.getNodeType() == Node.ELEMENT_NODE) {

        Element fstElmnt = (Element) fstNode;

        NodeList title = fstElmnt.getElementsByTagName("title").item(0).getChildNodes();
        System.out.println("Title : " + (title.item(0)).getNodeValue());

        NodeList id = fstElmnt.getElementsByTagName("id").item(0).getChildNodes();
        System.out.println("Id: " + (id.item(0)).getNodeValue());

        Node tempiNode = fstElmnt.getElementsByTagName("tempi").item(0);
        System.out.println("Type : " + ((Element) tempiNode).getAttribute("type"));

        Node contento = tempiNode.getChildNodes().item(0);
        System.out.println("Made in : " + ((Element) contento).getAttribute("madeIn"));
    }
}

XML :

Root element entry
Information of all entries
Title : FEED TITLE
Id: 5467sdad98787ad3149878sasda
Type : application/xml
Made in : USA

, - Rome?

+2

Source: https://habr.com/ru/post/1745430/


All Articles