如何在java中读取这个xml文件(需要使用exported,firstName,lastName,...)?

时间:2013-05-22 10:24:23

标签: java xml

<?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>

如何在java中读取此xml文件(需要使用exported,firstName,lastName,...)? 我们怎么做? 我这样做是为了

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

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

    Node nNode = nList.item(temp);       

    System.out.println("\nCurrent 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();

    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用XPath查询和Java支持查询Xml文档。

您可以参考How to read XML using XPath in Java了解更多信息

答案 1 :(得分:0)

如果您对JAXB没有任何问题,请转到此处

  1. 将xml转换为XSD

    您可以在线转换xml到XSD,例如(http://www.freeformatter.com/xsd-generator.html

  2. 将XSD转换为Java类

    如果您使用的是eclipse juno,则可以轻松地将其模式转换为Java类。请查看此处的详细步骤(http://theopentutorials.com/examples/java/jaxb/generate-java-class-from-xml-schema-in-eclipse-ide/

  3. 使用XML作为对象(与OOPS一致,非常适合您的客户详细信息XML)来访问值

    公共类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. 输出就是这样的

    客户ID:EmailSubscription客户emailjjjil@gmail.com   - - - -细节 - - - - - - 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