Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

How to read this xml file in java (need to be done by using exported,firstName,lastName,...)?

<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="XXXXXXX">
<cuobject type-id="EmailSubscription" object-id="jjjil@gmail.com">
    <object-attribute attribute-id="exported">true</object-attribute>
    <object-attribute attribute-id="firstName">jjj</object-attribute>
    <object-attribute attribute-id="lastName">jjj</object-attribute>
    <object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>

<cuobject type-id="EmailSubscription" object-id="gggj@gmail.com">
    <object-attribute attribute-id="exported">true</object-attribute>
    <object-attribute attribute-id="firstName">ghh</object-attribute>
    <object-attribute attribute-id="lastName">fhh</object-attribute>
    <object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>

<cuobject type-id="EmailSubscription" object-id="mmm@gmail.com">
    <object-attribute attribute-id="exported">true</object-attribute>
    <object-attribute attribute-id="firstName">mmm</object-attribute>
    <object-attribute attribute-id="lastName">mmm</object-attribute>
    <object-attribute attribute-id="subscribed">true</object-attribute>
</cuobject>
</objects>

How to read this xml file in java (need to be done by using exported,firstName,lastName,...)? how do we do that? I am doing it like

NodeList nList = doc.getElementsByTagName("cuobject");

for (int temp = 0; temp < nList.getLength(); temp++) 
{               

    Node nNode = nList.item(temp);       

    System.out.println("
Current Element :" + nNode.getNodeName());         

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

        Element eElement = (Element) nNode;

        String email = eElement.getAttribute("object-id");

        String firstName =  eElement.

        getElementsByTagName("object-attribute").item(1).getTextContent();

        String lastName = eElement.

        getElementsByTagName("object-attribute").item(2).getTextContent();

        String subscribed = eElement.

        getElementsByTagName("object-attribute").item(3).getTextContent();

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here we go, if you have no problem with JAXB

  1. Convert your xml to XSD.

    You can convert xml to XSD online such as (http://www.freeformatter.com/xsd-generator.html)

  2. Convert XSD to Java Class

    if you are using eclipse juno you can easily convert it schema to Java class. check out for the detailed step here (http://theopentutorials.com/examples/java/jaxb/generate-java-class-from-xml-schema-in-eclipse-ide/)

  3. Use the XML as an object( in line to OOPS, which suites very well to your customer details XML) to access the values

    public class MyTestClass {

    /**
     * @param args
     */
    public static void main(String[] args) {
        File file = new File("C:\kar\file1.xml");
        try{
        JAXBContext jaxbContext = JAXBContext.newInstance(Objects.class);
        Unmarshaller jaxbUnMarshaller = jaxbContext.createUnmarshaller();
    
    
        Objects listcustomers = (Objects) jaxbUnMarshaller.unmarshal(file);
        List<Cuobject> cusmters=listcustomers.getCuobject();
        Iterator<Cuobject> it = cusmters.iterator();
        while(it.hasNext()){
            Cuobject customer = it.next();
            System.out.println("Customer ID: "+ customer.typeId + " Customer email" + customer.objectId);
            List<ObjectAttribute> details =customer.objectAttribute;
            Iterator<ObjectAttribute> it1 = details.iterator();
            System.out.println(" -------Details------------");
            while(it1.hasNext()){
                ObjectAttribute detail = it1.next();
    
                System.out.println(detail.attributeId + " =" + detail.value);
            }
        }
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
    

    }

  4. Output would be something like this

    Customer ID: EmailSubscription Customer emailjjjil@gmail.com -------Details------------ exported =true firstName =jjj lastName =jjj subscribed =true

    Customer ID: EmailSubscription Customer emailgggj@gmail.com
     -------Details------------
    exported =true
    firstName =ghh
    lastName =fhh
    subscribed =true
    Customer ID: EmailSubscription Customer emailmmm@gmail.com
     -------Details------------
    exported =true
    firstName =mmm
    lastName =mmm
    subscribed =true
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...