How to parsing rdf xml in Java

106 Views Asked by At

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

0

There are 0 best solutions below