0

I used java dom to parsing rdf xml in java, but I couldn't parsing rdf xml in the direction I wanted. Here is my code :

String[] arr = new String[100];

    DocumentBuilderFactory factory_parsing = DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder builder_parsing = factory_parsing.newDocumentBuilder();
        Document document = builder_parsing.parse("D://Test/dog_test.xml");

        Element root = document.getDocumentElement();
        System.out.println(root.getNodeName());


        Node firstNode = root.getFirstChild();
        Node next = firstNode.getNextSibling();

        NodeList childList = next.getChildNodes();

        System.out.println(childList.getLength());
        for(i=0; i<childList.getLength(); i++){
            Node item = childList.item(i);
            if(item.getNodeType() == Node.ELEMENT_NODE){
                System.out.println(item.getNodeName());
                arr[i] = item.getNodeName();
                System.out.println(item.getTextContent());
            }else {
                System.out.println("blank node.");
            }
        }

The result :

rdf:RDF
5
blank node.
test:Animal.Name
Jeck
blank node.
test:Dog.breed
Akita
blank node.

dog_test.xml :

<rdf:RDF 
xmlns:test="http://www.test/2022#" 
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" 
xml:base="http://www.desktop-eqkvgq5.net/2022#">
<test:Dog rdf:ID="_100">
<test:Animal.Name>Jeck</test:Animal.Name>
<test:Dog.breed>Akita</test:Dog.breed>
</test:Dog>
<test:Cat rdf:ID="_101">
<test:Animal.Name>Tom</test:Animal.Name>
<test:Cat.breed>Munchkin</test:Cat.breed>
</test:Cat>
</rdf:RDF>

So I analyzed it hard to use the Jena method, but I don't know how to use it. I'm exhausted. My question is two.

  1. Is my java dom code wrong? The result is a mess. Even information on the cat is completely missing. Is java dome unable to parsing rdf xml?
  2. I'd like to use the RDFXMLParser provided by Jena, but I'd like to know an example of using it.

What I want is to store data corresponding to s, p, and o as a string and change the final result to triple.

The results I want are as follows :

100 rdf:type test:Dog
100 test:Animal.Name Jeck
100 test:Dog.breed Akita
101 rdf:type test:Cat
101 test:Animal.Name Tom
101 test:Cat.breed Munchkin

Thank you for any advice

user20297975
  • 129
  • 8
  • 1
    The cat element is the **second** element child of the root element. Your code explicitly only traverses the children of the **first** child of the root element. (Your code does not even try to get the first element child, but uses the first child of any type. You are lucky to get any output at all.) – Thomas Behr Dec 07 '22 at 13:43
  • Jena has a lot of documentation available. I would start with the [RDF Core API Tutorial](https://jena.apache.org/tutorials/rdf_api.html) and in particular the section on [Reading RDF](https://jena.apache.org/tutorials/rdf_api.html#ch-Reading-RDF) – RobV Dec 08 '22 at 09:53

0 Answers0