1

I'm trying to read XML input from command line in Python3. So far I tried various method and following is my code for read XML,

import sys
import xml.dom.minidom
try:
    input = sys.stdin.buffer
except AttributeError:
    input = sys.stdin

xmlString = input.read()

But this continuedly getting inputs please someone can tell how to stop getting inputs after getting XML file

My XML file is,

<response>
 <article>
  <title>A Novel Approach to Image Classification, in a Cloud Computing Environment stability.</title>
  <publicationtitle>IEEE Transactions on Cloud Computing</publicationtitle>
  <abstract>Classification of items within PDF documents has always been challenging.  This stability document will discuss a simple classification algorithm for indexing images within a PDF.</abstract>
 </article>
 <body>
  <sec>
   <label>I.</label>
   <p>Should Haven't That is a bunch of text pattern these classification and cyrptography.  These paragraphs are nothing but nonsense.  What is the statbility of your program to find neural nets. Throw in some numbers to see if you get the word count correct this is a classification this in my nd and rd words.  What the heck throw in cryptography.</p>
   <p>I bet diseases you can't find probability twice.  Here it is a again probability.  Just to fool you I added it three times probability.  Does this make any pattern classification? pattern classification! pattern classification.</p>
   <p>
    <fig>
     <label>FIGURE.</label>
     <caption>This is a figure representing convolutional neural nets.</caption>
    </fig>
   </p>
 </sec>
 </body>
</response>

Since this has number of lines I cant input this from conventional way using input()

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63

1 Answers1

2

Reading from the console / command line is done with input(). Try:

import xml.dom.minidom

xmlString = input()

For more details on sys.stdin, take a look at this SO post.

Edit: If you wanted to read multiple lines from the console, try sys.stdin.readlines, like xmlString = sys.stdin.readlines(). The user terminates multi-line input with CTRL+D. Or, you can just have the user write the XML to a file, and parse that file (easier, but maybe not desireable).

Collin Heist
  • 1,962
  • 1
  • 10
  • 21