在使用dom解析XML文件时需要帮助

时间:2014-01-10 05:58:21

标签: dom xml-parsing saxparser

<?xml version="1.0" encoding="UTF-8"?>
    <OrderList>
            <Order>
            <StoreCode>FFF</StoreCode>
            <CustomerAccount>test1@testing.com</CustomerAccount>
            <CustomerOrderNumber>1579221</CustomerOrderNumber>
            <CustomerOrderSubmissionDate>20131219080856</CustomerOrderSubmissionDate>
            <BillTo>
                <TenderList>
                    <Tender>
                        <TenderType>DC</TenderType>
                        <TenderCode>1111</TenderCode>
                        <ExpireDate>11/2014</ExpireDate>
                        <CCVNumber/>
                    </Tender>
                </TenderList>
            </BillTo>
            <OrderLineList>
                <Line LineNumber="1">
                    <ProductId>19512826</ProductId>
                    <Quantity>1</Quantity>
                    <ShipMethod>GROUND</ShipMethod>
                    <RegistryID/>
                    <ShipToAddress>
                        <Line1>123 xyx AZ</Line1>
                        <City>zzyz</City>
                        <State>NY</State>
                        <PostalCode>10464</PostalCode>
                        <Phone>1234567890</Phone>
                        <FirstName>TEsT</FirstName>
                        <LastName></LastName>
                        <CountryCode>US</CountryCode>
                        <ShipToEmail/>
                    </ShipToAddress>
                    <ShippingValue>4.95</ShippingValue>
                    <UnitPrice>8.05</UnitPrice>

                </Line>
            </OrderLineList>
        </Order>


    <Order>
            <StoreCode>FFF</StoreCode>
            <CustomerAccount>test1@testing.com</CustomerAccount>
            <CustomerOrderNumber>1579221</CustomerOrderNumber>
            <CustomerOrderSubmissionDate>20131219080856</CustomerOrderSubmissionDate>
            <BillTo>
                <TenderList>
                    <Tender>
                        <TenderType>DC</TenderType>
                        <TenderCode>1111</TenderCode>
                        <ExpireDate>11/2014</ExpireDate>
                        <CCVNumber/>
                    </Tender>
                </TenderList>
            </BillTo>
            <OrderLineList>
                <Line LineNumber="1">
                    <ProductId>19512826</ProductId>
                    <Quantity>1</Quantity>
                    <ShipMethod>GROUND</ShipMethod>
                    <RegistryID/>
                    <ShipToAddress>
                        <Line1>123 xyx AZ</Line1>
                        <City>zzyz</City>
                        <State>NY</State>
                        <PostalCode>10464</PostalCode>
                        <Phone>1234567890</Phone>
                        <FirstName>TEsT</FirstName>
                        <LastName></LastName>
                        <CountryCode>US</CountryCode>
                        <ShipToEmail/>
                    </ShipToAddress>
                    <ShippingValue>4.95</ShippingValue>
                    <UnitPrice>8.05</UnitPrice>

                </Line>                
                <Line LineNumber="2">
                    <ProductId>19512826</ProductId>
                    <Quantity>1</Quantity>
                    <ShipMethod>GROUND</ShipMethod>
                    <RegistryID/>
                    <ShipToAddress>
                        <Line1>123 xyx AZ</Line1>
                        <City>zzyz</City>
                        <State>NY</State>
                        <PostalCode>10464</PostalCode>
                        <Phone>1234567890</Phone>
                        <FirstName>TEsT</FirstName>
                        <LastName></LastName>
                        <CountryCode>US</CountryCode>
                        <ShipToEmail/>
                    </ShipToAddress>
                    <ShippingValue>4.95</ShippingValue>
                    <UnitPrice>8.05</UnitPrice>

                </Line>
            </OrderLineList>
        </Order>
    </OrderList>

我需要验证以下条件。

我需要跟踪每个订单,我应该验证每个订单是否为空,我还需要存储相应订单的空字段标签。

我正在使用DOM解析器。是否足以使用它。如何验证订单是否有多个行号。

1 个答案:

答案 0 :(得分:0)

以下是解析xml以上的示例代码。只需在代码中正确更改文件位置。

import java.io.BufferedWriter;
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import javax.xml.parsers.DocumentBuilder;  
import javax.xml.parsers.DocumentBuilderFactory;  
import org.w3c.dom.Document;  
import org.w3c.dom.Node;  
import org.w3c.dom.NodeList;  



public class format {


public static void main(String[] args) throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
    dbf.setValidating(false); 
    DocumentBuilder db = dbf.newDocumentBuilder();   

  // replace following  path with your input xml path  
     Document doc = db.parse(new FileInputStream(new File   ("C:\\Users\\ambuj_mishra\\Desktop\\order.xml")));  

  // replace following  path with your output xml path 
     File OutputDOM = new File("C:\\Users\\ambuj_mishra\\Desktop\\outapip1.txt");
        FileOutputStream fostream = new FileOutputStream(OutputDOM);
        OutputStreamWriter oswriter = new OutputStreamWriter (fostream);
        BufferedWriter bwriter = new BufferedWriter(oswriter);

        // if file doesnt exists, then create it
        if (!OutputDOM.exists()) {
            OutputDOM.createNewFile();}


        visitRecursively(doc,bwriter);
        bwriter.close(); oswriter.close(); fostream.close();

        System.out.println("Done");
   }
   public static void visitRecursively(Node node, BufferedWriter bw) throws IOException{  

         // get all child nodes  
     NodeList list = node.getChildNodes();                                  
     for (int i=0; i<list.getLength(); i++) {          
             // get child node              
   Node childNode = list.item(i);  
   if (childNode.getNodeType() == Node.TEXT_NODE)
   {
   //System.out.println("Found Node: " + childNode.getNodeName()           
  //  + " - with value: " + childNode.getNodeValue()+" Node type:"+childNode.getNodeType()); 

String nodeValue= childNode.getNodeValue();
nodeValue=nodeValue.replace("\n","").replaceAll("\\s","");
if (!nodeValue.isEmpty())
{
   System.out.println(nodeValue);
   bw.write(nodeValue);
   bw.newLine();
}
   }
   visitRecursively(childNode,bw);  

        }         

 }  

}